PHW2: Square Class

So far we have created a Circle class. Next we’ll create a Square class.

Rx: <10 min Av: 10-20 min Sd: 20-30 min DNF: 31+ min

Instructions

Part I: Create a Square class

  1. Start your timer
  2. If you haven’t been following along in class, download the Shapes Project and import it into your workspace.
  3. Right-click on your Shapes project and go to New > Class to create a Square class. Go ahead and check the box for Eclipse to create a main method.
  4. Add 4 fields to this class: x, y, width, and color. Integers are recommended.
  5. Add 2 constructors:
    • Default constructor (no parameters)
    • Constructor with 4 parameters (one for each field)
  6. Add get & set methods for all the fields
  7. Add a getArea() and a getPerimeter() method & test them in main by creating a Square variable & initializing it by calling one of the constructors.

Part II: Draw a Square in Picture

  1. To your square class, add a paint method that draws the square like we did for Circle.
  2. Add a Square field to your Picture class to test it. Make sure to initialize your Square field in the constructor.
  3. Run the program to verify your changes.
  4. Export your program by right-clicking on your project folder, and selecting “Export > General > Archive File”. Name the file Square_uLogin.zip.
  5. Stop your timer

Demonstration

Once you’ve finished doing the HW a single time, watch me do it:

Standard Practice HW Advice

You’ll learn significantly less from watching me solve the practice HW if you haven’t attempted the HW yourself first.

While it’s an achievement to finish the HW no matter how long it takes, you might experience “diminishing returns” if you work longer than the DNF time. Thus, it might be strategic to stop working at the DNF time and watch my solution.

After watching my solution, I recommend that you repeat the HW if you have not achieved at least Av performance. If so, be sure to:

Feel free to keep trying until you make Rx if that’s of interest to you.

My Final Project

Shapes_phw3.zip