Jave Question: Write the code of data type class RainFall_yourLastName including
ID: 3829619 • Letter: J
Question
Jave Question:
Write the code of data type class RainFall_yourLastName including data member rainfallMonth that is an double array size 12 to store the rain amount of 12 months, 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 less rain The method toString to display the result in the following format: For example if the input from the keyboard for each month as data in the following table: The output is: Rainfall in the year: 70.98 Average monthly rainfall: 5.92 The month with the most rain May The month with the less rain AugustExplanation / Answer
RainFall_yourLatName.java
public class RainFall_yourLatName {
private double rainfallMonth[]=null;
//Parameterized constructor
public RainFall_yourLatName(double rainfallMonth[]) {
super();
this.rainfallMonth =rainfallMonth;
}
//Method which calculates the total rainfall of the year
public double totalRainFall()
{
double tot=0.0;
for(int i=0;i<rainfallMonth.length;i++)
{
tot+=rainfallMonth[i];
}
return tot;
}
//Method which calculates the average rainfall of the year
public double averageMonthlyRainfall()
{
double tot=totalRainFall();
return tot/rainfallMonth.length;
}
//Method which find in which month the rainfall is maximum
public int maxRainFallMonth()
{
double max=rainfallMonth[0];
int index=0;
for(int i=0;i<rainfallMonth.length;i++)
{
if(max<rainfallMonth[i])
{
max=rainfallMonth[i];
index=i;
}
}
return index;
}
//Method which find in which month the rainfall is minimum
public int minRainFallMonth()
{
double min=rainfallMonth[0];
int index=0;
for(int i=0;i<rainfallMonth.length;i++)
{
if(min>rainfallMonth[i])
{
min=rainfallMonth[i];
index=i;
}
}
return index;
}
}
______________________
TestRF.java
import java.util.Scanner;
public class TestRF {
public static void main(String[] args) {
//months array which contains names of the months
String months[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Double Array which stares rainfall
double rainfall[]=new double[12];
//getting the rainfall of each month and populate into rainfall[] array
for(int i=0;i<months.length;i++)
{
//Getting the input entered by the user
System.out.print("Enter the Rainfall in the month "+months[i]+":");
rainfall[i]=sc.nextDouble();
}
//creating an object by passing the rainfall array as input
RainFall_yourLatName rf=new RainFall_yourLatName(rainfall);
//Displaying the output
System.out.printf("RainFall in the year :%.2f ",rf.totalRainFall());
System.out.printf("Average Monthly RainFall :%.2f ",rf.averageMonthlyRainfall());
System.out.println("The Month with most rain :"+months[rf.maxRainFallMonth()]);
System.out.println("The Month with less rain :"+months[rf.minRainFallMonth()]);
}
}
_______________________
Output:
Enter the Rainfall in the month January:3.95
Enter the Rainfall in the month February:2.53
Enter the Rainfall in the month March:3.51
Enter the Rainfall in the month April:8.69
Enter the Rainfall in the month May:16.77
Enter the Rainfall in the month June:4.05
Enter the Rainfall in the month July:1.02
Enter the Rainfall in the month August:0.03
Enter the Rainfall in the month September:1.03
Enter the Rainfall in the month October:9.43
Enter the Rainfall in the month November:13.57
Enter the Rainfall in the month December:6.4
RainFall in the year :70.98
Average Monthly RainFall :5.92
The Month with most rain :May
The Month with less rain :August
___________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.