P19. (PuzzlePiece) Write a class called PuzzlePiece.
A puzzle piece will have a label which will be a number
between 1 and 15 and the location of the puzzle piece (which
will be the coordinates of the upper left corner).
Your class should contain three
constructors:
- a default constructor that sets the
coordinates to (0,0) and the label to 0,
- a constructor that has a single argument
that is assigned to label and sets the
coordinates to (0,0), and
- a constructor that takes three arguments:
the label, the x-coordinate, and the y-coordinate
(in that order) and sets the values.
PuzzlePiece should contain a move method:
public void move(int dx, int dy)
that changes the position by dx units in the x
direction and dy units in the y direction.
Finally, you should have a draw method:
public void draw()
that draws a square containing the label, using
the object's coordinates for the upper left corner.
Write a second class called Program19 that uses
PuzzlePiece and demonstrates its features.
Program19 should construct two PuzzlePieces (one
using the default constructor and one using the
other constructor), it should draw the puzzle pieces,
and it should move at least one of the pieces.
Submit this as program 19
using the electronic submission. Please call the
file (and the class) Program19.
Include an HTML file in the directory called Program19.html
that displays your applet.
P20. (Connect-the-Dots) Write a applet in
plain java that draws a dot when the user clicks
the mouse and connects that dot to the previous one.
Your applet should have a clear button that clears what
has been drawn. For example:
Hints:
- Modify OneColorDraw.java from plain
java notes.
- There are two different cases for drawing.
The first is when there is no previous point to connect
the new point (this occurs when the applet first starts
and after the
clear button has been clicked). The second case is when
you do have a previous point and you draw a line between
the two points.
- You can draw a "dot" by drawing a "filled rectangle"
of small width and height.
Submit this as program 20
using the electronic submission. Please call the
file (and the class) Program20.
Include an HTML file in the directory called Program20.html
that displays your applet.
P21. (Sorted Merge) P9.6, p 416. Your program
should (in the following order):
- Ask for the length of the first sorted list of integers.
- Read in the first list of integers.
- Ask for the length of the second sorted list of integers.
- Read in the second list of integers.
You may assume that the lists are sorted, and you do not
have to error check for this.
Your program should then print out the sorted list of
numbers.
Submit this as program 21
using the electronic submission. Please call the
file (and the class) Program21.
P22. (Permutations) P9.13, p 418.
Submit this as program 22 using the electronic submission.
Please call the file (and the class) Program22.