Getters and Setters
I am trying to store user input into a set method to be used to compute something in another method within that same class. Having trouble getting it to work though. Here is a bit of whatI have, specifically.
publicdouble price;
publicdouble getPrice()
{
return price;
}
publicvoid setPrice(double p)
{
this.price = p;
}
and in the test...
String input = JOptionPane.showInputDialog ("Unit price");
Double price1 = setPrice(input);
Two things:1. Like someone has mentioned above. setPrice() doesn't return a value, therefore you cant set the value of 'price1' with it.2. Why do you need public visibility for 'price' attribute?Regards,yc
public class Item {
private double price;
public double getPrice() {
return price;
}
public void setPrice(double price ) {
this.price = price ;
}
...
}
//elsewhere
String input = JOptionPane.showInputDialog ("Unit price");
double unitPrice = Double.parseDouble(input);
Item item = new Item();
item.setPrice(unitPrice);
Does that make sense?