Download the L07.zip file and extract
the files to your cs125 folder in your home directory.
You will use these files to complete this week's lab exercises.
This lab covers objects. A quick overview is given here.
Often it is useful to create a new class whose instances make it easier to use instances of some previously-existing class. The Scanner class you have been using is an example of this--it makes it way easier to use the InputStream object System.in to read input from the keyboard.
Your are going to create a class called FancyBoard that's able to draw rectangles, as well as drawing lines and pegs, on a 2-D Board object. Here's the class diagram for FancyBoard.
FancyBoard |
|
|
Begin by creating a file named FancyBoard.java with the appropriate opening text "public class FancyBoard {" and closing text "}".
Add a declaration for the instance variable myBoard.
Add code for the FancyBoard constructor. It will need to create a Board object, and save its address in the instance variable myBoard.
Create a method definition in FancyBoard for each 2-d method in the Board class shown in the above class diagram. (They're all in black text.) These methods should simply call the corresponding board object's method, passing on the arguments supplied, and returning the value that the board method returns, if there is one. For example, the implementations of displayMessage(...) and getClick() would look like this:
public void displayMessage( String msg ) {
this.myBoard.displayMessage( msg );
}
public Coordinate getClick() {
return this.myBoard.getClick();
}
You can simply type this code into your FancyBoard.java file--it does just what you want. Then do something analogous to define drawLine(...), getColumns(), getRows(), putPeg(...), and removePeg().
Finally, add definitions for the new methods drawRectangle(...) and removeRectangle(...) that use the board object's drawLine(...) and removeLine(...) methods to draw and remove a rectangle from the board.
That's it! You can use the file UsingFancyBoard.java to test your implementation of FancyBoard. When you run it, you should first see the board that appears bottom-left; then click on the board, and the red peg, lines, and rectangle should be removed, resulting in the board you see bottom-right.
![]() |
![]() |
Submit the code for FancyBoard.java here.
You are given the Rectangle.class (i.e., the compiled version of the Rectangle.java file), which has the following class diagram:
|
|
|
It is worth noting that the pre and post conditions for setHeight are
// PRE: none // POST: sets this.height to be the provided value, if that value is >0 // otherwise, do not change the height
Also, the constructor ensures that all non-positive numbers are treated as 1 (since a Rectangle with negative width/height doesn't make sense). You can try the methods out if you wish to verify this.
Open the file WreckedRectangle.java.
Using what you know about
public and private, along with the Rectangle
class provided to you, make a modification to the
WreckedRectangle.java file such that the Rectangle has a
negative height.
You may only add one line of code and you may not use any of the methods in Rectangle.java (since they won't help you).
(Note: Rectangle.class has to be in the same directory as your WreckedRectangle.java)
Submit ALL the code for WreckedRectangle.java here.
If you request solutions for this lab you will be sent an email that contains your answers as well as the expected answers. The email will be sent out to all interested students once the usual deadline for completing the exercises has passed for all students.