Java Reference
In-Depth Information
Exercise P7.13. Implement the following algorithm to construct magic
n-by-n
2
squares; it works only if n is odd. Place a 1 in the middle of the
bottom row. After k has been
placed in the (i, j) square, place k+1 into the
square to the right and down
, wrapping around the borders. However, if the
square to the right and down has already been filled, or if you are in the
lower-right corner, then you must move to the square straight up instead.
Here is the 5¶5 square that you get if you follow this method:
329
330
11
18
25
2
9
10
12
19
21
3
4
6
13
20
22
23
5
7
14
16
17
24
1
8
15
Write a program whose input is the number n and whose output is the
magic square of order n if n is odd. Implement a class
MagicSquare
with a constructor that constructs the square and a
toString
method that
returns a representation of the square.
G Exercise P7.14. Implement a class Cloud that contains an array list of
Point
2D.Double
objects. Support methods
public void add(Point2D.Double aPoint)
public void draw(Graphics2D g2)
Draw each point as a tiny circle.
Write a graphical application that draws a cloud of 100 random points.
G Exercise P7.15. Implement a class
Polygon
that contains an array list
of
Point2D.Double objects.
Support methods
public void add(Point2D.Double aPoint)
public void draw(Graphics2D g2)
Draw the polygon by joining adjacent points with a line, and then closing
it up by joining the end and start points.