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

public class DailyTemps
{
public final int DAYS = 7; //default number of days
private int [] dayTemps;
  
//Default Constructor
//Creates daily temps with DAYS elements
public DailyTemps()
{
dayTemps = new int [DAYS];
}
  
//Constructor
//param temps array of DAYS temps
public DailyTemps ( int [] temps )
{
//instantiate array with same length as parameter
dayTemps = new int [temps.length];
  
//copy paramter array to dailBills array
for ( int i = 0; i < dayTemps.length; i++ )
{
dayTemps[i] = temps[i];
}
}
//Accessor method
public int [] getDayTemps()
{
int [] tempHolder = new int [ dayTemps.length ];
for ( int i = 0; i < dayTemps.length; i ++ )
{
tempHolder [i] = dayTemps [i];
}
return tempHolder;
}
//Mutator method
public void setDayTemps( int[] newDayTemps)
{
dayTemps = newDayTemps;
}
//toString method
public String toString ()
{
String returnDayTemps = "Day temps: ";
for ( int i = 0; i < dayTemps.length; i++ )
returnDayTemps += dayTemps[i] + " ";
  
return returnDayTemps;
  
}
//equals method
public boolean equals( Object o )
{
if ( ! ( o instanceof DailyTemps ) )
return false;
else
{
DailyTemps g2 = (DailyTemps) o;
  
if ( dayTemps.length != g2.dayTemps.length )
return false;
  
for ( int i = 0; i < dayTemps.length; i++ )
{
if ( dayTemps[i] != g2.dayTemps[i] )
return false;
}
  
return true;
}
  
}
  
}

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