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);

[906 byte] By [cowboy11_01a] at [2007-11-27 3:21:59]
# 1
What error are you getting?
macrules2a at 2007-7-12 8:24:45 > top of Java-index,Java Essentials,Java Programming...
# 2
> Double price1 = setPrice(input)setPrice is declared as void and doesn't return a value.Kaj
kajbja at 2007-7-12 8:24:45 > top of Java-index,Java Essentials,Java Programming...
# 3
It can return a value, or change to set and then get.Kaj
kajbja at 2007-7-12 8:24:45 > top of Java-index,Java Essentials,Java Programming...
# 4
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
ycliana at 2007-7-12 8:24:45 > top of Java-index,Java Essentials,Java Programming...
# 5

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?

DrLaszloJamfa at 2007-7-12 8:24:45 > top of Java-index,Java Essentials,Java Programming...