Question

In: Computer Science

JAVA - The Westfield Carpet Company has asked you to write an application that calculates the...

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.

Solutions

Expert Solution

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


Related Solutions

How can we write an application that calculates connascence of an application?
How can we write an application that calculates connascence of an application?
Suppose that the president of a carpet manufacturing firm has asked you to look into the...
Suppose that the president of a carpet manufacturing firm has asked you to look into the possibility of bypassing the firm's wholesalers (who sell to carpet, department, and furniture stores) and sell direct to these stores. What caution would you voice on this matter, and what type of information would you gather before making this decision?
Write a program in java which calculates the total price of an item you purchased which...
Write a program in java which calculates the total price of an item you purchased which includes sales tax. Create a Scanner object to read information from keyboard, Display prompts and get input, perform the calculation and display the Result of the program.
***IN C# ONLY, USING WINDOWS FORMS*** --NO JAVA--. Create a GUI application in C# that calculates...
***IN C# ONLY, USING WINDOWS FORMS*** --NO JAVA--. Create a GUI application in C# that calculates and displays the total travel expenses of a business person on a trip. Here is the information that the user must provide: • Number of days on the trip • Amount of airfare, if any • Amount of car rental fees, if any • Number of miles driven, if a private vehicle was used • Amount of parking fees, if any • Amount of...
You will write a Java Application program to perform the task of generating a calendar for...
You will write a Java Application program to perform the task of generating a calendar for the year 2020. You are required to modularize your code, i.e. break your code into different modules for different tasks in the calendar and use method calls to execute the different modules in your program. Your required to use arrays, ArrayList, methods, classes, inheritance, control structures like "if else", switch, compound expressions, etc. where applicable in your program. Your program should be interactive and...
JAVA Point of Sale System The McDowell Restaurant chain has asked you to write a menu...
JAVA Point of Sale System The McDowell Restaurant chain has asked you to write a menu program for their new Fast-food service machines. Your program already prints the following menu like this: ********************************************************************** McDowell’s Restaurant ********************************************************************** Make your selection from the menu below: 1. Regular Hamburger $1.50 2. Regular Cheeseburger $1.75 3. Fish Sandwich $2.50 4. Half-pounder with cheese $2.75 5. French Fries $0.99 6. Large Soft Drink $1.25 *********************************************************************** Select 1, 2, 3, 4, 5, or 6 ----- >...
IN JAVA Write a program that calculates the occupancy rate for each floor of a hotel....
IN JAVA Write a program that calculates the occupancy rate for each floor of a hotel. (Use a sentinel value and please point out the sentinel in bold.) The program should start by asking for the number of floors in the hotel. A loop should then iterate once for each floor. During each iteration, the loop should ask the user for the number of rooms on the floor and the number of them that are occupied. After all the iterations,...
Write a java program that calculates a speeding fine. The user is prompted for the speed...
Write a java program that calculates a speeding fine. The user is prompted for the speed of the vehicle, the posted speed limit and if the offence took place in the construction zone. The fine is calculated at $75 + 3$/ km over the speed limit for the first 20km over + $6 / km for the next 20 and $9/km after that. If the posted limit is 40km, the fine is doubled. If the offence took place in a...
You are asked to write a simple C++ phonebook application program. Here are the requirements for...
You are asked to write a simple C++ phonebook application program. Here are the requirements for the application. read the contact information from a given input file (phonebook.txt) into a dynamically created array of Contact objects. Each line of the input line includes name and phone information of a contact. Assume that each name has a single part Allow to perform operations on array of data such as search for a person, create a new contact or delete an existing...
You've been hired by Yogurt Yummies to write a C++ console application that calculates and displays...
You've been hired by Yogurt Yummies to write a C++ console application that calculates and displays the cost of a customer’s yogurt purchase. Use a validation loop to prompt for and get from the user the number of yogurts purchased in the range 1-9. Then use a validation loop to prompt for and get from the user the coupon discount in the range 0-20%. Calculate the following:         ● Subtotal using a cost of $3.50 per yogurt.         ● Subtotal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT