import java.text.*; //  needed for DecimalFormat class: p1027 of 4th Ed

/**
 Temperature.java
 Class for temperature objects
 Ref: #7,413,4th 
 AHD, Thurs 18th October 2007, 11:10 PT
 Please report any errors or omissions in this file.
 
 This is a class designed primarily to represent temperature
 records. It allows default initialisations of variables, get
 and set methods for variables, temperature comparison and
 conversion methods.
 
 
 
 
 
 
 To help with the understanding of the class, this java code
 has been commented for javadoc.exe, p1034-1035,4th Ed
 following the style in SavitchIn.java
 
 javadoc.exe has been run on this file (see instructions below)
 to create a web page documenting the Temperature.java class.
 You can inspect this webpage at:
 http://www.annedawson.com/Temperature.html
 
 
 To run javadoc, assuming the Java SDK is located here:
 C:\j2sdk1.4.2_01
 and assuming the Temperature.java file is in C:\temp
 Click on the Start button on the desktop
 Select Run
 Browse for this file:
 C:\j2sdk1.4.2_01\bin\javadoc.exe
 
 and in the Run textbox add the text exactly as here:
 C:\j2sdk1.4.2_01\bin\javadoc.exe -d c:\temp C:\temp\Temperature.java
 and click on OK to run

 ********************************************************************************
 IMPORTANT NOTE: if you installed java to the Program Files folder,
 for "Program Files" substitute "Progra~1" in the path, e.g.:
 C:\Progra~1\Java\jdk1.6.0_02\bin\javadoc.exe -d c:\temp C:\temp\Temperature.java
 ********************************************************************************
 
 
 the "-d c:\temp" is a javadoc flag specifying that the destination
 folder to write the html documentation files to is C:\temp
 
 when you supply a .java file name to javadoc, you must give the full path,
 e.g. C:\temp\Temperature.java
 
 Reference:
 www.annedawson.com/javadoc.htm
*/

public class Temperature
{
    private char scale; //'C' for celsius, 'F' for fahrenheit
    private double temperature; // any real number in double range
    
    /**
     Default Constructor: sets temperature to O and scale to 'C'
    */
    public Temperature( ) // default constructor
    {
        temperature = 0;
        scale = 'C';
    }


    /**
      Constructor: initialises temperature to parameter t
      @param t This is a number representing the temperature 
    */
	public Temperature(double t) // overloaded constructor
    {
        temperature = t;
        scale = 'C';
    }


    /**
      Constructor: initialises scale to parameter s
      @param s This is a character representing the scale
    */
    public Temperature(char s) // overloaded constructor
    {
        temperature = 0;
        scale = s;
    }
    
    
    /**
      Constructor: initialises scale to parameter s
      and temperature to t
      @param s This is a character representing the scale
      @param t This is a number representing the temperature 
    */
    public Temperature(double t, char s) // overloaded constructor
    {
        temperature = t;
        scale = s;
    }

  
    /**
      Accessor method: gets the C temperature value
      @return The temperature in Celcius
    */
    public double getCelsiusTemperature() // accessor method
    {
        if (scale == 'C')
           return temperature;
        else 
           return (convertToC(temperature));
    }

    
    /**
      Accessor method: gets the F temperature value
      @return The temperature in Fahrenheit
    */
    public double getFahrenheitTemperature() // accessor method
    {
        if (scale == 'F')
           return temperature;
        else 
           return (convertToF(temperature));
    }

    
    /**
      General method: writes the object's values to screen
    */
    public void writeOutput( ) // a method
    {
    	DecimalFormat oneDigitPastPoint = new DecimalFormat("0.0");
        System.out.println("Temperature: " + oneDigitPastPoint.format(temperature));
        System.out.println("Scale: " + scale);
        System.out.println();
    }


    /**
      Mutator method: sets the object's temperature value with param
      @param newTemp This is the new temperature.
    */
    public void set(double newTemp) // a mutator method
    {
        temperature = newTemp;
        
    }
 

	/**
      Mutator method: sets the object's scale value with param
      @param newScale This is the new scale.
    */
    public void set(char newScale) // a mutator method
    {
        scale = newScale;
        
    }

 
    /**
      Mutator method: sets the object's scale and temperature values with params
      @param newTemp This is the new temperature.
      @param newScale This is the new scale.
    */
    public void set(double newTemp, char newScale) // a mutator method
    {
        temperature = newTemp;
        scale = newScale;
    }
    
    
    // for an example of an equals method
    // see pages 275-279 of 4th Ed textbook
    
    /**
      General method: checks if objects have equal temperatures
      @param otherObject This is the object to compare to the current object
    */
    public boolean equals(Temperature otherObject)
    {
    	if (this.scale == otherObject.scale)
    		return (this.temperature == otherObject.temperature);	
    	
    	// if execution reaches here, we know that the temperature
    	// scales of the two objects are different, so we
    	// need to convert before comparing...	
    	if (this.scale == 'C')
    	{
    		// the other object's scale is Fahrenheit,
    		// so we convert this object's temperature
    		// to Fahrenheit:
    		
    		double C = this.temperature;
    		double thisF = convertToF(C);
    		return (thisF == otherObject.temperature);
     	}
    	else
    	{
    		// the other object's scale is Celsius,
    		// so we convert this object's temperature
    		// to Celsius:
    		double F = this.temperature;
    		double thisC = convertToC(F);;
    		return (thisC == otherObject.temperature);
    	} 
    }

    
    /**
      General method: converts C to F temperature value
      @return The temperature in Fahrenheit
    */
	private double convertToF(double C) // this is a helper method so private
    {
    	return ((9.0 * C / 5.0) + 32.0);// this helper method can only be used	
	}                                   // by methods of this class


	/**
      General method: converts F to C temperature value
      @return The temperature in Celcius
    */
	private double convertToC(double F) // this is a helper method so private
    {
    	return (5.0 * (F - 32.0) / 9.0);// this helper method can only be used		
	}                                   // by methods of this class


    /**
      General method: checks if temperature of current object is
      greater than that of other object
      @param otherObject This is the object to compare to the current object
    */
    public boolean greaterThan(Temperature otherObject)
    {
    	if (this.scale == otherObject.scale)
    		return (this.temperature > otherObject.temperature);	
    	
    	// if execution reaches here, we know that the temperature
    	// scales of the two objects are different, so we
    	// need to convert before comparing...	
    	if (this.scale == 'C')
    	{
    		// the other object's scale is Fahrenheit,
    		// so we convert this object's temperature
    		// to Fahrenheit:
    		
    		double C = this.temperature;
    		double thisF = convertToF(C);
    		return (thisF > otherObject.temperature);
     	}
    	else
    	{
    		// the other object's scale is Celsius,
    		// so we convert this object's temperature
    		// to Celsius:
    		double F = this.temperature;
    		double thisC = convertToC(F);;
    		return (thisC > otherObject.temperature);
    	} 
    }

  
    /**
      General method: checks if temperature of current object is
      less than that of other object
      @param otherObject This is the object to compare to the current object
    */
    public boolean lessThan(Temperature otherObject)
    {
    		if (this.scale == otherObject.scale)
    		return (this.temperature < otherObject.temperature);	
    	
    	// if execution reaches here, we know that the temperature
    	// scales of the two objects are different, so we
    	// need to convert before comparing...	
    	if (this.scale == 'C')
    	{
    		// the other object's scale is Fahrenheit,
    		// so we convert this object's temperature
    		// to Fahrenheit:
    		
    		double C = this.temperature;
    		double thisF = convertToF(C);
    		return (thisF < otherObject.temperature);
     	}
    	else
    	{
    		// the other object's scale is Celsius,
    		// so we convert this object's temperature
    		// to Celsius:
    		double F = this.temperature;
    		double thisC = convertToC(F);;
    		return (thisC < otherObject.temperature);
    	} 
	}
}

    
