Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in Java that prompts the user to input cash amounts and calculat

ID: 3567912 • Letter: W

Question

Write a program in Java that prompts the user to input cash amounts and calculates 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. 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 uses the averageCash() method defined above passing the values. Verify that the input values are greater than 0. Negative numbers are not allowed. Output the values used and the averageCash value.

Test Data:

Monday: 250.15

Tuesday: 320.55

Wednesday: 400.12

Thursday: 0 (invalid, prompt user to enter a new amount)

Thursday: 220.44

Friday: 310.55

Saturday: 550.25

Sunday: 400.29

Explanation / Answer

import java.util.Scanner;


//AverageAmount.java
public class AverageAmount
{
   static String weekdasy[]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
  
   public static void main(String[] args)
   {
       Scanner reader=new Scanner(System.in);
      
       int SEVEN_DAYS=7;
       //to store the cash for each day
       double[] cash=new double[SEVEN_DAYS];
      
      
       boolean invalid=false;
      
      
       System.out.println("===Cash Amounts====");
       System.out.println("Enter cash for each day in a week");
       for (int i = 0; i < cash.length; i++)
       {
          
           //prompt user to enter the cash amount
           //and repeat loop if the cash amount
           //is less than or equal to zero.
           do {
              
               System.out.print(weekdasy[i]+" :");
               cash[i]=reader.nextDouble();
              
               if(cash[i]<=0)
                   invalid=true;
               else
                   invalid=false;
              
           } while (invalid);
          
          
           invalid=false;
       }
       printCash(cash);
       System.out.println("Average :"+averageCash(cash));      
   }
  
  
   //The method printCash that accepts cash array
   //and prints the cash for weekly days.
   private static void printCash(double[] cash) {
      
       System.out.println("Weekly cash amounts");
       for (int day = 0;day < cash.length; day++)
       {
          
           System.out.println(weekdasy[day]+":"+cash[day]);
       }
      
      
   }


//The method averageCash that accepts a double array cash and
   //returns the average of weekly days cash.
   private static double averageCash(double[] cash)
   {
       double total=0;  
       for (int day = 0; day < cash.length; day++) {
           total+=cash[day];
       }
      
       //return average of a
       return total/7;
   }
}

------------------------------------------------------------------------------
Sample output:
===Cash Amounts====
Enter cash for each day in a week
Monday :250.15
Tuesday :320.55
Wednesday :400.12
Thursday :0
Thursday :220.44
Friday :310.55
Saturday :550.25
Sunday :400.29
Weekly cash amounts
Monday:250.15
Tuesday:320.55
Wednesday:400.12
Thursday:220.44
Friday:310.55
Saturday:550.25
Sunday:400.29
Average :350.3357142857143

Hope this would helpful.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote