So I have to make the Java rainfall program. The thing is my professor wants me to submit it using myprogramminglab which is extremely sensitive if you don't get exactly the same output of mpl it gets marked wrong all the down to the spacing. I've got most of the program done only thing really i'm having trouble with is outputting the largest month and smallest month as a String and the input validation.
Here's the assignment:
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 stringDemonstrate 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.
Here's the output mpl wants the floating periods represent spaces and the arrows
newlines:
Enter·rainfall·for·month·1:1.12↵
Enter·rainfall·for·month·2:2.24↵
Enter·rainfall·for·month·3:3.36↵
Enter·rainfall·for·month·4:0.48↵
Enter·rainfall·for·month·5:2.00↵
Enter·rainfall·for·month·6:5.00↵
Enter·rainfall·for·month·7:12.12↵
Enter·rainfall·for·month·8:1.48↵
Enter·rainfall·for·month·9:3.12↵
Enter·rainfall·for·month·10:3.24↵
Enter·rainfall·for·month·11:6.6↵
Enter·rainfall·for·month·12:1.12↵
41.88↵
3.49↵
July↵
April↵
And here's my code
public class rainFall
{
Scanner input = new Scanner(System.in);
String rain[] = {"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "November", "December"};
static double sum = 0;
static double avg = 0;
int m = 1;
double numbers = 0;
public void setSum()
{
while(m < 13)
{
System.out.print("Enter rainfall for month " + m + ":");
numbers = input.nextDouble();
sum+= numbers;
m++;
}
}
public double getSum()
{
return sum;
}
public void setAverage()
{
avg = sum / 12;
}
public double getAverage()
{
return avg;
}
public String getLargest()
{
String largest = rain[0];
int r = 0;
for(int i = 0; i < numbers; i++)
{
if(rain[i] > numbers)
{
r = i;
}
}
return r;
}
public static void main(String[] args) {
rainFall rf = new rainFall();
rf.setSum();
rf.setAverage();
System.out.println(rf.getSum());
System.out.println(rf.getAverage());
}
}
My only errors are bad operands types at the if statement and the return statement in getLargest()