#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    43
    Rep Power
    3

    Temperature converter program, need help?


    This program should be able to do the following conversions:
    Celcius-Fahrenheit
    Celcius-Kelvin
    Kelvin-Celcius
    Kelvin-Fahrenheit
    Fahrenheit-Kelvin
    Fahrenheit-Celcius

    However, in the methods FAH() I dont know how to implement if it is from F to C or from F to K. Same for KEL() and CEL().

    Thanks for the help.

    Code:
    import java.lang.Object;
    /**
    
     * We want to use the Java collections library to sort 
     * a collection of temperatures. Therefore, the {@code Temperature} 
     * class implements the {@code Comparable} interface by overriding 
     * {@code compareTo} method.
     */
    
    public class Temperature implements Comparable<Temperature> {
    
    	double value;
    	Units units;
    	
      /** Enumerator for different temperature units */
      public static enum Units { FAHRENHEIT, CELCIUS, KELVIN }
    
      /**
       * Create a new {@code Temperature} with given attributes
       * @param value numerical value of {@code Temperature}
       * @param units {@code Units} of {@code Temperature}
       */
      public Temperature (double value, Temperature.Units units) {
           // ...
    	  this.value = value;
    	  this.units = units;
      }
    
      /**
        * Get the value of the {@code Temperature}
        * @return numerical value of the {@code Temperature} in the current {@code Units}
        */
      public double getValue() { 
        // ...
    	  return value;
      }
    
      /**
       * Get the current {@code Units} of the {@code Temperature}
       * @return current {@code Units} of {@code Temperature}
       */
      public Units getUnits() {
        // ...
    	 
    	  return units;
      }
    
      /**
       * Change the current {@code Units} of the {@code Temperature}. 
       * Changing the {@code Units} also changes the numerical value 
       * in a consistent manner.
       * @param units the new {@code Units} 
       */
      
      public double FAH(){
    	  double value1 = getValue();
    	  
              //from C to F
             value = (value1*(9/5)) + 32; 
    	  
    	 //from K to F
    	value = (value1 * (9/5)) - 459.67;
    	 
    	return value;
    	  
      }
      
      public double CEL(){
    	  double value2 = getValue();
    	  
              //from F to C
    	  value = (value2 - 32)*(5/9); 
    	  
    	 //from K to C
    	value = value2 - 273.15;
    	return value;
    	
      }
      
      public double KEL(){
    	  double value3 = getValue();
    	  
              //from F to K
    	  value= (value3 + 459.67)*(5/9) ;	 
    	  
    	   //from C to K
               value = value3 + 273.15;
    	  
    	return value;
    	  
      }
      
      
      public void changeUnits(Units units) {
        // ...converting
    	  
    	  if(units == Units.FAHRENHEIT) 
    		  FAH();
    		
    	  if(units == Units.CELCIUS) 
    		  CEL();
    		 
    	  if(units == Units.KELVIN)
    		  KEL();
    	  
      }
      
    
      /** 
       * Convert the {@code Temperature} to {@code String}. The output is
       * as follows
       * <pre><code>
       *    Temperature temperature = new Temperature(0, Units.CELCIUS);
       *    System.out.println(temperature.toString()); // prints "0 C"
       *    temperature.changeUnits(Units.FAHRENHEIT);
       *    System.out.println(temperature.toString()); // prints "32 F"
       *    temperature.changeUnits(Units.KELVIN);
       *    System.out.println(temperature.toString()); // prints "273.15 K"
       * </code></pre>
       */
      public String toString() {
        // ...
        
    	
    	return "Temperature is: " + value + units;
      }
    
      /**
       * In order to implement {@code Comparable}, we need to override
       * the {@code compareTo} method. 
       * @param temperature The {@code Temperature} to compare against
       * @return -1 if current object is less than {@code temperature}
       *          0 if both are equal
       *          1 if current object is greater than {@code temperature}
       */
      @Override
      public int compareTo(Temperature temperature) {
        // ...
    	
    	  //if(equals(temperature)) return 0;
    	  if(temperature == null) throw new NullPointerException();
    	  if(value < temperature.value){
    		  return -1;
    	  }else{ 
    		  if(value == temperature.value)
    		  return 0;
    	  }{
    		  return 1;
    	  }
    	 
      }
    			 
    
      /**
       * Indicates whether some object is "equal" to this one.
       * To maintain consistency, whenever a class overrdes 
       * {@code compareTo}, it must override {@code equals} so 
       * that
       * <pre>
       *   <code>o1.compareTo(o2) == 0</code> implies <code>o1.equals(o2) == true</code>
       * </pre>
       * See the API documentation of {@code Object} class for more details.
       */
      @Override
      public boolean equals (Object o) {
        // ...
    	//testing for reflexive ,symmetric,transitive,consistent,null
    	  if(this == o) return true; 
    	  if((o == null) || (o.getClass() != this.getClass())) return false;
    	  Temperature test = (Temperature)o;
    	  return value == test.value && (units == test.units || (units != null && units.equals(test.units)));
      }
    
      /**
       * Return a hash code of the object. To maintain consistency,
       * whenever a class overrides {@code equals} it mush also override
       * {@code hashCode} in such a manner that 
       * <pre>
       *   <code>o1.equals(o2) == true</code> implies <code>o1.hashCode() == o2.hashCode()</code>
       * </pre>
       * See the API documentation of {@code Object} class for more details.
       */
      @Override
      public int hashCode() {
        // ...
    	  int hash = 6;
    	  hash = (int) (28 * hash + value);
    	  hash = 28 * hash + (null == units ? 0 : units.hashCode());
    	  return hash;
    	  
      }
      
      
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Are all the conversions some math forumula? You need to look up the formulas using google and then if you have problems writing the code for any of them post the formula and your questions about your problems.
  4. #3
  5. No Profile Picture
    Choose Life...
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2006
    Location
    Scotland
    Posts
    461
    Rep Power
    513
    Think you need to be a bit more specific about what you are having trouble with...

IMN logo majestic logo threadwatch logo seochat tools logo