JAVA: Carpet Calculator The Westfield Carpet Company has asked you to write an a
ID: 3825938 • Letter: J
Question
JAVA: 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.
You will write three java files:
1. RoomDimension.java: defines the RoomDimension class
2. RoomCarpet.java: defines the RoomCarpet class, which has a RoomDimension object as a data field
3. the test program
RoomCarpet -size: RoomDimension carpetCost: double +RoomCarpet dim: RoomDimension, cost: double): +RoomCarpetCrc: RoomCarpet to: double +getSize 0: RoomDimension tgetCarpetCost0: double +toString Strin RoomDimension -length: double -width: double +RoomDimension(den: double, wid: double) +RoomDimensi onard: Room Dimension +get Area0: double +toString StrinExplanation / Answer
Here is the RoomCarpet.java:
class RoomCarpet //extends RoomDimension
{
RoomDimension size;
double carpetCost;
public RoomCarpet(RoomDimension dim, double cost)
{
size = new RoomDimension(dim.getLength(), dim.getWidth());
carpetCost = cost;
}
public double getTotalCost()
{
return carpetCost;
}
public String toString()
{
String output = size + " Carpet cost: $" + carpetCost * size.getArea();
return output;
}
}
And RoomDimension.java:
class RoomDimension
{
double length;
double width;
public RoomDimension(double len, double w)
{
length = len;
width = w;
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
public double getArea()
{
return length * width;
}
public String toString()
{
String output;
output = "Room dimensions: Length: " + length + " Width: " + width + " Area: " + getArea();
return output;
}
}
CarpetCalculator.java:
import java.util.Scanner;
public class CarpetCalculator
{
public static void main(String[] args)
{
final double CARPET_PRICE = 8.0; // Price per square foot
double length; // To input room length
double width; // To input room width
RoomDimension dimensions; // To hold room dimensions
RoomCarpet room; // To determine cost
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Display intro.
System.out.println("This program will display price to " +
"carpet a room. You must input the " +
"room's dimensions in feet and inches.");
// Get the length of the room.
System.out.print("Enter the length first: ");
length = keyboard.nextDouble();
// Get the width of the room.
System.out.print("Now enter the width: ");
width = keyboard.nextDouble();
// Create RoomDimension and RoomCarpet objects.
dimensions = new RoomDimension(length, width);
room = new RoomCarpet(dimensions, CARPET_PRICE);
// Display the dimensions and cost.
System.out.println(room);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.