SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Arrays
Exercise:
Programming Projects
Question:3 | ISBN:9780132830317 | Edition: 5

Question

Write a program that reads in the average monthly rainfall for a city for each month of the year and then reads in the actual monthly rainfall for each of the previous 12 months. The program should then print out a nicely formatted table showing the rainfall for each of the previous 12 months as well as how much above or below average the rainfall was for each month. The average monthly rainfall is given for the months January, February, and so forth, in order. To obtain the actual rainfall for the previous 12 months, the program should first ask what the current month is and then ask for the rainfall figures for the previous 12 months. The output should correctly label the months. There are a variety of ways to deal with the month names. One straightforward method is to code the months as integers and then do a conversion to a string for the month name before doing the output. A large switch statement is acceptable in an output method. The month input can be handled in any manner you wish so long as it is relatively easy and pleasant for the user. Include a loop that allows the user to repeat this entire calculation until the user requests that the program end.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

import java.util.*;
public class Main 
{
	private static String monthString(int rainfallMonth)
	{
        switch(rainfallMonth)
		{
		case 1:
			return "January";
		case 2:
			return "February";
		case 3:
			return "March";
		case 4:
			return "April";
		case 5:
			return "May";
		case 6:
			return "June";
		case 7:
			return "July";
		case 8:
			return "August";
		case 9:
			return "September";
		case 10:
			return "October";
		case 11:
			return "November";
		case 12:
			return "December";
      default:
			System.out.println("Incorrect month entered. ");
			System.exit(0);
			return "Error";
		}
	}
    public static void main(String args[])
	{


		Scanner input = new Scanner(System.in);
		double[] actualRain = new double[12];
		double[] averageRain = new double[12];


		System.out.println("Enter the average rainfall for each month:");
		for(int i = 1; i <= 12; i++)
		{

			System.out.print(monthString(i) + ": ");
			averageRain[i - 1] = input .nextDouble();	
		}
        System.out.println();
		System.out.print("What is the current month?(month number): ");
		int currentMonth = input.nextInt();
		
		System.out.println("Enter the actual rainfall to the past 12 months:");

		for(int i = currentMonth; i <= 12; i++)
		{
			System.out.print(monthString(i) + ": ");
			actualRain[i - 1] = input.nextDouble();	
		}
        for(int i = 1; i < currentMonth; i++)
		{
			System.out.print(monthString(i) + ": ");
			actualRain[i - 1] = input.nextDouble();	
		}
        System.out.println("Month\t\tAverage\t\t" +	" Actual\t\tDifference");
		System.out.println("------\t\t-------\t\t" +"-------\t\t----------");
         for(int i = 1; i <= 12; i++)
		{
			System.out.print(monthString(i) + "\t");

			//for formatting purposes
			if(i != 2 && i != 9 && i != 11 && i != 12)
				System.out.print("\t");
			System.out.print("  " + averageRain[i - 1] + "\t\t");
			System.out.print("  " + actualRain[i - 1] + "\t\t");
	         System.out.print("   " +(averageRain[i - 1] - actualRain[i - 1]));
			System.out.println();
		}
	}
}

Output of the program:

Enter the average rainfall for each month:
January: 5
February: 8
March: 9
April: 10
May: 2
June: 15.5
July: 18.5
August: 25
September: 22.5
October: 11
November: 5.5
December: 2

What is the current month?(month number): 8
Enter the actual rainfall to the past 12 months:
August: 24
September: 22.5
October: 15
November: 8.5
December: 5
January: 4
February: 6
March: 9
April: 10.5
May: 11.5
June: 22
July: 25
Month           Average          Actual         Difference
------          -------         -------         ----------
January           5.0             4.0              1.0
February          8.0             6.0              2.0
March             9.0             9.0              0.0
April             10.0            10.5             -0.5
May               2.0             11.5             -9.5
June              15.5            22.0             -6.5
July              18.5            25.0             -6.5
August            25.0            24.0             1.0
September         22.5            22.5             0.0
October           11.0            15.0             -4.0
November          5.5             8.5              -3.0
December          2.0             5.0              -3.0

 

0 0

Discussions

Post the discussion to improve the above solution.