Question: Complete the provided Temperature class. Add any attributes and helper methods as needed. You must complete the constructors and methods in the provided class (without

Complete the provided Temperature class. Add any attributes and helper methods as needed. You must complete the constructors and methods in the provided class (without changing any signatures, return types, or modifiers).

In this problem you will need to be able to convert temperatures between Celsius, Fahrenheit and Kelvin. For help, see https://en.wikipedia.org/wiki/Conversion_of_units_of_temperatureA temperature object holds a single temperature and displays it in one of the three scales. Once a scale has been set, it will display the temperature in that scale until changed. The default scale

is Celsius if not specified. Examples:

Temperature t = new Temperature(10.1); 
System.out.println(t.getScale()); System.out.println(t); t.setScale("F"); System.out.println(t); System.out.println(t.getScale()); 
// outputs the char 'C' // outputs 10.1C 
// outputs 50.18F // outputs the char 'F' 

When you set a temperature (without explicitly stating the scale), whatever scale is currently used by the object will be used.

Note: Repeatedly changing the scale should not change the value of the temperature. For example,

Temperature t = new Temperature(10.1); System.out.println(t); for(int i=0; i<10000; i+=1){ 
 t.setScale("F"); 
 t.setScale("C"); } 
System.out.println(t); 

Should print out identical strings.

Note: You should have no static attributes or methods in your class (unless they were supplied in the starter code).

Note: Your code must use encapsulation. Your grade will be reduced by 5 marks if you do not use encapsulation.

COMP1006/1406 - Summer 2018 1

Assignment #2 Due Monday, July 23 at 2:00 pm 2: Extreme Temperatures

Complete the MaxTemp class. The class consists of a single constructor and a single getter method.

The constructor takes an array of Temperature objects as input. The input will always be a

reference to an array (and never null).

The getter method returns an array of doubles with exactly two doubles in it. If the object was created with one more more Temperature objects in the constructors input array then the output consists of the maximum temperature of all Temperature objects passed to the constructor and a count of how many times that maximum was present in the array passed to the constructor (in that order). If zero Temperature objects were passed to the constructor (in the array) then the getter returns [0.0, 0.0].

Note: The max temperature returned must be displayed in the Kelvin scale. Note: Different Temperature objects in the array passed to the constructor may have different

temperature scales set for themselves.

Since the Temperature objects will store a floating point number for the temperature, you will use the provided EPSILON constant in the MaxTemp class and consider two temperatures as equal if their absolute difference is smaller than EPSILON. Therefore, if Math.abs(temp1 - temp2) < ESILON then temp1 and temp2 are considered equal (close enough to each other to be considered equal).

For example, if the array

 new Temperature[]{new Temperature(1001.12, "K"), new Temperature(-200.0, "F"), 
 new Temperature(1001.11, "K")} 

is passed to the constructor, and the tolerance is set to EPSILON = 0.1, then the getMax method will return [1001.12, 2.0].

The return value should still display the actual maximum temperature (in K). In the example above, even though we consider 1001.11 and 1001.12 the same (with the given EPSILON), 1001.12 is still the max to be returned.

Note: Your code must use encapsulation. Your grade will be reduced if you do not use encapsulation.

///// TEMPERATURE ////////

** * A Temperature object represents temperature (with a value and scale) * * COMP 1006/1406 * Summer 2018 * Assignment 2 */ public class Temperature{ /** different scale names */ public static String[] scales = {"Celsius", "Fahrenheit", "Kelvin"}; /** Initializes a temperature object with given value in Celcius * * If the initial temperature is less than -273.15 then the temperature * object will be initialized with -273.15C. * * @param temp is the initial temperature in Celsius. */ public Temperature(double temp){ } /** Initializes a temperature object with given value using the specified scale *  * If the temperature is lower than absolute zero, then set the temperature to * absolute zero (in whichever scale is specified). *  * Examples: new Temperature(12.3, "K") * new Temperature(-90.2, "Celsius") * * @param temp is the initial temperature * @param scale is the scale of initial temperature and must either be * one of the Strings in the scales array, or * the first letter (capitalized) of one of those strings. */ public Temperature(double temp, String scale){ } /** getter for the scale * * The output of this getter method must always be the first letter of one * of the strings in the scales array, capitalized. * * @return the current scale of the object as a single char (the first letter, * capitalized of one of the strings from scales) */ public char getScale(){ return 'X'; } /** getter for the temperature * * @return the temperature of the object using the current scale */ public double getTemp(){ return -Double.MAX_VALUE; } /** setter for scale * * * * @param scale is the new scale of the temperature and must either be * one of the Strings in the scales array, or * the first letter (capitalized) of one of those strings. */ public void setScale(String scale){ } /** setter for temperature * * @param temp is the new temperature (in the objects current scale) */ public void setTemp(double temp){ } /** setter for temperature * * * @param temp is the new temperature * @param scale is the scale of the new temperature. It must be * the first letter (capitalized) of one of the strings in * the scales array. */ public void setTemp(double temp, char scale){ } /** setter for temperature * * @param temp is the new temperature * @param scale is the scale of the new temperature. It must be one * of the strings in the scales array, or the first letter * (capitalized) of one of those strings. */ public void setTemp(double temp, String scale){ } /* ------------------------------------------------- */ /* ------------------------------------------------- */ /* do not change anything below this */ /* ------------------------------------------------- */ /* ------------------------------------------------- */ /** String representation of a temperature object */ @Override public String toString(){ return "" + this.getTemp() + this.getScale(); } }

///////// MAX TEMP ///////////

public class MaxTemp{ /** t1 and t2 are considered close enough if Math.abs(t1-t2) < EPSILON */ public static final double EPSILON = 0.01; /* add attributes as you need */ /* ---------------------------------------------------- * constructor * ---------------------------------------------------- */ public MaxTemp(Temperature[] temperatures){ // add your code here } /* ---------------------------------------------------- * getter * ---------------------------------------------------- */ public double[] getMax(){ // - returns an array of length 2 [max, count] // where max is the maximum temperature (expressed in the Kelvin scale) // of all Temperature objects passed to the constructor, and count // is the number of times that temperature was present (in the input // array of the constructor) // If there are no temperatures then return the array [0.0, 0.0] return new double[]{0.1, 0.2, 0.3, 0.4}; } /* OPTIONAL - use your main method to test your code */ public static void main(String[] args){ // testing code here is optional } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!