Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

What is carpet calculator java?

Asked by Amit Raj Oct 13, 2022 1.2K views 1 answer
Share

About this question

This program is supposed to have 3 parts to it. 2 classes and 1 test program which I have created. My code for the program runs and works fine. However, I based my program off a different UML diagram. I had to go back and add more modules based on the UML diagram that my professor gave the class. My program still runs fine although I am sceptical about the addition of those methods which I feel I do not need in my program but I have to include them. I want to know if I created them properly. The instructions only covered the basic modules. For the extra modules the professor wanted, there wasn't a clear reason why they were needed and what they were supposed to do which threw me off. Usually, the modules return something or are there to hold something. The ones I am unsure about are the ones marked with yellow lines next to them.


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.


public class Room_Dimension 
    {
        private double length;
        private double width;
        public Room_Dimension(double length, double width) 
        {
            this.length = length;
            this.width = width;
        }
        public double getLength() 
        {
            return length;
        }
        public double getWidth() 
        {
            return width;
        }
        public Room_Dimension(Room_Dimension roomDimensions)
        {
        }
        public double getArea()
{
            return length * width;
        }
        public String toString() 
        {
            return "Dimensions of room: (length = " + length + ", width = " + width + ")";
        }
    }
    public class Room_Carpet 
    {
        private Room_Dimension roomDimensions;
        private double carpetCost;
        public Room_Carpet(Room_Dimension roomDimensions, double carpetCost) 
        {
            this.roomDimensions = roomDimensions;
            this.carpetCost = carpetCost;
        }
        public Room_Carpet(Room_Carpet roomCarpet)
        {
        }
        public double getTotalCost() 
        {
            return carpetCost * roomDimensions.getArea();
        }
        public Room_Dimension getSize()
        {
            return roomDimensions;
        }
        public double getCarpetCost()
        {
            return carpetCost;
        }
        public String toString() 
        {
            return "" + roomDimensions + ", Carpet cost per square feet = $" + carpetCost + ", " + "Total cost = $" + getTotalCost() + "";
        }
    }
    import java.util.Scanner; 
    public class Carpet_Calculator 
    {
        public static void main(String[] args) 
        {
            // Create a Scanner object for keyboard input.
            Scanner keyboard = new Scanner(System.in);
            // Display information about the program. 
            System.out.println("This program will display the carpet cost of a room.");
            // Get the length of the room.
            System.out.print("Enter the length of room: ");
            double length = keyboard.nextDouble();
            // Get the width of the room.
            System.out.print("Enter the width of room: ");
            double width = keyboard.nextDouble();
            // Get the cost of price per square feet of the desired carpeting.
            System.out.print("Enter the price per square feet for the desired carpeting: ");
            double carpetCost = keyboard.nextDouble();
keyboard
.close();
// Create Room_Dimension and Room_Carpet objects.
            Room_Dimension dimensions = new Room_Dimension(length, width);
            Room_Carpet roomCarpet = new Room_Carpet(dimensions, carpetCost);
            // Print the object calling the toString
            System.out.println(roomCarpet);
        }
    }

Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.