Question: Help Correct my code. The user is supposed to enter length, breadth, and price per square foot of a carpet . Then the output prints
Help Correct my code. The user is supposed to enter length, breadth, and price per square foot of a carpet . Then the output prints the total cost of the carpet. Total cost of carpet = (length*breadth)/ price per square foot. DON'T REMOVE ANY OF MY FUNCTIONS, JUST CORRECT THEM. All the functions I used were mandatory by the professor. CODE SHOULD BE IN JAVA.
I used this diagram as reference

Sample Output:
Enter length: 12
Enter breadth: 10
Enter price per square unit: $8.0
Area of carpet is 120.0
Total price: $960.0
My Code: import java.util.Scanner;
class RoomDimension { private double length; private double width;
public RoomDimension(double len, double w) { this.length=len; this.width=w; } public double getArea() { return length*width; } public void setLength(double len) { this.length=len; } public double getLength(){ return length; } public void setWidth(double w) { this.width=w; } public double getWidth(){ return width; } } class RoomCarpet { private RoomDimension size; private double carpetCost; double area; public RoomCarpet(RoomDimension dim, double cost) { this.size=dim; this.carpetCost=cost; } public double getTotalCost() { return roomDimension.getArea()*carpetCost; } public String toString () { String output; output = "Room dimensions: Length: " + length + " Width: " + width + " Area: " + getArea (); return output; } }
public class Main { public static void main(String[] args) { double length,width,price; Scanner input=new Scanner(System.in); System.out.print("Enter length of floor: "); length=input.nextDouble(); System.out.print("Enter width of floor: "); width=input.nextDouble(); System.out.print("Enter price per unit square : "); price=input.nextDouble(); RoomDimension roomDim= new RoomDimension(length, width); RoomCarpet roomCarp= new RoomCarpet(roomDim, price);
System.out.println(roomCarp.toString()); //to print area double totalCost=roomCarp.getTotalCost(); System.out.println("The total cost of carpet: $" +totalCost); } }
REMINDER: Code should be in Java
RoomCarpet |- size : Room Dimension |- carpetCost : double + RoomCarpet(dim : RoomDimension, cost : double) + get TotalCost(): double + toString(): String Room Dimension - length : double - width : double + Room Dimension(len: double, w: double) + getArea(): double + toString(): String
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
