Hey guys, Im VERY new to java and am trying to run a code out of a tutorial book, using 2 classes. It sets the number and scale as private and I think thats where Im having my problems. Can you look at the 2 classes and see what Im doing wrong? Thanks
CODE:
class Temperature {
private double number;
private ScaleName scale;
public Temperature() {
number = 0.0;
scale = ScaleName.farenheit;
}
public Temperature(double number) {
this.number = number;
scale = ScaleName.farenheith;
}
public Temperature(ScaleName scale) {
number = 0.0;
this.scale = scale;
}
double Temperature(double number, ScaleName scale) {
this.number = number;
this.scale = scale;
}
public void setNumber(double number) {
this.number = number;
}
public double getNumber() {
return number;
}
public void setScale(ScaleName scale) {
this.scale = scale;
}
public ScaleName getScale() {
return scale;
}
}
END:
CODE:
import static java.lang.System.out;
class UseTemperature {
public static void main(String args[]) {
final String format = "%5.2f degrees %s\n";
Temperature temp = new Temperature();
temp.setNumber(70.0);
temp.setScale(Scalename.fahrenheit);
out.printf(format, temp.getnumber(), temp.getScale() );
temp = new Temperature(32.0);
out.printf(format, temp.getNumber(), temp.getScale() );
temp = new Temperature(ScaleName.celsius);
out.printf(format, temp.getNumber(), temp.getScale() );
temp = new Temperature(2.73, ScaleName.kelvin);
out.printf(format, temp.getNumber(), temp.getScale() );
}
}
END:
Help, Ive been getting through this book and its all made sense until now.
Thank you!
BTW, I know I misspelled fahrenheit in the first code, its corrected and still not running.
Cant get this to workCant use getter methods on a private class
Page 1 of 1
9 Replies - 671 Views - Last Post: 11 February 2009 - 11:54 AM
Replies To: Cant get this to work
#2
Re: Cant get this to work
Posted 11 February 2009 - 09:22 AM
it seems that there should be one more class for ScaleName, can you post it please
#3
Re: Cant get this to work
Posted 11 February 2009 - 09:27 AM
[code]
enum ScaleName {celsius, fahenheit, kelvin, rankine};
[code]
Missed that, sorry
The book has a cd with the code prewritten in Jcreator, when I bring it up on there It works fine, but Im using a differant JDK called dr Java, its super simple and am trying to get it to work that way. Is it a syntax error? I fixed all the butchered fahrenheit spellings and its getting a "cannont find symbol" error.
enum ScaleName {celsius, fahenheit, kelvin, rankine};
[code]
Missed that, sorry
The book has a cd with the code prewritten in Jcreator, when I bring it up on there It works fine, but Im using a differant JDK called dr Java, its super simple and am trying to get it to work that way. Is it a syntax error? I fixed all the butchered fahrenheit spellings and its getting a "cannont find symbol" error.
#4
Re: Cant get this to work
Posted 11 February 2009 - 09:40 AM
temperature method is missing the return statement
or is that a constructor?, then that double shouldnt be there
or is that a constructor?, then that double shouldnt be there
#5
Re: Cant get this to work
Posted 11 February 2009 - 09:59 AM
Which double? Yes its a constructor.
OK, found the double temperature that should be public, changed it and its still not working,
It highlights this line and says cannont find symbol.
scale = ScaleName.fahrenheit;
OK, found the double temperature that should be public, changed it and its still not working,
It highlights this line and says cannont find symbol.
scale = ScaleName.fahrenheit;
#6
Re: Cant get this to work
Posted 11 February 2009 - 10:14 AM
it compiled over here
you just had a couple of typos
public class Temperature {
private double number;
private ScaleName scale;
public Temperature() {
number = 0.0;
scale = ScaleName.fahrenheit;
}
public Temperature(double number) {
this.number = number;
scale = ScaleName.fahrenheit;
}
public Temperature(ScaleName scale) {
number = 0.0;
this.scale = scale;
}
public Temperature(double number, ScaleName scale) {
this.number = number;
this.scale = scale;
}
public void setNumber(double number) {
this.number = number;
}
public double getNumber() {
return number;
}
public void setScale(ScaleName scale) {
this.scale = scale;
}
public ScaleName getScale() {
return scale;
}
}
enum ScaleName {celsius, fahrenheit, kelvin, rankine}
class UseTemperature {
public static void main(String args[]) {
final String format = "%5.2f degrees %s\n";
Temperature temp = new Temperature();
temp.setNumber(70.0);
temp.setScale(ScaleName.fahrenheit);
System.out.printf(format, temp.getNumber(), temp.getScale() );
temp = new Temperature(32.0);
System.out.printf(format, temp.getNumber(), temp.getScale() );
temp = new Temperature(ScaleName.celsius);
System.out.printf(format, temp.getNumber(), temp.getScale() );
temp = new Temperature(2.73, ScaleName.kelvin);
System.out.printf(format, temp.getNumber(), temp.getScale() );
}
}
you just had a couple of typos
#7
Re: Cant get this to work
Posted 11 February 2009 - 10:33 AM
Thank you. I'll try it. Do I need to have it all on the same workspace like you typed it or did you just do that for ease of reading?
#8
Re: Cant get this to work
Posted 11 February 2009 - 10:38 AM
you can have them in the same file but its not recommended, i just did it for ease.. better to have each class in a different file but dont forget to put them in same folder otherwise you'll get and error when trying to compile
#9
Re: Cant get this to work
Posted 11 February 2009 - 11:52 AM
Thought so.
Got it, thank you!!!
Got it, thank you!!!
#10
Re: Cant get this to work
Posted 11 February 2009 - 11:54 AM
no problem
glad i could help
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|