Temperature Conversion coding issue

• 03-13-2013, 02:13 AM
snobbysteven
Temperature Conversion coding issue
I am stuck right here. I cant figure out how to compare the 2 temperatures and I also cant figure out to get 2 temperatures at the same time to compare without creating another degree and unit variable. I am also lost on how to do the boolean equals with (Temperature another) as well. Any help is appreciated!

Code:

```public class Temperature1832 {  public double degrees;  private char units; //'C' for Celsius, 'F' for Fahrenheit    public Temperature1832(double newDegrees, char newUnits)  {   set (newDegrees, newUnits);  }  public Temperature1832(double newDegrees)  {   set (newDegrees, 'C');  }  public Temperature1832(char newUnits)  {   set(0, newUnits);  }  public Temperature1832()//default  {   set(0, 'C');  }      public void set(double newDegrees, char newUnits)  {  newDegrees = 0;  newUnits = 'C';   }    public void set(double newDegrees)  {   degrees = newDegrees;  }    public void set(char newUnits)  {  if(newUnits == 'F')       {     units = newUnits;   }   else   {   System.out.println("Error: data is incorrect");   System.exit(0);;  }  }    public double getC()  {   if(units == 'F')   degrees = (5*(degrees-32)/9);       return degrees;  }  public double getF()  {     if(units == 'C')     degrees = (9*(degrees/5)+32);       return degrees;  } public boolean equals(Temperature another) { } public boolean isGreaterThan(Temperature another) { } public boolean isLessThan(Temperature another) { }```

Code:

```public class TemperatureTest1832 {  public static void main(String[] args)  {   Temperature1832 m1 = new Temperature1832(32, 'C');   Temperature1832 m2 = new Temperature1832(50, 'F');          } }```
• 03-13-2013, 02:29 AM
jim829
Re: Temperature Conversion coding issue
Why not just pick a scale (C or F) and use that as your base for all temperatures. Then, if someone sets the temperature, you either record it directly or convert to the chosen scale. Same thing when comparing two temperatures, if they specify one scale, then you either compare directly or convert their input to your base scale.

Regards,
Jim
• 03-13-2013, 02:51 AM
snobbysteven
Re: Temperature Conversion coding issue
This is an update of what I have so far

Code:

```public class Temperature1832 {  private double degrees;  private char units; //'C' for Celsius, 'F' for Fahrenheit    public Temperature1832(double newDegrees, char newUnits)  {   set (newDegrees, newUnits);  }  public Temperature1832(double newDegrees)  {   set (newDegrees, 'C');  }  public Temperature1832(char newUnits)  {   set(0, newUnits);  }  public Temperature1832()//default  {   set(0, 'C');  }      public void setTemperature1832(double newDegrees, char newUnits)  {  set(newDegrees, newUnits);   }    public void setdegrees(double newDegrees)  {   set(newDegrees, units);  }    public void setunits(char newUnits)  {  if(newUnits == 'F')       {     set(degrees, newUnits);   }   else   {   System.out.println("Error: data is incorrect");   System.exit(0);;  }  }  public void set(double newDegrees, char newUnits)  {   degrees = newDegrees;   if(units != 'C' || units != 'F') {   System.out.println("invalid input");   System.exit(0); } else {   units = newUnits; } }    public double getC()  {   double degrees1 = 0;   if(units == 'F')   degrees1 = (5*(degrees-32)/9);       return degrees1;  }  public double getF()  {   double degrees2 = 0;   if(units == 'C')     degrees2 = (9*(degrees/5)+32);       return degrees2;  } public boolean equals(Temperature another) {   return (this.degrees == another.getF()); } public boolean isGreaterThan(Temperature another) {   return (this.degrees > another.getF()); } public boolean isLessThan(Temperature another) {   return (this.degrees < another.getF()); } }```
• 03-13-2013, 03:11 AM
Fubarable
Re: Temperature Conversion coding issue
So since you didn't ask any questions about your new code, I guess we can assume that your program works the way you want it to work now, right?
• 03-13-2013, 03:14 AM
jim829
Re: Temperature Conversion coding issue
Unless you have been instructed to do it this way, you are making it harder than it should be. For example, you don't need a unit value except to guide the conversion process. Just store the temperature in say, Celsius.

Code:

```double celciusDegrees; public void set(double degrees, String unit) {   if (unit.equals("C"))     celciusDegrees = degrees;   else if (unit.equals("F"))     celciusDegrees = (degrees-32)*5/9.;   else       System.out.println("error!"; }```
The rest of your code can be constructed similarly for get and comparisons. In otherwords, you are normalizing your data (in this case temperature) to a standard unit.

Regards,
Jim
• 03-13-2013, 06:57 AM
snobbysteven
Re: Temperature Conversion coding issue
Quote:

Originally Posted by jim829
Unless you have been instructed to do it this way, you are making it harder than it should be. For example, you don't need a unit value except to guide the conversion process. Just store the temperature in say, Celsius.

Code:

```double celciusDegrees; public void set(double degrees, String unit) {   if (unit.equals("C"))     celciusDegrees = degrees;   else if (unit.equals("F"))     celciusDegrees = (degrees-32)*5/9.;   else       System.out.println("error!"; }```
The rest of your code can be constructed similarly for get and comparisons. In otherwords, you are normalizing your data (in this case temperature) to a standard unit.

Regards,
Jim

well this is the assignment sheet I was given,

Project 10 on page 468
• Class Temperature0000
o instance variables: the following must be included:
private double degrees;
private char units;//'C' for Celsius, 'F' for Fahrenheit
o methods
 Four Constructors
//Constructor to specify both temperature and units.
public Temperature(double newDegrees, char newUnits)
//Constructor to specify just the temperature, defaults to 'C' for Celsius.
public Temperature(double newDegrees)
//Constructor to specify just the units, defaults to 0 degrees.
public Temperature(char newUnits)
//Default constructor (specifies neither temperature or units), defaults to 0 degrees Celsius.
public Temperature()
 three mutators: methods to set (reset) the parameter values.
public void set(double newDegrees, char newUnits)
public void set(double newDegrees) //Method to set temperature only.
public void set(char newUnits) //Method to set units only.
 two accessors
public double getC()
public double getF()
 Three comparison methods: note you have to consider the units
public boolean equals(Temperature another)
public boolean isGreaterThan(Temperature another)
public boolean isLessThan(Temperature another)
 Method returns a string, e.g. “0 degree Celsius which is 32 degrees Fahrenheit”
public String toString() // not required in the textbook
• ClassTemperatureTest0000: test all your methods
Please note, in order to compare two temperature objects, you should make sure you are comparing based on the same unit, either Celsius or Fahrenheit. To do so, you can call getC or getF method for both objects.

am i still making it harder on myself then it needs to be?

thanks for the help!
• 03-13-2013, 11:57 AM
Ronin
Re: Temperature Conversion coding issue
I would have to agree with Jim. Use a single variable to store the units but be aware of rounding errors within the conversion.

Regards.
• 03-13-2013, 01:12 PM
jim829
Re: Temperature Conversion coding issue
Well, everything is pretty straight forward. I just don't agree with the need to store the units. My reasoning is that as long as the degrees are always stored in Celcius, then the units don't need to be stored. You can discuss this with your instructor but it could be that he or she just does not want a default temp but to use an indicator on how the temp is stored. Perhaps its to give the problem more complexity. You can still keep the units. Just always make them "C" Then you will never have to think about it. And if the internal representation of temp is always Celcius, it makes your comparison methods easier.

Here is a example of what I am talking about in pseudo code.

Code:

```// Using default temp // degrees are always stored in same units boolean isGreaterThan(Temp t)             return degrees > t.degrees   //Using units and no default temp //Conversion are only temporary and should not //permanently change instance values boolean isGreaterThan(temp t)   if units is equal to t.units               return degrees > t.degrees  else if units is equal to F               convert degrees to C                   return degrees > t.degrees   else         convert degrees to F                   return degrees > t.degrees```
Regards,
Jim
• 03-13-2013, 05:39 PM
snobbysteven
Re: Temperature Conversion coding issue
that worked a lot better! I have the program working as far as it prints out the 2 temperatures in Celsius and then the IF statement I have on the test page determines which one is larger or smaller. my next question is how to I get that if statement off the test page and replace it will the output from the boolean statements. I think I have the boolean statements typed up right but im not use to them having parameters so I cant figure out how to type them to bring the answer over to the test page with the parameters there. (for the boolean statements I used celsiusDegrees instead of degrees because degrees is private when I try to compile.)

Code:

```public class Temperature1832 {  private double degrees;  private char units; //'C' for Celsius, 'F' for Fahrenheit  private double celsiusDegrees;    public Temperature1832(double newDegrees, char newUnits)  {   set (newDegrees, newUnits);  }  public Temperature1832(double newDegrees)  {   set (newDegrees, 'C');  }  public Temperature1832(char newUnits)  {   set(0, newUnits);  }  public Temperature1832()//default  {   set(0, 'C');  }  public void setTemperature1832(double newDegrees, char newUnits)  {  set(newDegrees, newUnits);   }    public void setdegrees(double newDegrees)  {   set(newDegrees, units);  }    public void setunits(char newUnits)  {     set(degrees, newUnits);    }  public void set(double newDegrees, char newUnits)  {   degrees = newDegrees;   units = newUnits;     if (units == ('C'))     celsiusDegrees = degrees;   else if (units == ('F'))     celsiusDegrees = (degrees-32)*5/9.;   else       System.out.println("error!"); }    public double getC()  {   return celsiusDegrees;  }  public double getF()  {   return degrees;  } public boolean equals(Temperature another) {   return (celsiusDegrees == another.getC()); } public boolean isGreaterThan(Temperature another) {   return (celsiusDegrees > another.getC()); } public boolean isLessThan(Temperature another) {   return (celsiusDegrees < another.getC()); } }```

Code:

```public class TemperatureTest1832 {  public static void main(String[] args)  {   Temperature1832 m1 = new Temperature1832(165, 'F');   Temperature1832 m2 = new Temperature1832(80, 'C');       System.out.println("both temperatures converted to C will give you this:");  System.out.println(m1.getC());  System.out.println(m2.getC());        if(m1.getC() > m2.getC())  {  System.out.println("This tells you that your first input is higher then your second");  }  else if(m1.getC() < m2.getC())  {   System.out.println("This tells you that your first input is lower then your second");  }  else  {   System.out.println("Both inputs are equal");  }        } }```
• 03-13-2013, 06:08 PM
jim829
Re: Temperature Conversion coding issue
if (equalstest)
printsomething
else if(greaterthantest)
printsomething
else // no test required here because less than is all that is left.
printsomething

Here is a hint on if statements. They evaluate the expression to true or false. Since your tests return true or false you can use the return value directly.

Regards,
Jim