Homework 7 for Computer Science 125

Introduction to Computer Science

Boise State University, Spring 1999


This homework is due by 9 p.m, Friday, 9 April 1999. See the directions in Homework 1 to electronically submit homework.

Warm-up and Practice Problems

These are not be turned in. They are to help you understand the material, and some will appear on exams.

Graded Problems

You must comment your code (see book for examples).
You must include comments for all functions, following the javadoc format. See CCJ, p 183-185 for more details.

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:

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:

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):
  1. Ask for the length of the first sorted list of integers.
  2. Read in the first list of integers.
  3. Ask for the length of the second sorted list of integers.
  4. 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.