public class MaxTemp{ /** t1 and t2 are considered close enough if Math.abs(t1-t
ID: 3916648 • Letter: P
Question
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 } } 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 } } 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 i it. If the object was created with one more more Temperature objects in the constructor's 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 (tempi temp2)
Explanation / Answer
here is your code : ------------->>>>>>>>>
public class MaxTemp{
/** t1 and t2 are considered close enough if Math.abs(t1-t2) < EPSILON */
public static final double EPSILON = 0.1;
private Temperature[] temperatures;
/* add attributes as you need */
/* ----------------------------------------------------
* constructor
* ----------------------------------------------------
*/
public MaxTemp(Temperature[] temperatures){
// add your code here
this.temperatures = temperatures;
}
/* ----------------------------------------------------
* 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]
double[] res = new double[2];
res[0] = -2000000000;
res[1] = 0;
for(int i = 0;i<temperatures.length;i++){
temperatures[i].setScale("K");
if(res[0] < temperatures[i].getTemp()){
res[0] = temperatures[i].getTemp();
}
}
for(int i = 0;i<temperatures.length;i++){
if(Math.abs(res[0] - temperatures[i].getTemp()) <= MaxTemp.EPSILON){
res[1] = res[1] + 1;
}
}
return res;
}
/* OPTIONAL - use your main method to test your code */
public static void main(String[] args){
// testing code here is optional
Temperature[] temps = new Temperature[]{new Temperature(1001.12,"K"),
new Temperature(-200.0,"F"),
new Temperature(1001.11,"K")};
MaxTemp mt = new MaxTemp(temps);
double[] tt = mt.getMax();
System.out.println("["+tt[0]+", "+tt[1]+"]");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.