Extremely Confusing Prompt from instructor.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amistak
    New Member
    • Mar 2010
    • 3

    Extremely Confusing Prompt from instructor.

    So my teacher wants us to write the following program:
    "2. Write a Temperature class that has two instance variables: a temperature value ( a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have four constructor methods: one for each instance variable (assume zero degree if no value is specified and Celsius if no scale is specified), one with two parameters for the two instance variables, and a no argument constructor (set to Zero degree Celsius). Include the following: (1) two get methods to return the temperature one to return the degrees Celsius, the other to return the degrees Fahrenheit -use the following formulas to write the two methods, and round to the nearest tenth of a degree:
    degreeC = 5(degreeF – 32)/9
    degreeF = (9(degreeC)/5) + 32
    (2) three comparison methods: an equals method to test whether two temperatures are equal, one method to test whether one temperature is grater than another, and one method to test whether one temperature is less than another( note that a Celsius temperature can be equal to a Fahrenheit temperature as indicated by the above formulas). Then write a driver program (or Programs) that tests all the methods. Be sure to use each of the constructors, to include at least one true and one false case for each of the comparison methods, and to test at last the following temperature equalities: 0.0 degree C=32.0 degrees F, -40.0 degrees C = -40.0 degree F, and 100.0 degree C=121.0 degree F."

    She said that I only need two instance variables, a double temperature value,
    and a String a = " ";
    But then she requests that I write methods that compare two values of temperature, the methods would figure out if they were equal, larger or smaller then each other. She then requests that we use a test class to use each of these methods. I am extremely confused on how I should orientate the main class, as well as how to call the methods in the test class.
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Could we see the code you have defined for your temperature class?
    Have you defined a CompareTo() or Equals() type method?

    Comment

    • amistak
      New Member
      • Mar 2010
      • 3

      #3
      Code:
      public class Temp
      {
      double temperature = 0;
      String Scale = " ";
      
          /**
           * Constructor for objects of class Temp
           */
          public Temp(double x,String b)
          {
              temperature = x;
              Scale = "a";
          }
          public Temp()
          {
              temperature = 0;
              Scale = " ";
          }
          public double celcius()
          {
              temperature = 5.0*(temperature - 32)/9.0 ;
              return temperature;
          }
          public double fahrenheit()
          {
              temperature = (9*(temperature)/5.0) + 32;
              return temperature;
          }
          public double equal()
          {
              if(x==y)
              
              System.out.println(" The numbers are equal to eachother " );
              
              
          }
          public double greater()
          {
          }
          public double smller()
          {
          }
          
      }
      This is my code so far. Doing any sort of comparison method confuses me as she only wanted me to declare one temperature value. I would assume that my constructor that is taking values would need more than just one value and its scale if I was going to use methods in my main class for comparison.
      Last edited by Frinavale; Mar 18 '10, 08:31 PM. Reason: Changed quote tags into code tags.

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        I can see a few problems:

        1. The constructor doesn't use the String value passed in, but instead sets all temperatures to be of type "a".

        2. Equal function. Where are you getting y from?
        You want to compare to another temperature, so change the declaration to include another Temp, eg:
        public double equal(Temp temp2)

        Same problem in your greater and smaller functions.

        4. Your celsius and farenheit functions automatically assume that the temperature is the opposite format. This is not always the case!
        if you call farenheit and scale equals F, you should just return temperature, without any conversion.

        4. There's a couple ways to actually compare temperatures:
        A. Convert both temperatures to F
        B. Convert both temperatures to C

        I suggest making the conversion standard, regardless of what the output is.
        Eg, if you decide to always convert to C, and you're comparing 2 F temperatures, convert them both to C anyways.

        Try fixing these one at a time, and them come back if there are still problems.

        Comment

        • amistak
          New Member
          • Mar 2010
          • 3

          #5
          Wont I need to declare more things in my contructor and instance variables if I am going to compare more than one temperature?

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            Not really, all you need are the numeric temperature and the scale you're using, and you already have that.

            If you're comparing multiple temperatures, you have multiple instances of the class, eg
            Code:
            Temp boilingF = new Temp(212,"F")
            Temp freezingC = new Temp(0, "C")
            boolean equalTemp = boilingF.equal(freezingC);

            Comment

            Working...