Java Program Write a RainFall class that has the following f

Java Program

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.

The expected output has to look identical to this when ran:

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

This is the code I have:

import java.util.Scanner;
public class RainFall {
private static Scanner keyboard;
private static double leastRain;

public static void main(String[] args) {
keyboard = new Scanner(System.in);
  
final int SIZE = 12;
double[] monthlyRain = new double[SIZE];
String []months={\"January\", \"February\", \"March\", \"April\", \"May\", \"June\", \"July\", \"August\", \"September\", \"October\", \"November\", \"December\"};
System.out.println(\"Enter the monthly rain fall for month: \");
int i = 0;
  
// Added input validation with \'Do-While Loop\'
for(i = 0; i < SIZE; i++){
  
do{
System.out.print(\"Month \" + (i+1) + \": \");
monthlyRain[i] = keyboard.nextDouble();
}
while(monthlyRain[i] < 0);
  
}

System.out.println(\" \" + sumRain(monthlyRain));
System.out.println(\" \" + averageRain(monthlyRain));
double mostRain=mostRain(monthlyRain);
leastRain = leastRain(monthlyRain);
System.out.print(\" \");
for(i=0;i<12;i++)
{
if(monthlyRain[i]==mostRain)
System.out.print(\" \"+months[i]);
}
System.out.print(\" \");
for(i=0;i<12;i++)
{
if(monthlyRain[i]==leastRain)
System.out.print(\" \"+months[i]);
}
}
  
// Create a function that returns the total rainFall for the year.
  
static double sumRain(double[] array){
double sum = 0;
for(int i = 0; i < array.length; i++){
sum += array[i];
}
return sum;
}
  
// Create a function that returns the average monthly rainFall for the year.
static double averageRain(double[] array){
double average = 0;
average = sumRain(array)/array.length;
return average;
}
  
// Create a function that returns the month with the most rain.
static double mostRain(double[] array){
double most = 0;
for (int i = 0; i < array.length; i++){
if(array[i] > most){
most = array[i];
}
}
return most;
}
  
// Create a function that returns the month with the least rain.
static double leastRain(double[] array){
double least = array[0];
for(int i = 0; i < array.length; i++){
if(array[i] < least){
least = array[i];
}
}
return least;
}
  
}

Solution

The code goes as follows:

import java.util.*;
import java.text.DateFormatSymbols;

public class RainFall
{
private double[] RainFall = null;
//double RainFall[] = {1.12,2.24,3.36,.48,2,5,12.12,1.48,3.12,3.24,6.6,1.12};
public static void main(String[] args)
{

Scanner stdin = new Scanner(System.in);   

double[] rainfallInput = {1.12,2.24,3.36,.48,2,5,12.12,1.48,3.12,3.24,6.6,1.12};

/*for(int i=1;i<=12;i++)
{
System.out.print(\"Enter rainfall for month \" + i + \":\");
rainfallInput[i-1]=stdin.nextDouble();
stdin.nextLine();
while(rainfallInput[i-1]<0)
{
System.out.print(\"Enter rainfall for month \" + i + \":\");
rainfallInput[i-1]=stdin.nextDouble();
stdin.nextLine();
}
}*/
RainFall[] test1 = new RainFall[rainfallInput.length];

System.out.println(Arrays.toString(test1));


//RainFall test1 = new RainFall(rainfallInput);
//System.out.println(Arrays.toString(test1));

/*
//for(int i=0;i<12;i++)
// System.out.println(rainfall[i]);
System.out.println(totalRainfall(rainfall));
System.out.println(averageRainfall(rainfall));
System.out.println(highestRainfall(rainfall));
System.out.println(lowestRainfall(rainfall));
*/
}

RainFall(double[] RainFall) {
//this.RainFall = RainFall;
}

public static double totalRainfall(double[] rainfall)
{
double temp=0.0;
for(int i=0;i<rainfall.length;i++)
temp += rainfall[i];
return temp;
}

public static double averageRainfall(double[] rainfall)
{
double temp=0.0;
for(int i=0;i<rainfall.length;i++)
temp += rainfall[i];
return temp/rainfall.length;
}

public static String highestRainfall(double[] rainfall)
{
int temp=0;
for(int i=0;i<12;i++)
if(rainfall[i]>rainfall[temp])
temp=i;
return new DateFormatSymbols().getMonths()[temp];
}

public static String lowestRainfall(double[] rainfall)
{
int temp=1;
for(int i=0;i<12;i++)
if(rainfall[i]<rainfall[temp])
temp=i;
return new DateFormatSymbols().getMonths()[temp];
}
}

Java Program 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 th
Java Program 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 th
Java Program 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 th
Java Program 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 th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site