Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the code IN JAVA of data type class RainFall: data member rainfallMonth si

ID: 3770544 • Letter: W

Question

Write the code IN JAVA of data type class RainFall: data member rainfallMonth size 12, constructors, and all the following methods • Total rainfall for the year • The average monthly rainfall • The month with the most rain • The month with the last rain Also, write the code of a driver class named RainFallMonitor that reads the rain amount of each month from the keyboard, stores into an array size 12. Create an object of class RainFall to pass this array in and use this array to display the result of total, average, the most and the last rain by calling methods from class RainFall.

Explanation / Answer

public class RainFall
{
private double[] rainArray = new double[SIZE];
public static final int SIZE = 12;
RainFall()
{
}
public RainFall(double[] r)
{
rainArray = new double[SIZE];
}
public void setArrayValueAtIndex( double Val, int Index)
{
if ( Index > SIZE - 1 )
return;
rainArray[Index] = Val;
}
public double getTotal()
{
double total = 0;
for ( int i = 0; i < SIZE; i++ )
{
total += rainArray[i];
}
return total;
}
public String getMonth( int monthIndex )
{
switch( monthIndex )
{
case 0 : return "January";
case 1 : return "February";
case 2 : return "March";
case 3 : return "April";
case 4 : return "May";
case 5 : return "June";
case 6 : return "July";
case 7 : return "August";
case 8 : return "September";
case 9 : return "Octobar";
case 10 : return "November";
case 11 : return "December";
default : return "Error";
}
}
public double getAverage()
{
return (getTotal() / SIZE);
}
public String getMost()
{
int mostRain = 0;
for ( int i = 1; i < SIZE; i++ )
{
if ( rainArray[i] > rainArray[mostRain] )
mostRain = i;
}
return getMonth( mostRain );   
}
public double getMostValue()
{
int mostRain = 0;
for ( int i = 1; i < SIZE; i++ )
{
if ( rainArray[i] < rainArray[mostRain] )
mostRain = i;
}
return rainArray[mostRain];
}
public String getLeast()
{
int leastRain = 0;
for ( int i = 1; i < SIZE; i++ )
{
if ( rainArray[i] < rainArray[leastRain] )
leastRain = i;
}
return getMonth( leastRain );
}
public double getLeastValue()
{
int leastRain = 0;
for ( int i = 1; i < SIZE; i++ )
{
if ( rainArray[i] < rainArray[leastRain] )
leastRain = i;
}
return rainArray[leastRain];
}
}

*********************

import java.util.Scanner;
public class RainFallDemo
{
public static void main(String[] args)
{
double rain;
Scanner keyboard = new Scanner(System.in);
RainFall rainfall = new RainFall();
for ( int i = 0; i < rainfall.SIZE; i++ )
{
System.out.print("Enter a value for " + rainfall.getMonth( i ) + ": " );
rain = keyboard.nextDouble();
rainfall.setArrayValueAtIndex( rain, i );
}
System.out.println("The total rainfall for this year is " + rainfall.getTotal());
System.out.println("The average rainfall for this year is " + rainfall.getAverage());
System.out.println("The month with the highest amount of rain is" + rainfall.getMost() + "with" + rainfall.getMostValue() + "inches.");
System.out.println("The month with the lowest amount of rain is" + rainfall.getLeast() + "with" + rainfall.getLeastValue() + "inches.");
}
}