Hello this is my first java project and i need help. Thank you in advanced for a
ID: 3665167 • Letter: H
Question
Hello this is my first java project and i need help. Thank you in advanced for all your answers and reply to this
The teacher wants us to use constants to understand the code better
First, read in rainfall values from the user.
Read in twelve values tor represent the amount of rainfall for each of the 12 months.
Do not accept negative numbers (since this does not make sense for rainfall).
Store the values in a local array.
Note: the array should be declared in the main method, not as a static variable outside of main.
Second, offer a menu of options to the user.
The menu should let the user choose between six actions (described in 3-8)
For each action, write a method to accomplish that action.
Write a method to output the rainfall for each month.
Write a method to find the total rainfall for the year.
Note: because the array was declared in main, you will need to pass it as a variable to this and all other methods.
Write a method to find the average monthly rainfall.
Write a method to return the name of the month with the most rain.
Note: if two months have the same maximum value, you can output either month.
Write a method to return the name of the month with the least rain.
Note: if two months have the same minimum value, you can output either month.
Write a method to update the amount of rain in any given month.
Notes:
Your code should be in a single class and should contain at least seven methods (main plus the six required methods).
You can also write additional helper methods.
Each method is worth 10 points. The remainder of the points will be awarded for general style and approach.
You will receive no credit for files that do not compile.
For full credit, follow good principles of object-oriented programming, method design, and naming conventions.
Use constants when possible and avoid duplicating code.
Hint: think about whether you can use an array of month names to simplify code and avoid duplicating code.
Consider using switches in addition to conditionals.
I provided a sample output file (using 2014-2015 data from http://ggweather.com/sf/monthly.html).
Your program does not have to run exactly like mine, but you can use this as a guide for what the output might look like and the different tests you should run to make sure your program works properly.
sample program test is this
https://gyazo.com/7901aed6cbadbf4bd8ed859654a19357
https://gyazo.com/34a2a707938b0603ce3b573b86abed81
https://gyazo.com/21d37ea6169ef59db0bcb2a8e677f54a
Explanation / Answer
/**The java program that prompts user to enter rain fall
* for 12 months.Then prints a menu of choices to select.
* Then the correspoding input is taken from user.
* The displays the result to console.*/
import java.util.Scanner;
public class RainFallProgram
{
//Create a Scanner class object
private static Scanner scanner = new Scanner(System.in);
//an array of string of 12 month names
private static String monthNames[]=
{"Jan","Feb",
"March","April",
"May","June",
"July","August",
"Sep","Oct",
"Nov",
"Dec"};
public static void main(String[] args)
{
//an array of type double
double[] rainarray=new double[12];
//set rain to zero
double rain=0;
//Read 12 double values and check of non-negative values
for (int month = 0; month < rainarray.length; month++)
{
//validation of input rain fall
do
{
System.out.println("Enter the amount of rain for "+monthNames[month]+".:");
//read rain fall
rain=scanner.nextDouble();
if(rain<0)
System.out.println("Invalid value. Rainfall cannot be negative.");
}while(rain<0);
//Set rain to rainarray at month index
rainarray[month]=rain;
}
char choice;
//Repeat the loop until user enters any key other than
//v,t,a,m,l and u .
do
{
//call menu function
choice=menu();
switch(choice)
{
case 'v':
//call print funtion
print(rainarray);
break;
case 't':
//call total funtion
System.out.printf("Total rain fall %.2f ",total(rainarray));
break;
case 'a':
//call average funtion
System.out.printf("Averaege rain fall %.2f ",average(rainarray));
break;
case 'm':
//call max funtion
System.out.printf("Maximum rain fall %s ",max(rainarray));
break;
case 'l':
//call min funtion
System.out.printf("Least rain fall %s ",min(rainarray));
break;
case 'u':
//call update funtion
update(rainarray);
break;
}
}while((choice=='v') ||(choice=='t')||(choice=='a')||(choice=='m')||
(choice=='l')||(choice=='u'));
}
/**The method update that prompts for month name number and new rain fall and sets
* the new value to the corresponding month*/
private static void update(double[] rainarray)
{
int month;
System.out.println("Enter the number of month to update(1-12)");
month=scanner.nextInt();
System.out.println("Enter new rain fall for "+monthNames[month-1]);
double rainfall=scanner.nextDouble();
rainarray[month-1]=rainfall;
}
/**The method min that returns the month in which the minimum rainfall is occured
* The method will not return empty string if two months have same rain falls*/
private static String min(double[] rainarray)
{
//Assume first month is minimum rain fall month
double leastRainfall = rainarray[ 0 ];
//Set name of starting month, january
String minMonth=monthNames[0];
boolean repeat=false;
for (int i = 1; i < rainarray.length; i++)
{
if (rainarray[i] < leastRainfall)
{
leastRainfall=i;
//set month name of correspoding i
minMonth=monthNames[i];
}
if(rainarray[i]==leastRainfall)
repeat=true;
}
if(repeat)
return "";
else
//returns minimum rain fall month
return minMonth;
}
/**The method max that returns the month in which the maximum rainfall is occured
* The method will not return empty string if two months have same rain falls*/
private static String max(double[] rainarray)
{
//Assume first month is largest rain fall month
double largest = rainarray[ 0 ];
//Set name of starting month, january
String maxMonth=monthNames[0];
boolean repeat=false;
for (int i = 1; i < rainarray.length; i++)
{
if (rainarray[ i ] > largest)
{
largest=i;
//set month name of correspoding i
maxMonth=monthNames[i];
}
if(rainarray[i]==largest)
repeat=true;
}
if(repeat)
return "";
else
//returns minimum rain fall month
return maxMonth;
}
//The method average that retuns the average rainfall
private static double average(double[] rainarray)
{
return total(rainarray) / rainarray.length;
}
//The method total that retuns the total rainfall
private static double total(double[] rainarray)
{
double total = 0;
for (int i = 0; i < rainarray.length; i++)
total += rainarray[ i ];
return total;
}
/**The method print that prints the name of month and rainfall in a
* neatly formatted order*/
private static void print(double[] rainarray)
{
for (int i = 0; i < rainarray.length; i++)
{
System.out.printf("%-10s . %.2f inches ",monthNames[i],rainarray[i]);
}
}
private static char menu()
{
System.out.println("Enter 'v' to view the monthly rainfall");
System.out.println("Enter 't' to find the total rainfall");
System.out.println("Enter 'a' to find the average rainfall");
System.out.println("Enter 'm' to find the month with the most rainfall");
System.out.println("Enter 'l' to find the month with the least rainfall");
System.out.println("Enter 'u' to update rainfall for a given month");
System.out.println("Enter any other character to quit.");
char ch=scanner.next().charAt(0);
return ch;
}
}
----------------------------------------------------------------------------------------------------------------
Sample output:
Enter the amount of rain for Jan.:
0.00
Enter the amount of rain for Feb.:
1.47
Enter the amount of rain for March.:
0.12
Enter the amount of rain for April.:
-1.30
Invalid value. Rainfall cannot be negative.
Enter the amount of rain for April.:
1.30
Enter the amount of rain for May.:
0.09
Enter the amount of rain for June.:
0.12
Enter the amount of rain for July.:
0.08
Enter the amount of rain for August.:
0.01
Enter the amount of rain for Sep.:
0.55
Enter the amount of rain for Oct.:
0.46
Enter the amount of rain for Nov.:
229
Enter the amount of rain for Dec.:
11.70
Enter 'v' to view the monthly rainfall
Enter 't' to find the total rainfall
Enter 'a' to find the average rainfall
Enter 'm' to find the month with the most rainfall
Enter 'l' to find the month with the least rainfall
Enter 'u' to update rainfall for a given month
Enter any other character to quit.
v
Jan . 0.00 inches
Feb . 1.47 inches
March . 0.12 inches
April . 1.30 inches
May . 0.09 inches
June . 0.12 inches
July . 0.08 inches
August . 0.01 inches
Sep . 0.55 inches
Oct . 0.46 inches
Nov . 229.00 inches
Dec . 11.70 inches
Enter 'v' to view the monthly rainfall
Enter 't' to find the total rainfall
Enter 'a' to find the average rainfall
Enter 'm' to find the month with the most rainfall
Enter 'l' to find the month with the least rainfall
Enter 'u' to update rainfall for a given month
Enter any other character to quit.
t
Total rain fall 244.90
Enter 'v' to view the monthly rainfall
Enter 't' to find the total rainfall
Enter 'a' to find the average rainfall
Enter 'm' to find the month with the most rainfall
Enter 'l' to find the month with the least rainfall
Enter 'u' to update rainfall for a given month
Enter any other character to quit.
a
Averaege rain fall 20.41
Enter 'v' to view the monthly rainfall
Enter 't' to find the total rainfall
Enter 'a' to find the average rainfall
Enter 'm' to find the month with the most rainfall
Enter 'l' to find the month with the least rainfall
Enter 'u' to update rainfall for a given month
Enter any other character to quit.
m
Maximum rain fall Dec
Enter 'v' to view the monthly rainfall
Enter 't' to find the total rainfall
Enter 'a' to find the average rainfall
Enter 'm' to find the month with the most rainfall
Enter 'l' to find the month with the least rainfall
Enter 'u' to update rainfall for a given month
Enter any other character to quit.
l
Least rain fall Jan
Enter 'v' to view the monthly rainfall
Enter 't' to find the total rainfall
Enter 'a' to find the average rainfall
Enter 'm' to find the month with the most rainfall
Enter 'l' to find the month with the least rainfall
Enter 'u' to update rainfall for a given month
Enter any other character to quit.
u
Enter the number of month to update(1-12)
1
Enter new rain fall for Jan
2.5
Enter 'v' to view the monthly rainfall
Enter 't' to find the total rainfall
Enter 'a' to find the average rainfall
Enter 'm' to find the month with the most rainfall
Enter 'l' to find the month with the least rainfall
Enter 'u' to update rainfall for a given month
Enter any other character to quit.
v
Jan . 2.50 inches
Feb . 1.47 inches
March . 0.12 inches
April . 1.30 inches
May . 0.09 inches
June . 0.12 inches
July . 0.08 inches
August . 0.01 inches
Sep . 0.55 inches
Oct . 0.46 inches
Nov . 229.00 inches
Dec . 11.70 inches
Enter 'v' to view the monthly rainfall
Enter 't' to find the total rainfall
Enter 'a' to find the average rainfall
Enter 'm' to find the month with the most rainfall
Enter 'l' to find the month with the least rainfall
Enter 'u' to update rainfall for a given month
Enter any other character to quit.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.