Java

OOP GUIDE / WORK

 

HOUSE CLASS

 

Topics

  • Instance variables
  • Instance methods
  • Constructors
  • Drawing objects with NOOPDraw

 

 

TASK – PART 1 – NOOPDRAW

 

Before getting started with OOP, make sure you are comfortable with the NOOPDraw class that provides easy graphics drawing functionality.  There is a lesson called “Static Graphics” in the Grade 11 course on this topic.

 

Remember that you need the NOOPDraw class in your current Java project to use NOOPDraw functionality.

 

 

TASK – PART 2 – HOUSE CLASS INSTANCE VARIABLES

 

Create a House class that will store required information for House objects to be drawn on the screen.  The shape of a house will simply be a filled rectangle and triangle (see image).  A house will also have a colour.

The house class will need the following instance variables.

 

  • int x – the x-coordinate of the top left corner of the rectangle.
  • int y – the y-coordinate of the top right corner of the rectangle.
  • int width – the width of the house and the roof
  • int height – the height of the rectangle
  • int roofHeight – the height of the triangle
  • int r – the amount of red in the house’s colour (0 to 255)
  • int g – the amount of green in the house’s colour (0 to 255)
  • int b – the amount of blue in the house’s colour (0 to 255)

 

TASK – PART 3 – HOUSE CLASS CONSTRUCTORS

 

Add the following constructors to the House class:

 

a) Constructor #1 - It has an argument for every instance variable (datafield).

b) Constructor #2 - It gets info for all instance variables (datafields) except colour. The colour is chosen randomly.

c) Constructor #3 - It has no arguments.  All aspects of the house are chosen randomly.  You can decide on adequate ranges for the random numbers.  Make sure we can fit many houses on a screen.

 

TASK – PART 4 – HOUSE CLASS METHODS

 

In this section, you will create a few simple methods.


a) Method #1-Create the method random255() that will return a number from 0 to 255.  It will be useful when generating random colours.  Because this method is not useful from outside the class, it should be private.

b) Method #2-Create the method getLeft() that returns the x-coordinate of the left side of the house.

c) Method #3-Create the method getRight() that returns the x-coordinate of the right side of the house.

d) Method #4-Create the method getTop() that returns the y-coordinate of the top of the house (peak).

e) Method #5-Create the method getBottom() that returns the y-coordinate of the bottom of the house.

 

 

TASK – PART 5 – HOUSE DRAW METHOD

 

a) In the main function of a class called Tester, create a few House objects.  Test each constructor and method.

b) Still in the main function, create a NOOPDraw window.

 

c) In the House class, create a draw() method.  It will draw the House object using NOOPDraw methods.

 

d) In the main function, use the draw() method on each house to draw them.