Question

Room Class....... in JAVA!!!!! Write a class named Room that has the following fields: Length: Double...

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: $__________

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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) );

}
}


answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
Room Class....... in JAVA!!!!! Write a class named Room that has the following fields: Length: Double...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class named Room that has two fields one for the room’s length and one...

    Write a class named Room that has two fields one for the room’s length and one for the width. The class should have a constructor that sets the length and the width and a method named “getArea” that returns the room’s area (area = ½ length x width). It should also have a method named “isEqual” (with a Room argument) that determines if the two rooms are equal or not. (25 points). Do not write the demo to test this...

  • need help in java , on jgrasp Write a class named Room that has two fields...

    need help in java , on jgrasp Write a class named Room that has two fields one for the room's length and one for the idth The class should have a constructor that sets the length and the width anda method named "getArea" that returns the room's area (area = length x width). should also have a method named "isEqual" (with a Room argument) that determines the two rooms are equal or not. (25 points). Do not write the demo...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • java Write a class named Car that has the following fields: • yearModel: The yearModel field...

    java Write a class named Car that has the following fields: • yearModel: The yearModel field is an int that holds the car's year model. • make: The make field is a String object that holds the make of the car. • speed: The speed field is an int that holds the car's current speed. In addition, the class should have the following methods: • Constructor: The constructor should accept the car's year model and make as arguments. These values...

  • Java Program Write a class named Car that has the following fields: yearModel- The yearModel field...

    Java Program Write a class named Car that has the following fields: yearModel- The yearModel field is an int that holds the car’s year model. make- The make field is a String object that holds the make of the car, such as “Ford”, “Chevrolet”, etc. speed- This speed field is an int that holds the car’s current speed. In addition, the class should have the following methods: Constructor- The constructor should accept the car’s year model and make as arguments....

  • Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you...

    Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you are expected to implement in your Shape class; - A private String color that specifies the color of the shape - A private boolean filled that specifies whether the shape is filled - A private date (java.util.date) field dateCreated that specifies the date the shape was created Your Shape class will provide the following constructors; - A two-arg constructor that creates a shape with...

  • Create a class Circle with one instance variable of type double called radius. Then define an...

    Create a class Circle with one instance variable of type double called radius. Then define an appropriate constructor that takes an initial value for the radius, get and set methods for the radius, and methods getArea and getPerimeter. Create a class RightTriangle with three instance variables of type double called base, height, and hypotenuse. Then define an appropriate constructor that takes initial values for the base and height and calculates the hypotenuse, a single set method which takes new values...

  • This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets...

    This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...

  • 2. Create a class named Student that contains the following » idStudent. The idStudent is an...

    2. Create a class named Student that contains the following » idStudent. The idStudent is an int variable that holds the student's matric number. name. The name field references a String object holds the student's name. . major. The major field references a String object holds the student's major. . classification. The classification field references a String object holds the student's classification level (bongsu, kecil, muda, sulung) . Constructor should accept the student's matric number, name, major and classification as...

  • Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields...

    Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT