Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

carpet calculator the westfield carpet company has asked you to write an applica

ID: 670596 • Letter: C

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 floor that is 12 feet long and 10 feet wide is 120 square feet. to cover that floor with carpet that costs $8 per square foot would cost $960. (12x10x8=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. the RoomDimension class should have a method that returns the area of the room. (The area of the room is the rooms length multiplied by the rooms width).

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 returns the total cost of the carpet.

Explanation / Answer

import java.util.*; // Use public access sepicifier before class keyword

class RoomDimension{

private double length, width;

RoomDimension(double length,double width){

this.length = length;

this.width = width;

}

public double getLength(){

return length;

}

public double getWidth(){

return width;

}

public double getArea(){

return length*width;

}

public String toString(){

return "RoomDimension [length = "+length+",width="+width+"]";

}

}

public class RoomCarpet{

private RoomDimension rd;

private double cost;

public RoomCarpet(RoomDimension rd, double cost){

super();

this.rd = rd;

this.cost = cost;

}

public double getTotalCost(){

return cost*rd.getArea();

}

public String toString(){

return "RoomCarpet [roomDimensions = "+rd+",costOfCarpet = "+cost+","+"total cost = "+getTotalCost()+"]";

}

}

public class MainClass{

public static void main(String args[]){

final double COST_SQRFT = 8.0;

Scanner sc = new Scanner(System.in);

System.out.println("This program will display the carpet cost of a room");

System.out.println("Enter the dimension in feet");

System.out.println("Enter the length of a room");

double length = sc.nextDouble();

System.out.println("Enter the width of a room");

double width = sc.nextDouble();

sc.close();

RoomDimension rd = new RoomDimension(length,width);

RoomCarpet rc=new RoomCarpet(rd,COST_SQRFT);

System.out.println(rc);

}

}