can someone help me with this Write your own class called WeatherStats to manage
ID: 3851648 • Letter: C
Question
can someone help me with this
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
WeatherStats.java
public class WeatherStats
{
public static void main(String[] args)
{
temperature t1 = new temperature(5);
t1.addNextTemp(50);
t1.addNextTemp(60);
t1.addNextTemp(80);
t1.addNextTemp(90);
t1.addNextTemp(80);
t1.addNextTemp(70);
System.out.println( "maximum change in temperature " + t1.getMaxDifference());
System.out.println("day number when maximum change in temperature occurred " + t1.getMaxDiffDay());
}
}
temperature.java
public class temperature
{
public double[] tempe ;
int entries;
public temperature(int n) {
this.tempe = new double[n];
this.entries = 0;
}
public temperature() {
this.tempe = new double[10];
this.entries = 0;
}
public boolean addNextTemp(double t)
{
if(this.entries < this.tempe.length)
{
this.tempe[entries] = t;
this.entries = this.entries + 1;
return true;
}
else
{
System.out.println("No position available in collection");
return false;
}
}
public double getMaxDifference()
{
double maxdiff = 0;
for (int i = 1; i<this.tempe.length ; i++ ) {
if(java.lang.Math.abs(tempe[i] - tempe[i-1]) >= maxdiff)
{
maxdiff = java.lang.Math.abs(tempe[i] - tempe[i-1]);
}
}
return maxdiff;
}
public int getMaxDiffDay()
{
double maxdiff = 0;
int index = 1;
for (int i = 1; i<this.tempe.length ; i++ ) {
if(java.lang.Math.abs(tempe[i] - tempe[i-1]) >= maxdiff)
{
maxdiff = java.lang.Math.abs(tempe[i] - tempe[i-1]);
index = i;
}
}
return index + 1;
}
}
Sample Output:
No position available in collection
maximum change in temperature 20.0
day number when maximum change in temperature occurred 3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.