Room Class....... in JAVA!!!!! Write a class named Room that has the following f
ID: 3685159 • Letter: R
Question
Room Class....... in JAVA!!!!!
Write a class named Room that has the following fields:
Length: Double variable that holds the rooms length(in feet). Breadth: The double variable that holds the rooms breadth(in feet). Height: Double variable that holds rooms height in feet. Color: String object that holds the color you want the room to be painted with.
The class should have a constructor to initialize the fields for a particular instance (or object) of the class as given: All double fields are assigned 0.0. The color field is assigned "white".
Write appropriate mutator methods to store values in these fields and accessor methods that return the values in these fields. Also, it should have the following methods:
getArea(): that calculates and returns the total surface area of the room.
getCost(): that takes the above calculated area and per square feet price of painting(10$) as parameters to calculate and return the total cost of painting the room. Once you have written the class, write a seperate program that asks the user for the above 4 values. It should display following on the terminal screen:
______ ft X _______ ft X ________ ft (order: length X width X height)
Color: _______
Cost of painting: $__________
Explanation / Answer
public class Room { // save as "Room.java"
// private instance variable, not accessible from outside this class
private double length;
private double breadth;
private double height;
private String color;
// 1st constructor
public Room() {
length = 0.0;
breadth = 0.0;
height = 0.0;
color = "white";
}
// 2nd constructor with given values
public Room(double length, double breadth, double height, String color) {
this.length = length;
this.breadth = breadth;
this.height = height;
this.color = color;
}
public void setLength( double length)
{
this.length = length;
}
public void setBreadth ( double breadth)
{
this.breadth = breadth;
}
public void setHeight( double height)
{
this.height = height;
}
public void setColor( String color)
{
this.length = length;
}
public double getLength()
{
return length;
}
public double getBreadth()
{
return breadth;
}
public double getHeight()
{
return height;
}
public String getColor()
{
return color;
}
// A public method for retrieving the area
public double getArea() {
return length*breadth*height;
}
// A public method for computing the area of Room
public double getCost(double area) {
return area*10;
}
public static void main(String[] args) {
// Declare and allocate an instance of class Room called r1
// with default length, breadth , height and color
Room r1 = new Room();
// Use the dot operator to invoke methods of instance r1.
System.out.println(r1.getLength()+" ftX "+ r1.getBreadth()+" ftX " + r1.getHeight()+" ft");
System.out.println("Color: " + r1.getColor());
double area = r1.getArea();
System.out.println("Cost of painting: $" + r1.getCost(area) );
System.out.println();
// Declare and allocate an instance of class Room called r2
// with the given length, breadth , height and color
Room r2 = new Room(1.0,2.0,3.0,"red");
// Use the dot operator to invoke methods of instance r2.
System.out.println(r2.getLength()+" ftX "+ r2.getBreadth()+" ftX " + r2.getHeight()+" ft");
System.out.println("Color: " + r2.getColor());
area = r2.getArea();
System.out.println("Cost of painting: $" + r2.getCost(area) );
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.