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: 3802005 • 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

DailyTemperature.java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;


public class DailyTemperature {

   private int[] tempList;
  
   public DailyTemperature(int[] temperatures) {
       this.tempList = temperatures;
   }

   public int[] getTempList() {
       return tempList;
   }

   public void setTempList(int[] tempList) {
       this.tempList = tempList;
   }
  
   public List<Integer> tempBelowFreezing(){
       List<Integer> list = new ArrayList<Integer>();
      
       for(int iCount=0;iCount<tempList.length;iCount++){
           if(tempList[iCount] < 0){
               list.add(tempList[iCount]);
           }
       }
      
       if(list.size()>0)
           return list;
       else
           return null;
      
   }
  
   public List<Integer> tempAboveHundred(){
       List<Integer> list = new ArrayList<Integer>();
      
       for(int iCount=0;iCount<tempList.length;iCount++){
           if(tempList[iCount] > 100){
               list.add(tempList[iCount]);
           }
       }
      
       if(list.size()>0)
           return list;
       else
           return null;
      
   }
  
   public int tempDiffBetweenConsDays(){
      
       int maxDiff = 0;
      
       for(int iCount=0;iCount<tempList.length;iCount++){
           if(iCount+1 == tempList.length)
               continue;
          
           if(tempList[iCount] > tempList[iCount+1]){
               if(tempList[iCount] - tempList[iCount+1] > maxDiff)
                   maxDiff = tempList[iCount] - tempList[iCount+1];
           }else{
               if(tempList[iCount+1] - tempList[iCount] > maxDiff)
                   maxDiff = tempList[iCount+1] - tempList[iCount];
           }
       }
      
       return maxDiff;
   }

   public List sortedDesc(){
       int[] sortedData = tempList;
       Integer[] objectArray = new Integer[sortedData.length];

       for(int ctr = 0; ctr < sortedData.length; ctr++) {
       objectArray[ctr] = Integer.valueOf(sortedData[ctr]); // returns Integer value
       }
      
       Comparator<Integer> comparator = new Comparator<Integer>() {

       @Override
       public int compare(Integer o1, Integer o2) {
       return o2.compareTo(o1);
       }
       };
      
       Arrays.sort(objectArray, comparator);
      
         
      
       return Arrays.asList(objectArray);
      
   }
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------

DailyTempClient.java

public class DailyTempClient {

  
   public static void main(String[] args) {
      
       int[] temps = {101,-100,-6,110,50,0,-10};
      
       DailyTemperature dailyTemp = new DailyTemperature(temps);
      
       System.out.println("Temperatures below freezing temperature : "+dailyTemp.tempBelowFreezing());
      
       System.out.println("Temperatures above hundred degree : "+dailyTemp.tempAboveHundred());
      
       System.out.println("Highest difference in temperatures between consecutive days : "+dailyTemp.tempDiffBetweenConsDays());
      
       System.out.println("Temperatures sorted in descending order : "+dailyTemp.sortedDesc());

   }

}

----------------------------------------------------------------------------------------------------------------------------------------------------------------

Output

Temperatures below freezing temperature : [-100, -6, -10]
Temperatures above hundred degree : [101, 110]
Highest difference in temperatures between consecutive days : 201
Temperatures sorted in descending order : [110, 101, 50, 0, -6, -10, -100]

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