JAVA Question: Carpet Calculator The Westfield Carpet Company has asked you to w
ID: 3573259 • Letter: J
Question
JAVA
Question: Carpet Calculator
The Westfield Carpet Company has asked you to write an application that calculates the price of carpeting for rectangular rooms. To calculate the price, you multiply the area of the floor (width times length) by the price per square foot of carpet. For example, the area of the floor that is 12 feet long and 10 feet wide is 120 square foot. To cover that floor with carpet that costs $8 per square foot would cost $960.
First you should create a class named RoomDimension that has two fields: one for the length of the room and one for the width of the room. The RoomDimension class should have a method that returns the area of the room.
Next you should create a RoomCarpet class that has a RoomDimension object as a field. It should also have a field for the cost of the carpet per square foot. The RoomCarpet class should have a method that calculates and returns the cost of the carpet.
Once you have the two classes, write a test program to ask the user to enter the dimensions of a room and the price per square foot of the desired carpeting. Then display the total cost of the carpet.
RoomCarpet -size: RoomDimension -carpetCost: double +RoomCarpet (dim: RoomDimension, cost: double): +RoomCarpet(rc: RoomCarpet) +getTotalCost0: double +getSize(): RoomDimension. getCarpetCost0: double +toString0:String RoomDimension -length: double width double +RoomDimension(len: double, wid: double) RoomDimension(rd: RoomDimension) +getArea(): double oString(): StringExplanation / Answer
public class RoomDimension {
private double length;
private double width;
/**
* @param length
* @param width
*/
public RoomDimension(double length, double width) {
this.length = length;
this.width = width;
}
/**
* @param rd
*/
public RoomDimension(RoomDimension rd) {
this.length = rd.length;
this.width = rd.width;
}
public double getArea() {
return (length * width);
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "RoomDimension [length=" + length + ", width=" + width
+ ", getArea()=" + getArea() + "]";
}
}
public class RoomCarpet {
private RoomDimension size;
private double carpetCost;
/**
* @param size
* @param carpetCost
*/
public RoomCarpet(RoomDimension size, double carpetCost) {
this.size = size;
this.carpetCost = carpetCost;
}
/**
* @param rc
*/
public RoomCarpet(RoomCarpet rc) {
this.size = rc.size;
this.carpetCost = rc.carpetCost;
}
public double getTotalCost() {
return (size.getArea() * carpetCost);
}
/**
* @return the size
*/
public RoomDimension getSize() {
return size;
}
/**
* @return the carpetCost
*/
public double getCarpetCost() {
return carpetCost;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "RoomCarpet [getTotalCost()=" + getTotalCost() + ", getSize()="
+ getSize() + ", getCarpetCost()=" + getCarpetCost() + "]";
}
}
import java.util.Scanner;
public class TestRoomCarpet {
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
System.out.print("Enter room length:");
double length = scanner.nextDouble();
System.out.print("Enter room width:");
double width = scanner.nextDouble();
System.out.print("Enter room Cost :");
double cost = scanner.nextDouble();
RoomDimension dimension = new RoomDimension(length, width);
RoomCarpet carpet = new RoomCarpet(dimension, cost);
System.out.println(carpet);
} catch (Exception e) {
// TODO: handle exception
} finally {
if (scanner != null)
scanner.close();
}
}
}
OUTPUT:
Enter room length:10
Enter room width:12
Enter room Cost :8
RoomCarpet [getTotalCost()=960.0, getSize()=RoomDimension [length=10.0, width=12.0, getArea()=120.0], getCarpetCost()=8.0]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.