Java Reference
In-Depth Information
b)
Modify part (a) to have lines fan out from all four corners, as shown in the right screen
capture of Fig. 4.20. Lines from opposite corners should intersect along the middle.
4.2
Figure 4.21 displays two additional designs created using while loops and drawLine .
a)
Create the design in the left screen capture of Fig. 4.21. Begin by dividing each edge
into an equal number of increments (we chose 15 again). The first line starts in the top-
left corner and ends one step right on the bottom edge. For each successive line, move
down one increment on the left edge and right one increment on the bottom edge. Con-
tinue drawing lines until you reach the bottom-right corner. The figure should scale as
you resize the window so that the endpoints always touch the edges.
b)
Modify your answer in part (a) to mirror the design in all four corners, as shown in the
right screen capture of Fig. 4.21.
Fig. 4.21 | Line art with loops and drawLine .
4.16 Wrap-Up
This chapter presented basic problem solving for building classes and developing methods
for these classes. We demonstrated how to construct an algorithm (i.e., an approach to
solving a problem), then how to refine the algorithm through several phases of pseudocode
development, resulting in Java code that can be executed as part of a method. The chapter
showed how to use top-down, stepwise refinement to plan out the specific actions that a
method must perform and the order in which the method must perform these actions.
Only three types of control structures—sequence, selection and repetition—are
needed to develop any problem-solving algorithm. Specifically, this chapter demonstrated
the if single-selection statement, the if else double-selection statement and the while
repetition statement. These are some of the building blocks used to construct solutions to
many problems. We used control-statement stacking to total and compute the average of
a set of student grades with counter- and sentinel-controlled repetition, and we used con-
trol-statement nesting to analyze and make decisions based on a set of exam results. We
introduced Java's compound assignment operators and its increment and decrement oper-
ators. Finally, we discussed Java's primitive types. In Chapter 5, we continue our discus-
sion of control statements, introducing the for , do while and switch statements.
 
Search WWH ::




Custom Search