Reputation: 11
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!
Upvotes: 0
Views: 148
Reputation: 837
The problem is in your RoomCarpet
class. In the constructor you are confusing the JVM by using the same variables carpetCost = carpetCost
, Which results in instance variable hiding. The compiler gets confused and is not able to assign the correct value to the carpetCost variable.
Your Modified RoomCarpet Class:
class RoomCarpet {
public RoomDimension room;
public double carpetCost;
public RoomCarpet(RoomDimension room1, double carpetCost) {
room = new RoomDimension(room1);
this.carpetCost = carpetCost;
}
public double getTotalCost() {
return room.getArea() * carpetCost;
}
public String toString() {
return "The total cost is " + this.getTotalCost();
}
}
It is illegal in Java to declare two local variables with the same name inside the same or enclosing scopes. Interestingly, you can have local variables, including formal parameters to methods, which overlap with the names of the class’ instance variables. However, when a local variable has the same name as an instance variable, the local variable hides the instance variable.
Upvotes: 2
Reputation: 3516
Your problem is in your RoomCarpet class, in the constructor to be specififc.
There you are writing
public RoomCarpet(RoomDimension room1, double carpetCost) {
// TODO Auto-generated constructor stub
room = new RoomDimension(room1);
carpetCost = carpetCost;
}
and the problem here is that the compiler uses the parameter variable for "carpetCost" both times so you are basically assigning the variable to itself and don't even touch the instance variable you would like to change.
To overcome this problem reference the instance variable with "this":
public RoomCarpet(RoomDimension room1, double carpetCost) {
// TODO Auto-generated constructor stub
room = new RoomDimension(room1);
this.carpetCost = carpetCost;
}
To avoid situations like this in the future just get used to reference instance variables via "this." because the the compiler knows which variable you're talking about.
By the way...If you're not using eclipse already (which I am assuming because of your problem) I would highly recommend it to you. It took me about a second to find the problem in your code because eclipse already marked the problematic part for me and told me that there might be a problem with it ;)
Greetings Raven
Upvotes: 2