In Java please, ! 8. Write your own class called WeatherStats to manage the orde
ID: 3855370 • Letter: I
Question
In Java please, !
8. Write your own class called WeatherStats to manage the ordered collection
of high temperatures. Have two constructors, one for a default maximum size
collection of 10, and the second for a user argument defined maximum size
collection. The user class will not do any prompting to the user or reading
of data, instead create the following methods:
• boolean addNextTemp (double temp) – add the temp to the next available
position in the collection, if room allows.
• getMaxDifference() – returns the maximum change in temperature (in
absolute value) on two consecutive days.
• getMaxDiffDay() – returns the day number for the second day of the two
consecutive days that the maximum change in temperature (in absolute value)
occurred.
Explanation / Answer
Here is the class for you:
class WeatherStats
{
double[] highTemperatures;
int capacity;
int currentSize;
//Have two constructors, one for a default maximum size collection of 10,
//and the second for a user argument defined maximum size collection.
WeatherStats()
{
capacity = 10;
highTemperatures = new double[capacity];
currentSize = 0;
}
WeatherStats(int size)
{
capacity = size;
highTemperatures = new double[capacity];
currentSize = 0;
}
//boolean addNextTemp (double temp) – add the temp to the next available
//position in the collection, if room allows.
boolean addNextTemp(double temp)
{
if(currentSize < capacity)
{
highTemperatures[currentSize++] = temp;
return true;
}
return false;
}
//getMaxDifference() – returns the maximum change in temperature (in
//absolute value) on two consecutive days.
double getMaxDifference()
{
double maxDiff = Math.abs(highTemperatures[0] - highTemperatures[1]);
for(int i = 1; i < currentSize-1; i++)
if(Math.abs(highTemperatures[0] - highTemperatures[1]) > maxDiff)
maxDiff = Math.abs(highTemperatures[i] - highTemperatures[i+1]);
return maxDiff;
}
//getMaxDiffDay() – returns the day number for the second day of the two consecutive
//days that the maximum change in temperature (in absolute value) occurred.
int getMaxDiffDay()
{
int maxDiffDay = 1;
double maxDiff = getMaxDifference();
for(int i = 0; i < currentSize-1; i++)
if(Math.abs(highTemperatures[i] - highTemperatures[i+1]) == maxDiff)
return i+1;
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.