CS 151 Programming Assignment # 11 — Room Class

$25.00

Category:

Description

CS 151 Programming Assignment # 11 — Room Class
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 )
{
Room rm;
rm = new Room( 10.2, 16.1, 12.0 ); // create a new room
System.out.println( rm.toString() ); // display its variables
rm.setWallColor( “green”);
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

CS 151 Programming Assignment #12
Goal: Compute a histogram of letter grades entered from the keyboard.
Create a class named GradeHistogram that includes a method inputData () that reads a
series of letter grades (A, B, C, D, F) from the user and counts the number of times each grade
occurs. The grades are entered one per line. Enter a ‘Z’ to signal the end of input. The grades
can occur in any order. Accept both upper and lower case. For example ‘A’ and ‘a’ are the
same grade. Don’t assume any upper limit for the number of grades.
Count the number of times a letter that is not a grade has been entered. The method
showData() writes out the histogram.
Write the program with a class definition of GradeHistogram and a separate class definition
GHTester that contains the static main() method. The main() method creates a
GradeHistogram object and call that object’s inputData () method to read and process
the data.
Store the grade frequencies in an array of integers. Use a separate variable to contain the
number of errors. You must use an array for this program since the purpose is practice with
arrays.
Do not write this program with just a single class. Write a driver class HistoTester which
contains main(), which instantiates a GradeHistogram and uses its methods. Here is an
example of the program working:
Enter Grade: C
Enter Grade: A
Enter Grade: D
Enter Grade: c
Enter Grade: S
Enter Grade: a
Enter Grade: Z
Grade Frequency
—– ———
A 2
B 0
C 2
D 1
F 1
errors 1
Class Average: 2.6
Expect much more input than shown here.
You will need: A while loop that keeps going until the user inputs a Z. You may also need to
use the charAt() method of the String to get a single character from the user’s input.
Program Outline:
import java.util.*;
public class GradeHistogram
{
. . . instance variables . . .
public void inputData()
{
…fill in here…
}
public void showData()
{
…fill in here…
}
public double classAverage()
{
…fill in here…
}
}
// Driver class
public class HistoTester
{
public static void main ( String[] args )
{
GradeHistogram gradeHist = new GradeHistogram();
gradeHist.inputData();
gradeHist.showData();
. . .
}
}
What to turn in: Turn in GradeHistogram.java and HistoTester.java. At the top of your
source files include your name and date in the comment header.