Program in Java. You have been hired to write a program that will determine the
ID: 3564831 • Letter: P
Question
Program in Java.
You have been hired to write a program that will determine the area of rooms in a house. The user will be prompted to enter the length and width of the bedroom, living room, kitchen, and bathroom. The program will calculate the area for each of theses rooms and show the total area of the rooms.
1.Declare a class called rectangle that has two private variables - length and width.
2. Declare a class called house that: creates four instances of the rectangle class, prompts user for the length and width of each room, and displays the total area of the rooms.
Explanation / Answer
import java.util.Scanner;
class rectangle{
private double length=0,width=0;
public rectangle(){
}
public rectangle(double length, double width){
this.length = length;
this.width = width;
}
public void setLength(double length){
this.length = length;
}
public void setWidth(double width){
this.width = width;
}
public double getLength(){
return length;
}
public double getWidth(){
return width;
}
public double getArea(){
return width*length;
}
}
class House {
public static void main(String... args){
rectangle bedroom = getInput("bedroom");
System.out.println("Area: " + bedroom.getArea());
rectangle living_room = getInput("living room");
System.out.println("Area: " + living_room.getArea());
rectangle kitchen = getInput("kitchen");
System.out.println("Area: " + kitchen.getArea());
rectangle bathroom = getInput("bathroom");
System.out.println("Area: " + bathroom.getArea());
double total_area = bedroom.getArea() + living_room.getArea() + kitchen.getArea() + bathroom.getArea();
System.out.println("Total Area = " + total_area);
}
public static rectangle getInput(String msg){
Scanner scr = new Scanner(System.in);
System.out.println("Enter length and width of the "+msg);
return new rectangle(scr.nextDouble(),scr.nextDouble());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.