Quantcast

Jump to content


Photo

Programmer Defined Classes (Java)


  • Please log in to reply
29 replies to this topic

#1 Norava

Norava
  • Pro Can Cran

  • 547 posts


Users Awards

Posted 27 October 2011 - 09:42 AM

Okay, so I can set tempF and tempC , but how do I convert temp to celsius, or temp to fahrenheit in the main class? I'm at a stand still right now. This is my user defined class. Yes, this is homework, and no, I'm not asking for you to write the code. Just some guidance in the right direction. Also, the question is at the beginning of the programmer defiined class.

//Define a new class named Temperature. The class has two //accessors— to- Fahrenheit and toCelsius— that return the 

//temperature in the specified unit and two mutators— 

//setFahrenheit and setCelsius— that assign the temperature 

//in the specified unit. Maintain the temperature internally in

//degrees Fahrenheit. Using this class, write a program that 

//inputs temperature in degrees Fahrenheit and outputs the 

//temperature in equivalent degrees Celsius.







public class Temperature {

	

	private double tempF;

	private double tempC;

	

		public Temperature(double tempF,double tempC){

			

			this.tempF = tempF;

			this.tempC = tempC;

		}

		




		

		public void setTempF(double tempF)

		{

			this.tempF = tempF;

		}

		

		public void setTempC(double tempC)

		{

			this.tempC = tempC;

		}

		

		public double toTempF()

		{

			return tempF = (9/5) * tempC + 32;

		}

		

		public double toTempC()

		{

			return tempC = (5/9) * (tempF - 32);

		}




}







This is my main method -not complete- , also which I'm stuck on.



public class C4Q7 {




	

	public static void main(String[] args) {

		

		Temperature celcius, fahrenheit;

		

		fahrenheit = new Temperature();

		fahrenheit.setTempF(93.89);

		

		celcius = new Temperature();

		celcius.setTempC(32);




	}





}


#2 Scot

Scot
  • ≡^ᴥ^≡

  • 3935 posts


Users Awards

Posted 27 October 2011 - 09:50 AM

You already declared and initialized the data members so all that's left is to call ToTempF() and ToTempC() for each object and print the result.


You should include parameters when you declare Temperature because you didn't set a default constructor. So instead of
<pre class="prettyprint"><span class="kwd">public</span><span class="pln"> </span><span class="typ">Temperature</span><span class="pun">(</span><span class="kwd">double</span><span class="pln"> tempF</span><span class="pun">,</span><span class="kwd">double</span><span class="pln"> tempC</span><span class="pun">){</span><span class="pln">

                        

                        </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">tempF </span><span class="pun">=</span><span class="pln"> tempF</span><span class="pun">;</span><span class="pln">

                        </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">tempC </span><span class="pun">=</span><span class="pln"> tempC</span><span class="pun">;</span><span class="pln">

                </span><span class="pun">}</span><span class="pln">
</span></pre>
do
<pre class="prettyprint"><span class="kwd">public</span><span class="pln"> </span><span class="typ">Temperature</span><span class="pun">(</span><span class="pln"></span><span class="pun">){</span><span class="pln">

                        

                        </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">tempF </span><span class="pun">=</span><span class="pln"> 0.0</span><span class="pun">;</span><span class="pln">

                        </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">tempC </span><span class="pun">=</span><span class="pln"> 0.0</span><span class="pun">;</span><span class="pln">

                </span><span class="pun">}</span><span class="pln">
</span></pre>


#3 Sida

Sida
  • Tsvetesman

  • 3865 posts

Posted 27 October 2011 - 10:02 AM

Also note that there's currently no way to get the result after you've converted it...so think about that when trying to print it ;)

#4 Norava

Norava
  • Pro Can Cran

  • 547 posts


Users Awards

Posted 27 October 2011 - 10:23 AM

So I set it at 0.0, but my biggest problem right now is how do I take the set temperature, and convert it to the other. For example, I set the tempF as 100, and do I just do
System.out.println("Fahrenheit converted to Celsius: " + (insert conversion here))

Also, if I wanted to have a scanner take the information, and set tempF and tempC, how would I go about doing that?

#5 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 27 October 2011 - 10:35 AM

The program brief says to maintain the temperature in Fahrenheit only, that suggests to me that you shouldn't have a tempC variable but instead convert it when necessary from Fahrenheit for output.

Maintain the temperature internally in degrees Fahrenheit.



#6 Sida

Sida
  • Tsvetesman

  • 3865 posts

Posted 27 October 2011 - 10:42 AM

You see the "public void" and "public double"? Void means that method gives you nothing back, but double means it will give you a double in return when you call it. That means for printing, you can use the method itself and it will essentially be treated as a double instead of a method as far as your task is concerned.

The first class has some private variables..when you set the temperature...you're changing this variable. When you call the method I mentioned above, it gets converted and sent back to you.

The program brief says to maintain the temperature in Fahrenheit only, that suggests to me that you shouldn't have a tempC variable but instead convert it when necessary from Fahrenheit for output.



I thought the same...but for some reason I was under the impression he was given the first class.

#7 Norava

Norava
  • Pro Can Cran

  • 547 posts


Users Awards

Posted 27 October 2011 - 10:44 AM

Kind of a stupid question, but internally means the user-defined class?

#8 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 27 October 2011 - 10:44 AM

Kind of a stupid question, but internally means the user-defined class?


In the Temperature class.

#9 Sida

Sida
  • Tsvetesman

  • 3865 posts

Posted 27 October 2011 - 10:46 AM

Kind of a stupid question, but internally means the user-defined class?


It means don't actually store the temp in celcuis in the user-defined class. You should only store the temp in F as a variable, not both.

#10 Norava

Norava
  • Pro Can Cran

  • 547 posts


Users Awards

Posted 27 October 2011 - 05:03 PM

Thanks for your patience everyone. I'm learning the code quickly, but my vocabulary isn't good. Posted Image

I'm still having problems. When I try to sytem.out.print fahrenheit, It messes up. O_o I'm still relatively confused as to how the whole "programmer defined classes" work.



public class C4Q7 {




	

	public static void main(String[] args) {

		

		Temperature celcius, fahrenheit;

		

		fahrenheit = new Temperature();

		fahrenheit.setTempF(94);

		

		celcius = new Temperature();

		celcius.setTempC(32);

		

		System.out.print("The temperature in Fahrenheit is: " + fahrenheit);




	}





}


And again, the main code.

//Define a new class named Temperature. The class has two 

//accessors— to- Fahrenheit and toCelsius— that return the 

//temperature in the specified unit and two mutators— 

//setFahrenheit and setCelsius— that assign the temperature 

//in the specified unit. Maintain the temperature internally in

//degrees Fahrenheit. Using this class, write a program that 

//inputs temperature in degrees Fahrenheit and outputs the 

//temperature in equivalent degrees Celsius.







public class Temperature {

	

	private double tempF;

	private double tempC;

	

		public Temperature(){

			

			this.tempF = 0.0;

			this.tempC = 0.0;

		}

		




		

		public void setTempF(double tempF)

		{

			this.tempF = tempF;

		}

		

		public void setTempC(double tempC)

		{

			this.tempC = tempC;

		}

		

		public double toTempF()

		{

			return tempF = (9/5) * tempC + 32;

		}

		

		public double toTempC()

		{

			return tempC = (5/9) * (tempF - 32);

		}





}


And I took out tempC in the first code snippet, and then re put it back in. As neither of them worked.

Edited by SirBaggy, 27 October 2011 - 05:03 PM.


#11 Scot

Scot
  • ≡^ᴥ^≡

  • 3935 posts


Users Awards

Posted 27 October 2011 - 05:04 PM

You can't print an object. You need a toString() method in your class

#12 Norava

Norava
  • Pro Can Cran

  • 547 posts


Users Awards

Posted 27 October 2011 - 05:31 PM

So would I have to use a getTempF() or a toString? Since it's double, wouldn't it be different?

#13 Junsu

Junsu
  • 1566 posts

Posted 27 October 2011 - 05:37 PM

Why is your code so ugly :( with all that white space


Your
System.out.print("The temperature in Fahrenheit is: " + fahrenheit);
Should be
System.out.print("The temperature in Fahrenheit is: " + fahrenheit.tempF);

EDIT:
Ops nvm, tempF isnt public.
Make a getTempF method

Edited by coolaznmagekid342, 27 October 2011 - 05:40 PM.


#14 Norava

Norava
  • Pro Can Cran

  • 547 posts


Users Awards

Posted 27 October 2011 - 06:50 PM

I think white space is pretty.... I think my double toTemp is wrong O_o any opinions?

#15 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 28 October 2011 - 12:53 AM

You still have your tempC variable in your Temperature class, you shouldn't ignore what you were asked to do or you'll lose marks for it. You should only have a tempF variable and convert it when necessary.

#16 Sida

Sida
  • Tsvetesman

  • 3865 posts

Posted 28 October 2011 - 01:15 AM

Ok you're getting a little confused about this. You said you don't want it done for you so I'll try not to give too much away. The question is a little stupid anyway as if you're only supposed to store the temp in degrees F, there's no reason for a setCalcius method but whatever.

Get rid of Temp C. Anything to do with it, get rid of it. You don't need the variable "private double tempc", you dont need the class "Temperature calcius", just get rid of it all. You only need the methods mentioned in the question.


Your "programmer defined class" should be thought of like an object that does things for you. What do you need to do? Well you need to:


  • Have one method that will let you say "Oi, remember this for me would you? It's the temperature in Fahrenheit"
  • Have one method that will let you say "What was that temperature I told you earlier?"
  • Have one method that will let you say "Remember the temperature I gave you earlier? Can I have it back in Celcius this time instead?"
That should be what your Temperature class does. Then, in your main class, you can create a new Temperature class and ask those exact questions to it. For the last question, the class only needs to work out the temp in Celcius when you ask it. There's no reason to save it.


For your printing, your toTempF() method returns a double for you to use, so use it like one:


System.out.println("The temperature in Fahrenheit is: " + fahernheit.toTempF());


The same goes for toTempC();

#17 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 28 October 2011 - 01:36 AM

Ok you're getting a little confused about this. You said you don't want it done for you so I'll try not to give too much away. The question is a little stupid anyway as if you're only supposed to store the temp in degrees F, there's no reason for a setCalcius method but whatever.


They do need a setCelsius method.

#18 Sida

Sida
  • Tsvetesman

  • 3865 posts

Posted 28 October 2011 - 01:50 AM

They do need a setCelsius method.


Why? The temp is only ever stored as fahrenheit and is converted on the return, what use is that method? O.o

#19 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 28 October 2011 - 01:57 AM

Why? The temp is only ever stored as fahrenheit and is converted on the return, what use is that method? O.o


Because it's in the program brief:

two mutators—

//setFahrenheit and setCelsius— that assign the temperature

//in the specified unit.


So the user would input it in Celsius and the program would convert it to Fahrenheit and set it to the variable.

#20 Sida

Sida
  • Tsvetesman

  • 3865 posts

Posted 28 October 2011 - 02:01 AM

Because it's in the program brief:



So the user would input it in Celsius and the program would convert it to Fahrenheit and set it to the variable.


Oh well yeah I know it's in the brief, but it's stupid that it's there. As soon as you set it to the variable, you're no longer maintaining the temperature internally as fahrenheit. The brief says nothing about converting C to F.

#21 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 28 October 2011 - 02:08 AM

Oh well yeah I know it's in the brief, but it's stupid that it's there. As soon as you set it to the variable, you're no longer maintaining the temperature internally as fahrenheit. The brief says nothing about converting C to F.


You are maintaining it in Fahrenheit because you're converting it from Celsius to Fahrenheit before setting it.

So if they do setCelsius(38.0) it gets converted to 100.4 before being set to the variable, there's not a problem there.

#22 Sida

Sida
  • Tsvetesman

  • 3865 posts

Posted 28 October 2011 - 02:26 AM

You are maintaining it in Fahrenheit because you're converting it from Celsius to Fahrenheit before setting it.

So if they do setCelsius(38.0) it gets converted to 100.4 before being set to the variable, there's not a problem there.


Yes but this program shouldn't have to call setCelsius() at all. Having this method in the first place means taking the temp in F, converting it to C, converting it back to F to store it, the converting it to C again to return it. It's pointless.

#23 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 28 October 2011 - 03:33 AM

Yes but this program shouldn't have to call setCelsius() at all. Having this method in the first place means taking the temp in F, converting it to C, converting it back to F to store it, the converting it to C again to return it. It's pointless.


No, it says there's a setFahrenheit method and a setCelsius method so there's the option to use either. If they use setFahrenheit obviously you're not going to convert it to C and then back to F to store it...that would be stupid.

Just because the program requested in the brief doesn't use that functionality doesn't mean that it shouldn't be included in the class, a good class should be designed so that it can be reused for different projects rather than just designed to fit one specific program.

#24 Sida

Sida
  • Tsvetesman

  • 3865 posts

Posted 28 October 2011 - 03:45 AM

No, it says there's a setFahrenheit method and a setCelsius method so there's the option to use either. If they use setFahrenheit obviously you're not going to convert it to C and then back to F to store it...that would be stupid.

Just because the program requested in the brief doesn't use that functionality doesn't mean that it shouldn't be included in the class, a good class should be designed so that it can be reused for different projects rather than just designed to fit one specific program.


Additional functionality is nice, sure, but then why not go ahead and add a whole flurry of methods, you know, just in case? Let's make it return the ml of liquid that would be required in a small thermometer to be able to display the outputted temp. My point is, in this small one-way conversion project that will never even have temp in C passed to it, that method is pretty much there for the hell of it and doesn't need to be.

#25 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 28 October 2011 - 03:59 AM

Additional functionality is nice, sure, but then why not go ahead and add a whole flurry of methods, you know, just in case? Let's make it return the ml of liquid that would be required in a small thermometer to be able to display the outputted temp. My point is, in this small one-way conversion project that will never even have temp in C passed to it, that method is pretty much there for the hell of it and doesn't need to be.


You include it because it's specified explicitly in the program brief. It's not usually a good idea to leave things out just because you don't think they should be included.


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users