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

Hi, I need help with this computer science program.Thanks in advance! 3. Carpet

ID: 3671523 • Letter: H

Question

Hi, I need help with this computer science program.Thanks in advance!


3. 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 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 S960. (12 × 10 × 8-960.) First, you should create a class named RoonDinension that has two fields: one for the lengtlh of the room and one for the width. The RoomDinension 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 Rooncarpet class that has a RoomDimension object as a field. I 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 per square foot of the desired carpeting. The application should display the total cost of the carpet.

Explanation / Answer

Main Class:

import java.io.*;

import java.util.ArrayList;

import java.util.Scanner;

public class MainProgram {

    public static void main(String[] args) {

        final double CARPET_PRICE_PER_SQFT = 8.0;

        // Create a Scanner object for keyboard input.

        Scanner keyboard = new Scanner(System.in);

        // Display intro.

        System.out.println("This program will display the "

                + "carpet cost of a room." + " Please enter the room's "

                + "dimension in feet.");

        // 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();

        //close keyboard

        keyboard.close();

        ****// Create RoomDimension and RoomCarpet objects.

RoomDimension dimensions = new RoomDimension(length,

            width);

    RoomCarpet roomCarpet = new RoomCarpet(dimensions,

            CARPET_PRICE_PER_SQFT);

****

        // Print the object calling the toString

        System.out.println(roomCarpet);

    }

}

Room Dimension:

import java.io.*;

import java.util.ArrayList;

import java.util.Scanner;

public class RoomDimension {

    private double length;

    private double width;

    public RoomDimension(double length, double width) {

        super();

        this.length = length;

        this.width = width;

    }

    public double getLength() {

        return length;

    }

    public double getWidth() {

        return width;

    }

    public double getArea() {

        return length * width;

    }

    @Override

    public String toString() {

        return "RoomDimension [length=" + length + ", width=" + width + "]";

    }

}

Room Carpet:

import java.io.*;

import java.util.ArrayList;

import java.util.Scanner;

public class RoomCarpet {

      private RoomDimension roomDimensions;

        private double costOfCarpet;

        public RoomCarpet(RoomDimension roomDimensions, double costOfCarpet) {

            super();

            this.roomDimensions = roomDimensions;

            this.costOfCarpet = costOfCarpet;

        }

        public double getTotalCost() {

            return costOfCarpet * roomDimensions.getArea();

        }

        @Override

        public String toString() {

            return "RoomCarpet [roomDimensions=" + roomDimensions

                    + ", costOfCarpet=" + costOfCarpet + ", "

                            + "total cost=" + getTotalCost() + "]";

        }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote