The following figure shows a UML diagram that aggregates the RoomCarpet class fr
ID: 3788846 • Letter: T
Question
The following figure shows a UML diagram that aggregates the RoomCarpet class from the RoomDimension class.
*I am using JGrasp so please make sure it is compatible
*Please comment throughout code
*FULL QUESTION IN IMAGES BELOW
Exercise 1: The following figure shows a UML diagram that aggregates the RoomCarpet class from the Room Dimension class. Room Carpet room: RoomDimension carpetCost: double total Cost: double Room Carpet (RoomDimension r, double cost) Cost double getTotal tostring String costs More (RoomCarpet other) boolean Room Dimension length double width: double Room Dimension (double l, double w) getLength double getWidth double toString StringExplanation / Answer
//RoomDimension.java
public class RoomDimension {
private double length;
private double width;
//Constructor that takes lengha and width
public RoomDimension(double l,double w) {
length=l;
width=w;
}
//Returns length of room
public double getLength(){
return length;
}
//Returns width of room
public double getWidth(){
return width;
}
//Returns string representation of room
public String toString() {
return String.format("Room with Lenght : %5.2f and width : %5.2f",length,width);
}
}
------------------------------------------------------------------------------------------------------------
//RoomCarpet.java
public class RoomCarpet {
//Declare RoomDimension variable
private RoomDimension room;
//instance variable declaration
private double carpetCost;
private double totalCost;
//Constructor takes RoomDimension and cost
public RoomCarpet(RoomDimension r,double cost) {
room=r;
carpetCost=cost;
}
//Returns total cost
public double getTotalCost(){
double totalCost=0;
totalCost=room.getLength()*room.getWidth()*carpetCost;
return totalCost;
}
/*Method costsMore that takes RoomCarpet and returns
true if the total cost is more than the total cost
of RoomCarpet*/
public boolean costsMore(RoomCarpet other){
return getTotalCost()>other.getTotalCost();
}
public String toString() {
return room.toString()+
String.format("Carpet Cost($per sq.ft):%3.1f Total Cost($):%4.2f",carpetCost,getTotalCost());
}
}
------------------------------------------------------------------------------------------------------------
/**
* The java program RoomCarpetDemo that prompts user
* to ener length and width of a room and cost of
* two rooms.Then finds the cost of carpet and
* prints which room costs more to console
* */
//RoomCarpetDemo.java
import java.util.Scanner;
public class RoomCarpetDemo {
public static void main(String[] args) {
double length1,width1;
double length2,width2;
double cost1,cost2;
//create an instance of Scanner class
Scanner s=new Scanner(System.in);
System.out.println("Enter the dimensions of the first room: ");
length1=s.nextDouble();
width1=s.nextDouble();
System.out.println("Enter the cost per square foot of carpet: ");
cost1=s.nextDouble();
System.out.println("Enter the dimensions of the second room : ");
length2=s.nextDouble();
width2=s.nextDouble();
System.out.println("Enter the cost per square foot of carpet : ");
cost2=s.nextDouble();
//Create two RoomCarpet objects with RoomDimension and cost
RoomCarpet r1=new RoomCarpet(new RoomDimension(length1, width1), cost1);
RoomCarpet r2=new RoomCarpet(new RoomDimension(length2, width2), cost2);
//calling costsMore method
boolean result=r1.costsMore(r2);
if(result)
System.out.println(r1.toString()+" costs more than "+r2.toString());
else
System.out.println(r1.toString()+" and"+r2.toString()+" cost the same.");
}
}
------------------------------------------------------------------------------------------------------------
sample output:
Enter the dimensions of the first room:
10.0 10.0
Enter the cost per square foot of carpet:
5.0
Enter the dimensions of the second room :
10.0 5.0
Enter the cost per square foot of carpet :
10.0
Room with Lenght : 10.00 and width : 10.00Carpet Cost($per sq.ft):5.0 Total Cost($):500.00
andRoom with Lenght : 10.00 and width : 5.00Carpet Cost($per sq.ft):10.0 Total Cost($):500.00 cost the same.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.