Question: 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
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.
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
Get step-by-step solutions from verified subject matter experts
