Tino APCS

Lab 2.1 Final House Checkpoint

For SecondHouse, you implemented functional programming by writing independently runnable methods to draw parts. What would someone have to do if they wanted to include your Window in their house? What's a better way? In this lab, you will learn to create classes that can be used by yourself or others in multiple projects.

Object Oriented Programming

Using object oriented programming, you can create classes that can be used in multiple projects without having to copy paste methods. For example, you have been using the DrawingTool and SketchPad classes.

If you wanted to create a custom Window class you could do the following:

  1. Move your window code to a Window class
  2. Add attributes, constructors, methods
  3. Use the new Window blueprint to create Window objects

Drawing separate objects on a single SketchPad

There are multiple ways to draw on the same sketchpad, but what it comes down to is that every DrawingTool used must reference the same sketchpad. With that in mind, here is a strategy for making multiple classes all draw on a single SketchPad:

  1. Accept a SketchPad object in your Constructors
  2. Each of your classes should declare a DrawingTool instance variable
  3. Instead of creating a new SketchPad in your separate classes, pass on the SketchPad parameter to the DrawingTool when you initialize the DrawingTool.
  4. The SketchPad is created at the beginning of the main method in a separate driver class and is passed in to the constructor of each house part.
  5. In the HouseDriver that draws your whole house, the SketchPad can be a static field declared at the top of your class and initialized in the main method just as you did in Lab 1.2 SecondHouse, since you will still have some parts of your house drawn with static helper methods in the driver.
  6. Later, once all the house parts are drawn by other classes, you can make the SketchPad a local variable declared and initialized inside the main method because you won't need any other methods in the driver that need access to the SketchPad.

Workflow

  1. Pick one of your house parts and create a separate class for that house part. Each class should be named in the format PX_LastName_FirstName_PartName. For example, if Karel Barker was in 2nd period and created a Window class, they would name it P2_Barker_Karel_Window.
  2. At first, only add attributes for x-position, y-position and DrawingTool
  3. Add a Constructor that takes a SketchPad and x, y position and initializes the x, y attributes and the DrawingTool
  4. Make sure to initialize the DrawingTool to use the SketchPad passed in to the part's constructor.
  5. Add a draw() method that draws your house part relative to the x, y position. Think carefully about and document which part of your house part will be located at (x, y). For example, if your part is a window, is (x, y) the center of the window? The upper left corner? bottom left corner?...etc
  6. You may have to update your code to get rid of any hardcoding of positions
  7. Everything else like size and Color can stay hard-coded for now
  8. Update your main method to create and use your house part. Test it for different x, y positions.
  9. Next, add simple attributes like color
  10. Add a custom Constructor that receives the same info as the previous constructor but also the new attributes you added.
  11. Update your main method to use the custom constructor. Test it for different colors, etc.
  12. Add scalability
  13. The user of this class should be able to customize size of this object. You can let users choose both width and height or just width and have the height depend on the width, etc
  14. Update your custom Constructor to also receive a width and height (or just one of these) so that users can choose the size.
  15. You should draw everything based on the location x, y, and scale width, height attributes.
  16. To test your code, you should make a separate TestDriver that just creates instances of your classes and draws them, rather than drawing your entire house picture.
  17. Create different instances of this part with different positions and sizes. Most of you will need to update your code to work with scaling. No hard-coding!
  18. Add other customizable properties using setter methods such as number of flower petals, whether a door is open/closed, number of puffs in a cloud, etc.

Final House Checkpoint Requirements

  1. Create separate classes for at least 2 of your house parts.
  2. At least one of your classes needs to be a complex part.
  3. Create a separate HouseDriver class with a main method that creates instances of your classes and draws the house. The parts of your house represented by the classes you created will be drawn using instances of those classes, but the rest of your house will still be drawn in the main method and in helper methods. Since you will still have some parts of your house drawn static methods, the driver can still have a static SketchPad, but that same SketchPad should be passed to the constructor of each house part class.
  4. Each class should have its own attributes that allow it to be customized to different locations, sizes, colors...etc
  5. Each class should have at least two constructors
  6. One constructor accepts only a SketchPad and an (x, y) location defining where to draw this object. All other attributes should be initialized to reasonable defaults.
  7. The other constructor accepts additional parameters to customize the look, size, shape, etc (Note: Don't make your parameter list too long. Leave some things for setter methods)
  8. Each class should include setter methods to set attributes not handled by the constructors
  9. Add Javadoc comments to your house part classes
  10. Update your main method to create and use your house part objects.

You must Sign In to submit to this assignment

Last modified: August 29, 2023

Back to Summary

Dark Mode

Outline