Programming Assignment # 11 — Room Class Solution
Design a Java class that represents a room of a house. Assume that a room is a rectangular box without doors or windows, so that it is described with three doubles: the width, depth, and height of the room. A room has a carpeted floor and painted walls and ceiling. Room objects have instance variables that hold
the carpet color and the color of the walls and ceiling. The ceiling and walls are the same color, but the carpet has its own color. Make all instance variables private.
The constructor sets the width, depth, and height of the room. These values never change, so there are no setters for them. The constructor should also initialize all colors to “white”. Room objects have methods that change the colors and retrieve the colors:
public void setCarpetColor( String color ) public String getCarpetColor()
public void setWallColor( String color ) public String getWallColor()
The instance variables for the colors hold String references (don’t use Java class Color, which is useful for graphics but not needed here). Also, implement the following methods:
public double getTotalArea() // the total area of the room
public double getWallArea() // the area of just the walls and ceiling
public double getFloorArea() // the area of the floor.
public String toString() // a String that describes the room
Now write a method that computes the gallons of paint needed for the room given the coverage of the paint in square feet covered per gallon of paint:
public double calcGallons(double squareFeetPerGallon)
If you use BlueJ you can test your class by instantiating an object and running its methods from the BlueJ interface. If you want to use the command line, write a separate testing class that consists only of a main() method. The main() method tests the class by doing something like this:
public static void main ( String args )
rm = new Room( 10.2, 16.1, 12.0 ); // create a new room
System.out.println( rm.toString() ); // display its variables
rm.setCarpetColor( “brown” );
System.out.println( rm.toString() );
System.out.println(“Total area: “ + rm.totalArea() );
System.out.println(“Wall and ceiling area: “ + rm.wallArea() );
System.out.println(“Floor area: “ + rm.floorArea() );
System.out.println(“Paint needed: “ + rm.calcGallons( 120 ) );
Of course, you will do more debugging than this. Add or remove lines of code from your main()
method depending on what aspects you wish to test.
Turn In: Your source file for the Room class, Room.java
You'll get a 3.0KB .ZIP file.