Write a RainFall class that has the following field: • an array of doubles that
ID: 3766652 • Letter: W
Question
Write a RainFall class that has the following field:
• an array of doubles that stores the rainfall for each of the 12 months of
the year (where the first index corresponds with January, the second with
February, etc.)
The class should also have the following methods :
• a method that returns the total rainfall for the entire year
• a method that returns the average monthly rainfall for the year
• a method that returns the month with the most rain as a string
• a method that returns the month with the least rain as a string
Demonstrate the class in a program that takes 12 doubles from the user (take the
doubles in the order of the months of the year, the first corresponding to the
rainfall in January, etc.). Do input validation: if the user inputs a negative
number, ignore it and continue asking them for input until you have 12
nonnegative doubles .
Once the user has given you all 12 doubles , create an instance of the RainFall
class and call its methods , printing out the total rainfall, the average
monthly rainfall, the month with the most rain, and the month with the least
rain, each on a separate line.
Explanation / Answer
import java.util.*;
public class RainStats {
private TreeSet<RainFall> rainFallByMonth = new TreeSet<RainFall>( new Comparator<RainFall>() {
@Override
public int compare(RainFall o1, RainFall o2) {
return new Double(o1.getSize()).compareTo(o2.getSize());
}
});
public RainStats(List<RainFall> rainFallByMonth) {
this.rainFallByMonth.addAll( rainFallByMonth);
}
public double getTotalRainfallForTheYear() {
double totalRainFall = 0.;
for (RainFall rainFall : rainFallByMonth) {
totalRainFall += rainFall.getSize();
}
return totalRainFall;
}
// implement other simple methods like above
public RainFall getTheMonthWithTheMostRain() {
return rainFallByMonth.last();
}
public RainFall getTheMonthWithTheLeastRain() {
return rainFallByMonth.first();
}
public static void main(String[] args) {
List<RainFall> rainFallByMonth = new ArrayList<RainFall>(12);
//receive input data for all 12 months and create RainFallObject
for (int i = 1; i <= 12; i++) {
Scanner keyboard = new Scanner(System.in);
rainFallByMonth.add(new RainFall(i, keyboard.nextDouble()));
}
RainStats rainStats = new RainStats(rainFallByMonth);
RainFall highestRain = rainStats.getTheMonthWithTheMostRain();
RainFall leastRain = rainStats.getTheMonthWithTheLeastRain();
System.out.println("Highest Rain Fall Month: "+ highestRain.getMonthId() + ", RainFall: "+ highestRain.getSize());
System.out.println("Least Rain Fall Month: "+ leastRain.getMonthId() + ", RainFall: "+ leastRain.getSize());
}
private static class RainFall {
private int monthId;
private double size;
public RainFall(int monthId, double size) {
this.monthId = monthId;
this.size = size;
}
public int getMonthId() {
return monthId;
}
public double getSize() {
return size;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.