Cant get this to work

Cant use getter methods on a private class

Page 1 of 1

9 Replies - 775 Views - Last Post: 11 February 2009 - 11:54 AM Rate Topic: -----

#1 tommy1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-January 09

Cant get this to work

Post icon  Posted 11 February 2009 - 09:18 AM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Cant get this to work

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

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
Was This Post Helpful? 0
  • +
  • -

#3 tommy1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-January 09

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.
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

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
Was This Post Helpful? 0
  • +
  • -

#5 tommy1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-January 09

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;
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Cant get this to work

Posted 11 February 2009 - 10:14 AM

it compiled over here
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
Was This Post Helpful? 0
  • +
  • -

#7 tommy1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-January 09

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?
Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

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
Was This Post Helpful? 0
  • +
  • -

#9 tommy1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-January 09

Re: Cant get this to work

Posted 11 February 2009 - 11:52 AM

Thought so.

Got it, thank you!!!
Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Cant get this to work

Posted 11 February 2009 - 11:54 AM

no problem ;) glad i could help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1