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

Write a java class encapsulating the concept of daily temperatures for a week. W

ID: 3805106 • Letter: W

Question

Write a java class encapsulating the concept of daily temperatures for a week. Write the following methods: A constructor accepting an array of seven temperatures as a parameter. Accessor, mutator, toString() and equals() methods A method returning how many temperatures were below freezing. A method returning an array of temperatures above 100 degrees. A method returning the largest change in temperature between any two consecutive days. A method returning an array of daily temperatures, sorted in descending order (modify selectionSort() in Sorter.java). Write a client class to test all the methods in your class.

Explanation / Answer

HI, Please find my implementation.

Please let me know in case of any issue.

import java.util.Arrays;

public class TemperatureMethods {

   private double temp[];

   // constructor

   public TemperatureMethods(double t[]) {

       temp = new double[t.length];

       for(int i=0; i<temp.length; i++)

           temp[i] = t[i];

   }

   public double[] getTemperatires(){

       return temp;

   }

   public void setTemperatures(double[] t){

       temp = t;

   }

   @Override

   public String toString() {

       return Arrays.toString(temp);

   }

   @Override

   public boolean equals(Object obj) {

       if(obj instanceof TemperatureMethods){

           TemperatureMethods other = (TemperatureMethods)obj;

           for(int i=0; i<temp.length; i++)

               if(temp[i] != other.temp[i])

                   return false;

           return true;

       }

       return false;

   }

   public int getCountBelowFreezing(){

       int count = 0;

       for(int i=0; i<temp.length; i++)

           if(temp[i] < 0)

               count++;

       return count;

   }

   public int getCountAbove100(){

       int count = 0;

       for(int i=0; i<temp.length; i++)

           if(temp[i] > 100)

               count++;

       return count;

   }

   public double largestChange(){

       double largest = temp[1]-temp[0];

       for(int i=2; i<temp.length; i++){

           if(largest < (temp[i]-temp[i-1]))

               largest = temp[i] - temp[i-1];

       }

       return largest;

   }

   public String descendingOrder(){

       double newArr[] = new double[temp.length];

       for(int i=0; i<temp.length; i++)

           newArr[i] = temp[i];

      

       for (int i = 0; i < newArr.length - 1; i++)

       {

           int index = i;

           for (int j = i + 1; j < newArr.length; j++)

               if (newArr[j] < newArr[index])

                   index = j;

           double smallerNumber = newArr[index];

           newArr[index] = newArr[i];

           newArr[i] = smallerNumber;

       }

       return Arrays.toString(newArr);

   }

}

###################

public class TempreratureTest {

   public static void main(String[] args) {

      

       double temp[] = {43, -43, 34, 1, 102, 121, 43};

       TemperatureMethods t1 = new TemperatureMethods(temp);

      

       System.out.println(t1.toString());

       System.out.println(t1.descendingOrder());

      

       System.out.println(t1.getCountAbove100());

       System.out.println(t1.getCountBelowFreezing());

       System.out.println(t1.largestChange());

   }

}

/*

Sample run:

[43.0, -43.0, 34.0, 1.0, 102.0, 121.0, 43.0]

[-43.0, 1.0, 34.0, 43.0, 43.0, 102.0, 121.0]

2

1

101.0

*/

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