Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java file outside of source root, there is the same error in both classes. How do I resolve this, so I will be able to

Java file outside of source root, there is the same error in both classes. How do I resolve this, so I will be able to run the code?

first Class

public class Month {

//data member to store the data

private String monthName;

private int amount;

//String parameter to set monthName field

//int parameter to set amount field

public Month(String monthName, int amount) {

this.monthName = monthName;

this.amount = amount;

}

//method to retrieve monthName and amount field

public String getmonthName() {

return monthName;

}

//method to retrieve the data

public int getamount() {

return amount;

}

}

Second Class

//import scanner function for prompting user

import java.util.Scanner;

//main class

public class RainFallAmounts {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

//an array hold 12 values

Month months[] = new Month[12];

String monthsName[] = {\"January\",\"February\",\"March\",\"April\",\"May\",\"June\",

\"July\",\"August\",\"Septmber\",\"October\",\"November\",\"December\"};

int rainfall;

for (int i = 0; i

//loop prompting user

while(true) {

System.out.print(\"Please enter the rainfall amount for \" +

monthsName[i] + \": \");

rainfall = sc.nextInt();

//invalid if input =>

if (rainfall > 0)

break;

System.out.println(\"Invalid amount. Try again.\");

}

months[i] = new Month(monthsName[i], rainfall);

}

//initialize totalRainfall to zero

int totalRainfall = 0;

// create and initialize min/max rainfall variable to month at index 0

int minRainfall = months[0].getamount();

int maxRainfall = months[0].getamount();

int maxIndex = 0;

int minIndex = 0;

for (int i = 0; i

//calculate total rain fall for 12 months

totalRainfall += months[i].getamount();

//find the month has the most rainfal

if (maxRainfall

maxIndex = i;

maxRainfall = months[i].getamount();

}

//find the month has the least rainfall

if (minRainfall > months[i].getamount()) {

minIndex = i;

minRainfall = months[i].getamount();

}

}

//calculate the average of 12 months

double avgRainfall;

avgRainfall = totalRainfall / 12.0;

//Display the results

System.out.println(\" The total rainfall is \" + totalRainfall + \" inches.\");

System.out.println(\"The average monthly rainfall is \" + avgRainfall + \" inches.\");

System.out.println(\"The month with the most rain was \" + monthsName[maxIndex] + \".\");

System.out.println(\"The month with the least rain was \" + monthsName[minIndex] + \".\");

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Human Resource Management A Global And Critical Perspective

Authors: Jawad Syed, J; Kramar Syed, Robin Kramar

2nd Edition

1137521627, 9781137521620

More Books

Students also viewed these Programming questions

Question

Define paraphrasing and reflecting.

Answered: 1 week ago