207k views
1 vote
The ABC 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 wideis 120 square feet. To cover that floor with carpet that costs $8per square footwould cost $960. (12x10x8= 960)

User Azmath
by
4.3k points

1 Answer

0 votes

Answer:

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;

}

}

And the code for 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;

}

}

User Ratnesh Maurya
by
4.0k points