Results 1 to 10 of 10
  1. #1
    snobbysteven is offline Member
    Join Date
    Oct 2012
    Posts
    17
    Rep Power
    0

    Exclamation 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!



    Java 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)
    {
    }

    Java Code:
    public class TemperatureTest1832 {
    
     public static void main(String[] args)
     {
      Temperature1832 m1 = new Temperature1832(32, 'C');
      Temperature1832 m2 = new Temperature1832(50, 'F');
      
      
      
      
     }
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,543
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    snobbysteven is offline Member
    Join Date
    Oct 2012
    Posts
    17
    Rep Power
    0

    Exclamation Re: Temperature Conversion coding issue

    This is an update of what I have so far



    Java 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());
    }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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?

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,543
    Rep Power
    5

    Default 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.

    Java 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    snobbysteven is offline Member
    Join Date
    Oct 2012
    Posts
    17
    Rep Power
    0

    Default Re: Temperature Conversion coding issue

    Quote Originally Posted by jim829 View Post
    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.

    Java 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!

  7. #7
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default 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.
    Last edited by Ronin; 03-13-2013 at 01:10 PM. Reason: Clarification

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,543
    Rep Power
    5

    Default 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.

    XML 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    snobbysteven is offline Member
    Join Date
    Oct 2012
    Posts
    17
    Rep Power
    0

    Default 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.)

    Java 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());
    }
    
    
    }

    Java 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");
     }
     
      
      
     }
    }

  10. #10
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,543
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. String to date conversion issue...
    By nikhil.navadiya in forum New To Java
    Replies: 5
    Last Post: 12-21-2011, 11:28 AM
  2. Coding issue
    By mbw290 in forum New To Java
    Replies: 2
    Last Post: 09-04-2011, 06:02 AM
  3. Temperature analysis program
    By Evii0 in forum New To Java
    Replies: 8
    Last Post: 03-29-2011, 09:05 AM
  4. temperature program with inputs
    By JingGong in forum New To Java
    Replies: 3
    Last Post: 10-17-2008, 06:34 PM
  5. Temperature converstion
    By jamesov89 in forum New To Java
    Replies: 6
    Last Post: 09-29-2008, 04:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •