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

JAVA. I got most of it, I just don\'t know how to invoke it at the end? Add a me

ID: 3830539 • Letter: J

Question

JAVA. I got most of it, I just don't know how to invoke it at the end?

Add a method (not the main method) to find the average of a list of integers. This method will not return anything:

Use an array to store the input list.

Prompt the user for the number of integers.

Use this input to set the size of the array.

Prompt the user for the numbers to find the average of. Store these numbers in the array (use a for loop to gather the input).

Ask for the numbers one at a time (ask for a number, wait for user input, ask for the next number, wait for user input, etc.)

Use a for statement to calculate the average. Note that the average will be a floating point data type so you will need to do a conversion. For instance, the average of 1, 2, 3, 4, and 6 is 3.2, not 3.0. Display the result with an output message. Include the numbers being averaged in the output message. Make sure the output message clearly states what the user is looking at.

Add a main method to the program: Create a menu asking the user to average a list of numbers or to quit (there should be two options in the menu):

Option 1 is to average a list of numbers.

Option 2 is to quit.

Use a switch statement with the menu to determine which option to use:

If the user chooses option 1: Invoke the method to average a list of numbers.

If the user chooses option 2: Display a polite goodbye message. Include a default message in case the user enters an invalid option.

Repeat the program (make sure the menu displays each time) until the user chooses to quit (option 2)

Explanation / Answer

package org.students;

import java.util.Scanner;

public class Menu {
   //Scanner object is used to get the inputs entered by the user
   static Scanner sc=sc=new Scanner(System.in);
   public static void main(String[] args) {
  
       //Declaring variable
       int choice;

      
       //This loop continues to execute until the user enters 2 as input
       while(true)
       {
           //Displaying the menu
           System.out.println(" :: Menu ::");
           System.out.println("1.Average List of Numbers");
           System.out.println("2.Quit");
          
           //getting the choice entered by the user
           System.out.print("Enter Choice :");
           choice=sc.nextInt();
          
           //Based on the user choice the corresponding case will be executed
           switch(choice)
           {
           case 1:{
               //calling the method to calculate the average
               average();
               continue;
           }
           case 2:{
               System.out.println("goodbye");
               break;
           }
           default :{
               System.out.println("** Invalid.Select valid Option **");
               continue;
           }
           }
           break;
       }
      
      
      

   }

   //This method will calculate the average elements
   private static void average() {
       //Declaring variables
       int num,sum=0;
       float average=0;
      
       //getting the input entered by the user
       System.out.print("How many numbers you want to enter :");
       num=sc.nextInt();
      
       //creating an integer array
       int nos[]=new int[num];
      
       //getting the inputs entered by the user and populate those values into an array
       for(int i=0;i<num;i++)
       {
           System.out.print("Enter the number#"+(i+1)+":");
           nos[i]=sc.nextInt();
       }
      
       //calculating sum
       for(int i=0;i<num;i++)
       {
           sum+=nos[i];
       }
       //Calculating average
       average=(float)sum/num;
      
       //displaying the average
       System.out.println("The average of elements in the array is :"+average);
      
   }

}

_____________________

Output:


:: Menu ::
1.Average List of Numbers
2.Quit
Enter Choice :1
How many numbers you want to enter :5
Enter the number#1:56
Enter the number#2:67
Enter the number#3:78
Enter the number#4:89
Enter the number#5:88
The average of elements in the array is :75.6

:: Menu ::
1.Average List of Numbers
2.Quit
Enter Choice :2

goodbye

______________Thank You