Java Hw Carpet calulator. Hi need help with hw assignment. create a class named
ID: 3555498 • Letter: J
Question
Java Hw Carpet calulator. Hi need help with hw assignment. 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 next two parts I have done. Just need this prgm. Here is what I have so far:
import java.util.Scanner;
/**
This program demonstrates a solution to the
Carpet Calculator programming challenge.
*/
public class RoomDemo
{
public static void main(String[] args)
{
final double CARPET_PRICE = 8.0; // Price per square foot
double length; // To input room length
double width; // To input room width
RoomDimension dimensions; // To hold room dimensions
RoomCarpet room; // To determine cost
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Display intro.
System.out.println("This program will display price to " +
"carpet a room. You must input the " +
"room's dimensions in feet.");
// Get the length of the room.
System.out.print("Enter the length first: ");
length = keyboard.nextDouble();
// Get the width of the room.
System.out.print("Now enter the width: ");
width = keyboard.nextDouble();
// Create RoomDimension and RoomCarpet objects.
dimensions = new RoomDimension(length, width);
room = new RoomCarpet(dimensions, CARPET_PRICE);
// Display the dimensions and cost.
System.out.println(room);
}
}
*****************************************AND******************************************************
import java.text.DecimalFormat;
public class RoomCarpet
{
private RoomDimension size;
private double carpetCost;
public RoomCarpet(RoomDimension dim, double cost)
{
size = new RoomDimension(dim.getLength(), dim.getWidth());
carpetCost = cost;
}
public double getTotalCost()
{
// Return the total cost.
return carpetCost * size.getArea();
}
public String toString()
{
DecimalFormat dollar = new DecimalFormat("#,##0.00");
String str = "Room dimensions: " +
size + " Carpet cost: $" +
dollar.format(getTotalCost());
return str;
}
}
*****************Thanks****************
Explanation / Answer
Assuming that the room is a rectangle shaped.
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 void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getArea() {
return length*width;
}
@Override
public String toString() {
return "length=" + length + ", width=" + width;
}
}
Also, Some suggestions in your code:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.