Java Reference
In-Depth Information
last month's sales and summarize the total sales by salesperson and by product. All totals should be
stored in the two-dimensional array sales . After processing all the information for last month, dis-
play the results in tabular format, with each column representing a salesperson and each row repre-
senting a particular product. Cross-total each row to get the total sales of each product for last month.
Cross-total each column to get the total sales by salesperson for last month. Your output should
include these cross-totals to the right of the totaled rows and to the bottom of the totaled columns.
7.21 (Turtle Graphics) The Logo language made the concept of turtle graphics famous. Imagine
a mechanical turtle that walks around the room under the control of a Java application. The turtle
holds a pen in one of two positions, up or down. While the pen is down, the turtle traces out shapes
as it moves, and while the pen is up, the turtle moves about freely without writing anything. In this
problem, you'll simulate the operation of the turtle and create a computerized sketchpad.
Use a 20-by-20 array floor that's initialized to zeros. Read commands from an array that con-
tains them. Keep track of the current position of the turtle at all times and whether the pen is cur-
rently up or down. Assume that the turtle always starts at position (0, 0) of the floor with its pen
up. The set of turtle commands your application must process are shown in Fig. 7.29.
Command
Meaning
Pen up
1
Pen down
2
Turn right
3
Tu r n l e f t
4
Move forward 10 spaces (replace 10 for a different number of spaces)
5,10
Display the 20-by-20 array
6
End of data (sentinel)
9
Fig. 7.29 | Turtle graphics commands.
Suppose that the turtle is somewhere near the center of the floor. The following “program”
would draw and display a 12-by-12 square, leaving the pen in the up position:
2
5,12
3
5,12
3
5,12
3
5,12
1
6
9
As the turtle moves with the pen down, set the appropriate elements of array floor to 1 s. When the
6 command (display the array) is given, wherever there's a 1 in the array, display an asterisk or any
character you choose. Wherever there's a 0 , display a blank.
Write an application to implement the turtle graphics capabilities discussed here. Write several
turtle graphics programs to draw interesting shapes. Add other commands to increase the power of
your turtle graphics language.
 
Search WWH ::




Custom Search