Object-Oriented Programming/Validation/Java

From Wikiversity
Jump to navigation Jump to search

main.java[edit]

/**This program demonstrates use of the Temperature class.**/

public class Main{

    public static void main(String[] args){
        Temperature temp = new Temperature(32.0, Temperature.temp_type.C);
        System.out.println(temp.get_farenheit());
        System.out.println(temp.get_celcius());

        Temperature temp1 = new Temperature(13.0, Temperature.temp_type.C);
        System.out.println(temp1.get_farenheit());
        System.out.println(temp1.get_celcius());

        Temperature temp2 = new Temperature(85.5, Temperature.temp_type.F);
        System.out.println(temp2.get_farenheit());
        System.out.println(temp2.get_celcius());

        Temperature temp3 = new Temperature(32.0, Temperature.temp_type.F);
        System.out.println(temp3.get_farenheit());
        System.out.println(temp3.get_celcius());

        Temperature temp4 = new Temperature(-30.0, Temperature.temp_type.C);
        System.out.println(temp4.get_farenheit());
        System.out.println(temp4.get_celcius());

        Temperature temp5 = new Temperature(-280, Temperature.temp_type.C);
    }
}

Temperature.java[edit]

/**Temperature container / converter.

Examples:

Temperature temp = new Temperature(32.0, 'C');
System.out.println(temp.get_farenheit());
System.out.println(temp.get_celcius());

Temperature temp = new Temperature(13.0, 'C');
System.out.println(temp.get_farenheit());
System.out.println(temp.get_celcius());

Temperature temp = new Temperature(85.5, 'F');
System.out.println(temp.get_farenheit());
System.out.println(temp.get_celcius());

Temperature temp = new Temperature(32.0, 'F');
System.out.println(temp.get_farenheit());
System.out.println(temp.get_celcius());

Temperature temp = new Temperature(-30.0, 'C');
System.out.println(temp.get_farenheit());
System.out.println(temp.get_celcius());

**/

public class Temperature {
    /**
    * The Temperature class takes a temperature and a metric
    * (celcius or farenheit) and can return either celcius or farenheit
    *
    * since Java does not natively support properties or optional parameters,
    * they have been implemented as a private setter method, public getter methods,
    * and a single char variable which indicates the type of temperature input.
    *
    * @author  Tristan Blus
    * @version 1.0
    * @since   2018-09-06
    **/
    public static final int TEMPERATURE_DIFFERENCE = 32;
    public static final double TEMPERATURE_RATIO = 5.0 / 9;

    private double temperature;
    private temp_type temp;
    private double celcius;
    private double farenheit;

    public enum temp_type{
        F, C
    }

    Temperature(double temperature, temp_type temp) {
        /**
        * The Temperature constructor takes temperature and metric and sets this.
        * celcius and this.farenheit by calling the set_temperatures method
        *
        * ARGS
        *   double temperature in either farenheit or celcius
        *   char metric: F or C for farenheit or celcius respectively
        *
        * RETURNS
        *   None
        **/
        this.temperature = temperature;
        this.temp = temp;

        this.set_temperatures(this.temperature);
    }

    private void set_temperatures(double temperature) {
        /**
         *SETTER The set_temperatures method sets both this,celcius and this.farenheit
         *
         *ARGS
         *  double temperature: temperature in either celcius or farenheit
         *  char metric: F or C for ferenheit or celcius respectively
         *
         *RETURNS
         *  None
         *
        **/
        double ABSOLUTE_ZERO_CELCIUS = -273.15;
        double ABSOLUTE_ZERO_FARENHEIT = -459.67;

        if (this.temp == temp_type.C) {
            try{
                if (this.validate(ABSOLUTE_ZERO_CELCIUS, temperature)== true){
                    this.celcius = this.temperature;
                    this.farenheit = to_farenheit(this.temperature);
                }
            }
            catch(IllegalArgumentException ex){
                System.out.println(ex.getMessage());
            }
        }
        else if (this.temp == temp_type.F) {
            try{
                if (this.validate(ABSOLUTE_ZERO_FARENHEIT, temperature)== true){
                    this.farenheit = this.temperature;
                    this.celcius = to_celcius(this.temperature);
                }
            }
            catch(IllegalArgumentException ex){
                System.out.println(ex.getMessage());
            }
        }
    }

    public double get_celcius() {
        /**
         * the get_celcius method returns the celcius temperature
         *
         * ARGS
         *  None
         *
         * RETURNS
         *  double celcius: the input temperature in celcius
         *
        **/
        return this.celcius;
    }

    public double get_farenheit() {
        /**
         * the get_celcius method returns the celcius temperature
         *
         * ARGS
         *  None
         *
         * RETURNS
         *  double farenheit: the input temperature in farenheit
         *
        **/
        return this.farenheit;
    }

    private double to_celcius(double value) {
        /**
        * the to_celcius method returns a farenheit temperature converted to
        * celcius
        *
        * ARGS
        *   double value: the farenheit temperature to be converted
        *
        * RETURNS
        *   double celcius: the converted temperature in celcius
        **/
        return (value - this.TEMPERATURE_DIFFERENCE) * this.TEMPERATURE_RATIO;
    }

    private double to_farenheit(double value) {
        /**
        * the to_farenheit method returns a celcius temperature converted to
        * farenheit
        *
        * ARGS
        *   double value: the celcius temperature to be converted
        *
        * RETURNS
        *   double farenheit: the converted temperature in farenheit
        **/
        return value / this.TEMPERATURE_RATIO + this.TEMPERATURE_DIFFERENCE;
    }

    private boolean validate(double min, double value){
        /**
        * the validate method returns a boolean indivating whether value is a
        * legitimate float variable between min and max
        *
        * ARGS
        *   double min: the minimum of the range of legitimate values
        *   double value: the value to be tested against max and min
        *
        * RETURNS
        *   boolean: True if value is between min and max
        **/
        if (value > min){
            return true;
        }
        else {
            throw new IllegalArgumentException("Value cannot be less than absolute zero");
        }
    }
}