3 Replies - 4970 Views - Last Post: 27 March 2011 - 10:04 PM Rate Topic: -----

#1 Maxis0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-March 11

Temperature class that returns comment about the temp. (i.e. cold)

Posted 27 March 2011 - 04:56 PM

Hi all,
I am new to java and I have encountered my first wall so any and all help would be greatly appreciated. I am writing a temperature class that handles both (and converts among) Celsius and Fahrenheit. My constructor should have two explicit params, first a character (unitChar) then a double (value). Also, two double instance variables (cVal & fVal or tempC & tempF). Next, two accessor methods (getCelsius and getFahrenheit) that just return a double value. Finally, I'd like to add a comment method that provides a description of the temperature. For example, below 32 = "Freezing", 32-50 = "Brisk", 50-80 = "Nice" and above 80 = "Hot". My problem is I do not know how to properly incorporate the explicit params so I haven't made much progress since I took up this take task:/. Please help.

import java.util.*;

public class Temperature
{
   public static void main(String[] args)
   {
      double cVal;
      double fVal;
      double value;

      System.out.println("Please enter a temperature.")
      Scanner keyboard = new Scanner(System.in);
      value = keyboard.nextInt();

      char unitChar;
      System.out.println("Please enter the unit of measure F or C")
      tempUnit = keyboard.next().charAt(0);

      if((unitChar=='f')||(unitChar =='F'))
      {
         cVal = (5.0/9.0)* value - 32;
      }
      else if((unitChar=='C')||(unitChar =='c'))
      {
         fVal = (9.0/5.0) * value + 32;
      }
   }
   
   public double getCelsius()
   {
      double cVal = (5.0/9.0) * value - 32;
      return cVal;
   }
   public double getFahrenheit()
   {
      double fVal = (9.0/5.0) * value + 32;
      return fVal;
   }

   public String getDesription()
   {
      String retVal = "";
      if (value <= 32)
      {
         System.out.println("It is freezing.");
      }
      if ((value >= 33) && (value <= 50))
      {
         System.out.println("It is brisk.");
      }
      if ((value >= 51) && (value <= 80))
      {
         System.out.println("It is nice.");
      }
      else if (value > 81)
      {
         System.out.println("It is hot.");
      }

      System.out.println("Done");
   }
} 



Is This A Good Question/Topic? 0
  • +

Replies To: Temperature class that returns comment about the temp. (i.e. cold)

#2 Manbearpig101  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 62
  • Joined: 17-June 10

Re: Temperature class that returns comment about the temp. (i.e. cold)

Posted 27 March 2011 - 05:08 PM

Something like this, maybe?

class Temperature {
     public char tempType;
     public double temp;
     public Temperature(char tempType, double temp) {
          this.tempType = tempType;
          this.temp = temp;
     }
     public double getFahrenheit() {
          if (tempType == 'f' || tempType == 'F') {
               return temp;
          } else {
               return ((temp * 9) / 5) + 32;
          }
     }
     public double getCelcius() {
          if (tempType == 'c' || tempType == 'C') {
               return temp;
          } else {
               return ((temp - 32) * 5) / 9;
          }
     }
     public String getDescription() {
          double value = getFahrenheit();//We use F as our standard
          if (value <= 32) {
               return "It is freezing!";
          } else if ((value >= 33) && (value <= 50)) {
               return "It is brisk.";
          } else if ((value >= 52) && (value <= 80)) {
               return "It is nice.";
          } else if (value >= 81) {
               return "It is hot!";
          }
          return "??";
     }
}


Was This Post Helpful? 1
  • +
  • -

#3 Ember  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 160
  • Joined: 24-April 10

Re: Temperature class that returns comment about the temp. (i.e. cold)

Posted 27 March 2011 - 05:28 PM

I'm going to make an assumption that you wish to style this in an Object-Oriented like fashion (because you said Temperature Class and have accessor methods). Your code, as is, will not work. Here is how an object is supposed to be constructed:

public class Temperature{
   private double cVal, fVal;
   //Methods go here to initialize, and access these "properties"
}



First thing to note is an object has "properties". So now, every Temperature object we make, will have these properties of cVal and fVal. We need a way to "Initialize" these values when we first make our new Temperature object. So..

public class Temperature{
   private double cVal, fVal;
   public Temperature(double value, char unitChar){
   if((unitChar=='f')||(unitChar =='F'))
      {
         cVal = (5.0/9.0)* value - 32;
         fVal = value;
      }
      else if((unitChar=='C')||(unitChar =='c'))
      {
         fVal = (9.0/5.0) * value + 32;
         cVal = value;
      }
  }
   //Other stuff that will be filled in as we move along here
}



I used your code to do this, you can see many similarities in your code and this "Constructor". Constructors are what you use to initialize properties of an Object. So, in your main code of another program, if you wanted to create a temperature class, it would look something like this:
Temperature detroit = new Temperature(21, 'f'); //I just created it so that the fVal = 21 and cVal is...-6 or somethin..



How does it work? A constructor is called whenever you call 'new _____' the blank being the object you are creating. We defined that the constructor must intake a double we named value and a char to define the temperature here:
 public Temperature(double value, char unitChar)


The code in the constructor said that if the unitChar was an f, then value will be set to fVal and cVal will do it's calculations to get it's value. Visa Versa for if unitChar is c.

Now comes the fun part. We called our cVal and fVal private wayyyy above. This means we can't access them if we say:
Temperature detroit = new Temperature(21, 'f');
System.out.println(detroit.cVal);//Invalid code because cVal is private.


We need an accessor to return the cVal to us. Remember that both cVal and fVal are both set already, we just need to ACCESS them.
public class Temperature{
   private double cVal, fVal;
   public Temperature(double value, char unitChar){
   if((unitChar=='f')||(unitChar =='F'))
      {
         cVal = (5.0/9.0)* value - 32;
         fVal = value;
      }
      else if((unitChar=='C')||(unitChar =='c'))
      {
         fVal = (9.0/5.0) * value + 32;
         cVal = value;
      }
  }
  public double getCelsius(){
     return cVal;
 }
}



Now I defined a method called getCelsius that is public, I can access it in the object like so:

Temperature detroit = new Temperature(-5, 'c');//Initializes a Celsius temperature to -5 degrees.
System.out.println("Temperature in Detroit is: "+detroit.getCelsius());//Will output "Temperature in Detroit is -5"



I am going to end my mini-tutorial here. Use the code I have given you to set up the rest of the methods you desire. If I have lost you anywhere(or you are just working procedural style), don't be afraid to message me, it isn't a subject to learn in an instant.

Hope I have given you a very basic understanding of Objects and how to use them.
Was This Post Helpful? 3
  • +
  • -

#4 Maxis0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-March 11

Re: Temperature class that returns comment about the temp. (i.e. cold)

Posted 27 March 2011 - 10:04 PM

I appreciate your responses Ember and Manbear... thanks to Embers little tutorial I was able to fix up my code and run the program the way I intended (and was far from).

public class Temperature 
{
	private double cVal, fVal;
	public Temperature(double value, double d)
	{
		if((d=='f')||(d =='F'))
		{
			cVal = (5.0/9.0) * value - 31;
			fVal = value;
		}
		else if((d=='C')||(d=='c'))
		{
			fVal = (9.0/5.0) * value + 32;
			cVal = value;
		}
	}
    public double getFahrenheit()
	{
		return fVal;
	}
	
	public double getCelsius()
	{
		return cVal;
	}
    public String getDescription()
	{
		double value = getFahrenheit();
		if (value <= 32)
		{
			return "Brrrr. It's freezing!";
		}
		else if ((value >= 33) && (value <= 80))
		{
			return "It is brisk.";
		}
		else if ((value >= 52) && (value <= 80))
		{
			return "It is nice.";
		}
		else if (value >= 81)
		{
			return "It is hot!";
		}
		return null;
	}
}



Thanks again!

This post has been edited by Maxis0n: 27 March 2011 - 10:10 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1