Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to write a java programm that could find the maximum temp from an ar

ID: 3915634 • Letter: I

Question

I am trying to write a java programm that could find the maximum temp from an array of temperatures and find out how many time that temp appare in the array, however, i am not quite sure how to use the constructor and the method to do the task. The programm has to work in this frame with out change any constructor and method. Java Se8

import java. util. Arrays public class MaxTemp t1 and t2 are considered close enough if Math abs (t1-t2)EPSILON public static final double EPSILON-0. 01 private double maxtemp private Temperature[] Temp; add attributes as you need constructor t/ public MaxTemp (Temperature[] temperatures) for (int 1-0, i temperatures. Îength:1+1){ Temperature T-new Temperature (i); T. setScale('K*); Temp [i] getter public double[] getMaxO -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 [o. 0, 0. 0] double result[] -new double [2] Temperature largest = Temp[0]; for(int i 0i Temp. 1ength;i+1) - new double em return new double [] f0. 1, 0.2, 0. 3, 0. 4) OPTIONAL-use your main method to test your code public static void main(Stringl] args) / testing code here is optional

Explanation / Answer

The function to be written is the getMax(). Rest of all the code looks quite straight forward. In the main also we just need to create an object of MaxTemp
and call the function getMax();The main code can be as follows:

public static void main(String[] args){

     Tempearture[] temp = new Temerature[10];
     Random rand = new Random();

     for (int i = 0; i<10; i++){
        double a = 1 + (50) * r.nextDouble();// just some ramdom valuues
        temp[i] = new Temperature(a,'K'); // Assuming Temprature class has constructor like that
     }
     MaxTemp m = new MaxTemp(temp);
     double[] d = m.getMax();
     System.out.println(d[0] + " " + d[1]);
}

Now coming to getMax function

public double[] getMax(){
   double[] result = new double[2];
   Temprature largest = new Tempearature(); //Assuming a default constructor
   largest.setValue(Temp[0].getValue()); // Assuming appropriate getters and setters are available
   for (int i = 0; i<Temp.length; i++){
       if (Temp[i] > largest) // Opeartors overloading is not there in java so
                              // this expression won't be allowed . We can
                             // make use of Comparable Interface for that but for
                             // we can just get the value of the Temperature object                      
                             //with appropriate getter fumction. may be
                             // getValue()

      if (Temp[i].getValue() > largest.getValue()){
          largest.setValue(Temp[i].getValue());
      }
   }
   result[0] = largest.getValue();
   double count = 0;
   for (int i = 0; i<Temp.length; i++){
       if (Temp[i].getValue() == result[0])
          count++;
   }
   result[1] = count;
   return result;

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote