Tino APCS

LAB 4.3 Rectangle

Background:

  1. It is always important to have a well-designed class before writing down any code. Having a class laid out on paper before writing the code allows programmers to see any design flaws before they have coded those flaws into their classes. Determining which classes are needed, what data those classes hold, and how those classes behave are the main objectives of OOP.

  2. The specifications of a class that models a rectangular shape would be:

Assignment:

  1. Implement a Rectangle class with the following properties.

    1. A Rectangle object is specified in the constructor with the left and right edges of the rectangle at x and x + width. The bottom and top edges are at y and y + height. You should draw this out on paper to confirm you understand which part of your rectangle is at (x, y).

    2. Initialize the 500 x 500 Sketchpad and the DrawingTool in the constructor.

    3. A method getPerimeter calculates and returns the perimeter of the Rectangle.

    4. A method getArea calculates and returns the area of the Rectangle.

    5. A method draw displays a new instance of a Rectangle object. Refer to the DrawingTool API for details on DrawingTool methods.

  2. Try your rectangle with both the default constructor and with a constructor that can take the x and y coordinates, the length of the rectangle, and the width. Here are some sample constructor calls:

Rectangle rectA = new Rectangle();
Rectangle rectB = new Rectangle(0, -80, 400, 160);
Rectangle rectC = new Rectangle(100, -100, 20, 300);

Extension (Optional)

  1. If you finished the lab, you already noticed that each Rectangle object draws in its own SketchPad window. That's because each Rectangle object has its own copy of a SketchPad and DrawingTool stored in its attributes. It's possible, however, to get all three Rectangle objects to share the same SketchPad. Here's how:

    1. Declare the SketchPad and DrawingTool attributes as static:

      private static DrawingTool pen 
      private static SketchPad paper
      
    2. Declaring an attribute as static makes all objects of the given type share the same copy of that attribute. Thus, in the case of having a static DrawingTool and SketchPad, all Rectangle objects use the exact same DrawingTool and SketchPad.

    3. One final detail you'll notice is that although all the drawing is done in a single SketchPad, three SketchPad windows are still created. We'll go over why this is during class... if you really want to get around it then the only solution is to create a SketchPad in the driver and pass it in as an argument to your Rectangle's constructor.

Submission Form

  1. Name your Rectangle class and Constructors properly. For example: P3_Wang_Michael_Rectangle.java
  2. Upload your Rectangle class using the form below. No driver is needed (even though you should have one that you used to test your code).

You must Sign In to submit to this assignment

Last modified: December 12, 2022

Back to Lab 4.2 Mpg

Dark Mode

Outline