Write a program that prompts the user to input cash amounts and use a method to
ID: 3766008 • Letter: W
Question
Write a program that prompts the user to input cash amounts and use a method to calculate the average amount for a week given 7 inputs of daily cash intake amounts for a cash register. Use an array to store the values. Recall that average is the sum of the values divided by the number of values. The method should take argument(s) of type double and return average weekly cash amount as a double.
Make sure you test your program using the test data given below and calls the averageCash() method defined above passing in the values. Verify that the input values are greater than or equal to 0. Negative numbers are not allowed. Output the values used and the averageCash value.
Run your application with the following test data using a loop to display the values stored in the array and save the screenshot of inputs and outputs to use when you submit all files to D2L.
Day
M
T W Th Th F Sat Sun
Cash Amount:
250.15 320.55 400.12
0 (invalid, message should prompt user to enter a new amount) 220.44
310.55
550.25
400.29
Expectations:
Use a loop to capture values as a user enters them.
Use an array to store the proper values as they are entered. Once all values have been captured and stored,
Output:
Use a loop to display the the values in the array
Output the total amount
Output the averageCash() amount return from your method averageCash()= the average of all values in the array.
Explanation / Answer
01
import java.util.*;
02
public class ArrayHandout
03
{
04
public static void main(String args[])
05
{
06
int[] a=new int[6];
07
Scanner sc=new Scanner(System.in);
08
System.out.println("Please enter cash values: ");
09
for(int j=0;j<6;j++)
10
a[j]=sc.nextInt();
11
System.out.println("Cash values are: ");
12
13
for (int i=0;i<a.length;i++)
14
System.out.println(a[i]);
15
System.out.println("Average of cash is: " + getAverage(a));
16
System.out.println("The Sum of cash is: " + getSum(a));
17
}
18
public static double getAverage(double average)
19
{
20
double z =0;
21
double[] b=new double[6];
22
int i=0;
23
for (i = 0; i < b.length; i++) {
24
z = z + b[i];}
25
26
return z/7;
27
}
28
public static double getSum(double sum)
29
{
30
double z =0;
31
double[] b=new double[6];
32
int i=0;
33
for (i = 0; i < b.length; i++) {
34
z = z + b[i];}
35
36
return z;
37
}}
01
import java.util.*;
02
public class ArrayHandout