Here is the main java class, pretty simple just ask user for width and length of a room and the price for each square meter. Then it should display the area and the total cost for the room.
package exercise;
import javax.swing.JOptionPane;
public class carpetshopping {
public static void main(String[] args) {
// TODO Auto-generated method stub
String input;
double width;
double length;
double price;
input = JOptionPane.showInputDialog("Please enter the width of the room");
width = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Please enter the length of the room");
length = Double.parseDouble(input);
input = JOptionPane.showInputDialog("What about the price per unit area?");
price = Double.parseDouble(input);
RoomDimension dim = new RoomDimension(length, width);
System.out.println(dim);
RoomCarpet car = new RoomCarpet(dim, price);
System.out.println(car);
}
}
Here is the RoomDimension.java, it has two fields: length and width(both are double)which will get the dimension of a room and calculate the room area.
package exercise;
public class RoomDimension {
public double length;
public double width;
public RoomDimension(double len, double w) {
// TODO Auto-generated constructor stub
length = len;
width = w;
}
public RoomDimension(RoomDimension size) {
// TODO Auto-generated constructor stub
length = size.length;
width = size.width;
}
public double getArea() {
return length * width;
}
public String toString() {
return "The area of this room is " + this.getArea();
}
}
Here is the RoomCarpet.java, it has two fields, one is the price and the other is an object from RoomDimension.java, it will calculate the total cost of the room.
package exercise;
public class RoomCarpet {
public RoomDimension room;
public double carpetCost;
public RoomCarpet(RoomDimension room1, double carpetCost) {
// TODO Auto-generated constructor stub
room = new RoomDimension(room1);
carpetCost = carpetCost;
}
public double getTotalCost() {
return room.getArea() * carpetCost;
}
public String toString() {
return "The total cost is " + this.getTotalCost();
}
}
My problem is: whatever price user input, the total cost is always 0.0 Anyone help me? New baby to Java, a million thanks!