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

JAVA - The Westfield Carpet Company has asked you to write an application that c

ID: 3800317 • Letter: J

Question

JAVA -

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. (12 X 10 X 8 = 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 room's length multiplied by the room's 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.

Figure 8-21 is a UML diagram that shows possible class designs and the relationships among the classes. Once you have written these classes, use them in an application that asks the user to enter the dimensions of a room and the price epr square foot of the desired carpeting. The application should display the total cost of the carpet.

Explanation / Answer

RoomDimension.java

public class RoomDimension {
   // Declaring instance variables
   private double length;
   private double width;

   // Parameterized constructor
   public RoomDimension(double length, double width) {
       super();
       this.length = length;
       this.width = width;
   }

   // getters
   public double getLength() {
       return length;
   }

   public double getWidth() {
       return width;
   }

   // Calculate the area of the room
   public double areaOfRoom() {
       return getLength() * getWidth();

   }

}

____________________

RoomCarpet.java

public class RoomCarpet {
   // Declaring instance variable
   private double cost_of_carpet;

   // Creating RoomDimension class type variable
   RoomDimension roomdimension;

   // parameterized constructor
   public RoomCarpet(double cost_of_carpet, RoomDimension roomdimension) {
       super();
       this.cost_of_carpet = cost_of_carpet;
       this.roomdimension = roomdimension;
   }

   // Getter method
   public double getCost_of_carpet() {
       return cost_of_carpet;
   }

   // Method which calculate the total cost of carpet
   public double totalCostOfCarpet() {
       return getCost_of_carpet() * roomdimension.areaOfRoom();
   }

}

_____________________

DriverClass.java

import java.util.Scanner;

public class DriverClass {

   public static void main(String[] args) {
       //Declaring variables
       double length,width,carpetCostPerFt,total_carpet_cost;

       //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);

       //Getting the length of the carpet
       System.out.print("Enter the Length of the Carpet (in feet):");
       length=sc.nextDouble();
      
       //Getting the width of the carpet  
       System.out.print("Enter the Width of the Carpet (in feet):");
       width=sc.nextDouble();
      
       //Getting the cost of the carpet per feet
       System.out.print("Enter cost of Carpet (per sq ft) :$");
       carpetCostPerFt=sc.nextDouble();
      
       /* Creating the Room Dimension class object
       * by passing the length and width as arguments
       */
       RoomDimension dimension=new RoomDimension(length, width);
      
       //Creating the Room Carpet class object
       RoomCarpet roomcarpet=new RoomCarpet(carpetCostPerFt, dimension);
      
       //Calling the method which calculates the total cost of the carpet
       total_carpet_cost=roomcarpet.totalCostOfCarpet();
      
       //Displaying the total cost of the carpet
       System.out.println("Total of cost of carpet is :$"+total_carpet_cost);
      
      
   }

}

_______________________

output:

Enter the Length of the Carpet (in feet):12
Enter the Width of the Carpet (in feet):10
Enter cost of Carpet (per sq ft) :$8
Total of cost of carpet is :$960.0

_____________Thank YOu