Question
import java.util.Scanner; public class Rainfall { public static void main(String[] args) { System.out.println(NAME 2023); System.out.println(This Rainfall program will ask you to enter the amount of
import java.util.Scanner;
public class Rainfall {
public static void main(String[] args) {
System.out.println("NAME 2023");
System.out.println("This Rainfall program will ask you to enter the amount of rain fall for each day of the week.");
System.out.println("(Use a decimal point to enter partial inches.)");
System.out.println("You will then be shown a summary of your entries.");
String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
Scanner scanner = new Scanner(System.in);
double[] amounts = new double[days.length]; // make as many slots as in the days array (they will be empty)
double totalRainfall = 0;
int numberOfDaysWithNoRainfall = 0;
double highestDailyRainfall = 0;
String dayWithMostRainfall = days[0];
for (int i = 0; i
{
System.out.println("Enter the rain fall amount for " + days[i]);
amounts[i] = scanner.nextDouble(); // save this day's rainfall in amounts array
if (amounts[i] >= 0) // execute this block if value of amounts[i] is greater than or equal to 0
{
System.out.println("Rainfall for " + days[i] + " was " + amounts[i]);
totalRainfall += amounts[i]; // same as saying: totalRainfall = totalRainfal + amounts[i];
if (amounts[i] == 0) numberOfDaysWithNoRainfall += 1; // add 1
if (amounts[i] > highestDailyRainfall)
{
highestDailyRainfall = amounts[i];
dayWithMostRainfall = days[i];
}
}
else // value of amounts[0] is negative (so execute this block instead)
{
System.out.println("Entered rainfall amounts cannot be negative. Please reenter.");
i = i - 1; // reset "bump" counter
}
}
for (int i = 1; i
{
System.out.println("Enter the rain fall amount for " + days[i]);
amounts[i] = scanner.nextDouble(); // save this day's rainfall in amounts array
if (amounts[i] >= 0) // execute this block if value of amounts[i] is greater than or equal to 0
{
System.out.println("Rainfall for " + days[i] + " was " + amounts[i]);
totalRainfall += amounts[i]; // same as saying: totalRainfall = totalRainfal + amounts[i];
if (amounts[i] == 0) numberOfDaysWithNoRainfall += 1; // add 1
if (amounts[i] > highestDailyRainfall)
{
highestDailyRainfall = amounts[i];
dayWithMostRainfall = days[i];
}
}
else // value of amounts[0] is negative (so execute this block instead)
{
System.out.println("Entered rainfall amounts cannot be negative. Please reenter.");
i = i - 1; // reset "bump" counter
}
}
for (int i = 2; i
{
System.out.println("Enter the rain fall amount for " + days[i]);
amounts[i] = scanner.nextDouble(); // save this day's rainfall in amounts array
if (amounts[i] >= 0) // execute this block if value of amounts[i] is greater than or equal to 0
{
System.out.println("Rainfall for " + days[i] + " was " + amounts[i]);
totalRainfall += amounts[i]; // same as saying: totalRainfall = totalRainfal + amounts[i];
if (amounts[i] == 0) numberOfDaysWithNoRainfall += 1; // add 1
if (amounts[i] > highestDailyRainfall)
{
highestDailyRainfall = amounts[i];
dayWithMostRainfall = days[i];
}
}
else // value of amounts[0] is negative (so execute this block instead)
{
System.out.println("Entered rainfall amounts cannot be negative. Please reenter.");
i = i - 1; // reset "bump" counter
}
}
for (int i = 3; i
{
System.out.println("Enter the rain fall amount for " + days[i]);
amounts[i] = scanner.nextDouble(); // save this day's rainfall in amounts array
if (amounts[i] >= 0) // execute this block if value of amounts[i] is greater than or equal to 0
{
System.out.println("Rainfall for " + days[i] + " was " + amounts[i]);
totalRainfall += amounts[i]; // same as saying: totalRainfall = totalRainfal + amounts[i];
if (amounts[i] == 0) numberOfDaysWithNoRainfall += 1; // add 1
if (amounts[i] > highestDailyRainfall)
{
highestDailyRainfall = amounts[i];
dayWithMostRainfall = days[i];
}
}
else // value of amounts[0] is negative (so execute this block instead)
{
System.out.println("Entered rainfall amounts cannot be negative. Please reenter.");
i = i - 1; // reset "bump" counter
}
}
for (int i = 4; i
{
System.out.println("Enter the rain fall amount for " + days[i]);
amounts[i] = scanner.nextDouble(); // save this day's rainfall in amounts array
if (amounts[i] >= 0) // execute this block if value of amounts[i] is greater than or equal to 0
{
System.out.println("Rainfall for " + days[i] + " was " + amounts[i]);
totalRainfall += amounts[i]; // same as saying: totalRainfall = totalRainfal + amounts[i];
if (amounts[i] == 0) numberOfDaysWithNoRainfall += 1; // add 1
if (amounts[i] > highestDailyRainfall)
{
highestDailyRainfall = amounts[i];
dayWithMostRainfall = days[i];
}
}
else // value of amounts[0] is negative (so execute this block instead)
{
System.out.println("Entered rainfall amounts cannot be negative. Please reenter.");
i = i - 1; // reset "bump" counter
}
}
for (int i = 5; i
{
System.out.println("Enter the rain fall amount for " + days[i]);
amounts[i] = scanner.nextDouble(); // save this day's rainfall in amounts array
if (amounts[i] >= 0) // execute this block if value of amounts[i] is greater than or equal to 0
{
System.out.println("Rainfall for " + days[i] + " was " + amounts[i]);
totalRainfall += amounts[i]; // same as saying: totalRainfall = totalRainfal + amounts[i];
if (amounts[i] == 0) numberOfDaysWithNoRainfall += 1; // add 1
if (amounts[i] > highestDailyRainfall)
{
highestDailyRainfall = amounts[i];
dayWithMostRainfall = days[i];
}
}
else // value of amounts[0] is negative (so execute this block instead)
{
System.out.println("Entered rainfall amounts cannot be negative. Please reenter.");
i = i - 1; // reset "bump" counter
}
}
for (int i = 6; i
{
System.out.println("Enter the rain fall amount for " + days[i]);
amounts[i] = scanner.nextDouble(); // save this day's rainfall in amounts array
if (amounts[i] >= 0) // execute this block if value of amounts[i] is greater than or equal to 0
{
System.out.println("Rainfall for " + days[i] + " was " + amounts[i]);
totalRainfall += amounts[i]; // same as saying: totalRainfall = totalRainfal + amounts[i];
if (amounts[i] == 0) numberOfDaysWithNoRainfall += 1; // add 1
if (amounts[i] > highestDailyRainfall)
{
highestDailyRainfall = amounts[i];
dayWithMostRainfall = days[i];
}
}
else // value of amounts[0] is negative (so execute this block instead)
{
System.out.println("Entered rainfall amounts cannot be negative. Please reenter.");
i = i - 1; // reset "bump" counter
}
}
scanner.close(); // close the input source
System.out.println("Total rainfall for the week was " + totalRainfall);
System.out.println("Number of days with no rainfall was " + numberOfDaysWithNoRainfall);
if (totalRainfall > 0)
System.out.println("Day with the most rainfall was " + dayWithMostRainfall
+ " with " + highestDailyRainfall + " inches.");
}
}
I'm currently stuck on step 15. Please help determine what I'm missing. JAVA LANGUAGE
String[] days = \{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; This array object is type String[]. Each entry is a String. It is an array-of-Strings Object. The INDEX values, shown on the left, are not in the array but is how we access an individual element in the array. e.g. if we wrote System.out.println("Today is " + days[3]); we would see on the console: Today is Thursday The number of slots in the array cannot be increased or reduced once the array is declared. Printing the contents of an array To iterate/"loop" through the arr: To iterate/"loop" through the array and print each entry/element in turn we wo to "bump"/increment the i index value for the next loop (i++ means bump by 1 ): for (int i=0;i i++ ) // keep looping as long as i is less than 7 , add 1 to index i for each loop. System. out.println(days[i]); // This is the instruction that will be executed as the "body" of each loop. Rainfall.java source file you are writing with Eclipse. If you are running in "full screen edit" mode you need Verify that you see your welcome messages followed by a print of each day of the week, with each day on a new line. Great! 3. Declare another array, this time to hold the daily rain amounts. Declare this second ay, cay above the for loop, where you declared the "days" array. double[] amounts = new double[days.length]; // make as many slots as in the days array (they will be empty) We assume all the slots are initialized to 0 since we didn't specify any value. Let's print it to see! Modify the print line statement in the for loop to be: System.out.print1n(days[1] + " rainfall is " + amounts[i]); Save and run your program again. Now it should show the amount of rainfall we have so far for each day. Monday rainfall is . Tuesday rainfall is . Whednesday rainfall is . Friday rainfall is . Saturday rainfall is . Sunday rainfall is . scanner scanner = new Scanner(System.in); pointer field anything we want. Since Java is case-sensitive, we can call the POINTER FIELD to the Scanner object "scanner" to help us remember what it points to! number of dogs the user was typing is. (We will call nextDouble() in this program because the entered value might have a decimal point in it.) . Ask the user to enter daily rain fall amounts. We can co the same. for (int i=0; i i++ ) value (the value of int i ) to access both arrays! Change the body of your for loop to look like this: for (int i=0; i i++ ) \} system.out.print1n("Enter the rain fall amount for " + days[i]); System.out.println("Enter the rain fall amount for "+ days[i]); amounts[i] = scanner.nextDouble(); // save this day's rainfall in amounts array System.out.println("Rainfall for " + days[i] + " was " + amounts[i]); Save and run your program again. Your output on the Console should now look like this: Paul Bownan This Rainfall program will ask you to enter the amount of rain fall for each day of the week. (Use a decimal point to enter partial inches.) You will then be shown a summary of your entries. Enter the rain fall amount for Monday Monday rainfall was . Enter the rain fall amount for Tuesday 1.1 Tuesday rainfall was 1.1 Enter the rain fall amount for Wednesday Wednesday rainfall was 2.2 Enter the rain fall amount for Thursday Thursday rainfall was 3.3 Thursday rainfall was 3.3 4.4 Friday rainfall was 4.4 Enter the rain fall amount for Saturday Saturday rainfall was 5.5 Enter the rain fall amount for Sunday Sunday rainfall was 6.6 0. Following the for loop, we can close() the Scanner since we no longer anticipate accepting input from our user. Make sure this statement is below the ending brace of the for loop, at the bottom of the main() method. scanner.close(); // close the input source 1. Check for input errors as we go through the for loop. The user line of the for loop with this: Note the line that prints the user's entry will not be printed if the user's input was negative. 2. Rerun your program Does it now "handle" entry of negative values? Great! Note how the INDENTATION of new blocks of code to the right and ALIGNMENT of beginning and ending braces in the same vertical column makes the code easier to read! for (int i=;i i+t ) // keep looping as long as i is less than 7 , add 1 to index i for each loop. System.out.println("Enter the rain fall amount for " + days[i]); amounts[i] = scanner. nextDouble(); // store entered amount in array if ( amounts [i]>=)// execute this block if value of amounts[i] is greater than or equal to System.out.print1n("Rainfall for " + days[i] + " was " + amounts[i]); else // value of amounts[ [] is negative (so execute this block instead) system.out.printin("Entered rainfall amounts cannot be negative. Please reenter."); i=i1;// reset "bump" counter information). numberofDaysWithNoRainfall) but we want to print it after our for loop. double totalRainfall =0; int numberofDayswithNoRainfall =0; double highestDailyRainfall =0; String daywithMostRainfall = days []; As we go through the for loop, add the entered daily rainfall to one or more of these fields as appropriate. Note that *ALL* of this new code will go into the IF block we just added, immediately following the System.out.println() statement: since the conditionally-executed code is more than one statement, must declare the extent of the if block with braces. 15. Following the for loop, print the data accumulated during the execution of the for loop. Be sure these statements are below the right brace that defines the end of the for loop. (e.g. following the scanner.close(); statement) System.out.printlin("Total rainfall for the week was " + totalkainfall); System.out.printlin("Number of days with no rainfall was " + numberofoayswithnoRainfal1); if (totatRainfall, b) System.out.printin("Day with the most rainfall was " + daywithMostrainfall + " with " + highestDailykainfall + " inches."); 16. Run your program. Does it now show some interesting data about this week's rainfall ? Is the data accurate? 17. Let's draw a graph! the for loop we have just been through. First comment-out the line in the top for-loop that echos the just-entered rainfall amount: // System.out.println(days[i] + " rainfall was " + amounts[i]); Then add this identical for loop at the bottom of main(): // show a surmary list for (int i=0; i i++) // same as for-loop above \{ System.out.println(days[i] + " " + amounts[i]) ; 18. Run your program to verify this 2 nd for loop is running OK 19. Line things up a little. Make each day name 9 characters long and run again. Better? 20. Now add a tiny line of * characters on each print line in the table at the end so the user can see at a glance what kind of week we just had, rain-wise. Just before you go into the for loop at the bottom, declare a line of "dots" we will use to draw our graph. string graphoots = "*****************************************"; Then use this code as the loop body: for (int i=0;i i++ ) // same as for-loop above if (amounts [i]= ) System.out.println(days[i] +" " + amounts[i]) ; System.out.println(days[i] + " " + amounts[i] \} +" + + graphDots.substring( (, (int) (( amounts [i]+.9)3)))Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started