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

Create a menu-driven program that will accept a collection of non-negative integ

ID: 3694786 • Letter: C

Question

Create a menu-driven program that will accept a collection of non-negative integers from the keyboard, calculate the mean and median values and display those values on the screen. My menu should have 6 options: 1. Add a number to the array 2. Display the mean 3. Display the median 4. Print the array to the screen 5. Print the array in reverse order 6. Quit Program particulars: Use an array of type int to store the integers entered by the user. There must be error checking on the input integer. If it is negative, the program will print an error message and re-prompt. This process will continue until a non-negative integer is entered. You must use a try-catch structure to trap both types of input errors (like letters where numbers should go) and range errors (like -1). There must be error checking on the menu choice entered. If the user enters a choice not on the menu, the program will print an error message, re-display the menu and re-prompt. This process will continue until a valid option value is entered. Your solution must be modular. The design of your methods is up to you, but the rules of “highly cohesive” and “loosely coupled” must be followed. Your program should be well-documented. Explain what you’re doing in your code. Be sure to include the usual name and assignment notes. Note your program will have to sort your array before you can find the median. Include your SortSearchUtil.java file which will contain your sort method.

Explanation / Answer

MeanMedian.java

import java.util.*;

public class MeanMedian {

   static Scanner input = new Scanner(System.in);

   static int num;

   static int[] intAra = new int[0];
       

   public static void main(String[] args) {    
    
      System.out.println("Welcome to Integer Array Fun");
    
      num = enterInt();
    
      menu();   
    
   }

   public static int enterInt() { //method takes an integer from user

      boolean continueInput = true;
    
      do {    
    
         try {
       
            System.out.print(" Please enter a non-negative integer: ");
    
            num = input.nextInt();
    
            intAra = toAra(num); //create an array from the integer
    
            System.out.println();        
    
            while (num < 0) {

               System.out.print("I am sorry, that is not a non-negative integer. Please enter a non-negative integer: ");

               num = input.nextInt();       
            }
       
            continueInput = false;
         }    
    
         catch(InputMismatchException ex) {
    
            System.out.println(" I am sorry, that is not a non-negative integer.");

            input.nextLine();
          
          
         }    
      } while (continueInput);
      return num;
    
   }

   public static void menu() { //method creates a menu of choices   
    
      int choice = 0;
    
      boolean continueInput = true;
    
      do {
    
         try {

            while (choice != 6) {
    
               System.out.println("Please select from the following menu choices: " +
               "   1. Add a number to the array" +
               "    2. Display the mean" +
               "    3. Display the median" +
               "    4. Print the array to the screen" +
               "    5. Print the array in reverse order" +
               "    6. Quit the program ");
       
               System.out.print("Choice--> ");
       
               choice = input.nextInt();
       
               switch (choice) {
       
                  case 1: enterInt();
                          break;
          
                  case 2: findMean(intAra);
                          break;
          
                  case 3: findMedian(intAra);
                          break;
                  
                  case 4: printAra(intAra);
                          break;
          
                  case 5: reverseAra(intAra);
                          break;
                
                  case 6: System.out.println(" Thank you and have a nice day.");
                          break;
          
                  default: System.out.println(" I am sorry, that is an invalid menu choice." +
                  " Please try again ");
               }
            }
          
            continueInput = false;
         }
       
         catch(InputMismatchException ex) {
    
            System.out.println(" I am sorry, that is not valid menu choice. ");

            input.nextLine();          
         }    
      } while (continueInput);
       
   }

   public static int[] toAra(int num) { //adds an int input to an array    
    
      int[] ints = new int[intAra.length + 1];
    
      for (int i = 0; i < ints.length; i++) {
    
         if (i == ints.length - 1)
            ints[i] = num;
         else
            ints[i] = intAra[i];
      }
          
      return ints;    
   }

   public static void printAra(int[] intAra) { //prints the numbers input by the user as an array

      System.out.println(" " + Arrays.toString(intAra) + " ");
   }

   public static void findMean(int[] intAra) { //Calculates the mean of the array of numbers

      int count = 0;
    
      int sum = 0;

      for (int i = 0; i < intAra.length; i++) {
    
         count++;       
       
         sum += intAra[i];
      }
    
      double mean = (double) sum / count;
    
      System.out.println(" The mean of the array is " + mean + " ");
   }

   public static void findMedian(int[] intAra) { //Calculates the median of the array of numbers

      int[] sortedAra = intAra.clone();
    
      SortSearchUtil.sortSelection(sortedAra); //sorts the array
    
      if (sortedAra.length % 2 == 0) {
         int[] median = {sortedAra[(sortedAra.length / 2) - 1], sortedAra[sortedAra.length / 2]};
         System.out.println(" The median of the array is " + Arrays.toString(median));
      }
      else {
         int median = sortedAra[sortedAra.length / 2];
         System.out.println(" The median of the array is " + median);
      }
    
      System.out.println();
   }

   public static void reverseAra(int[] intAra) { //reverses the order of the array

      int[] reverseAra = intAra.clone();

      for (int i = 0; i < (reverseAra.length / 2); i++) {
    
         int temp = reverseAra[i];
       
         int reciprocal = reverseAra.length - (i + 1);
       
         reverseAra[i] = reverseAra[reciprocal];
       
         reverseAra[reciprocal] = temp;
      }
          
      System.out.println(" The array sorted in reverse order is: " +
      Arrays.toString(reverseAra) + " ");
   }
}


SortSearchUtil.java

public class SortSearchUtil {
   
   public static int linearSearch(int[] numbers, int match) {
    
      int bingo = 0;
       
      for (int i = 0; i < numbers.length; i++) {
         numbers[i] = i + 1;
         if (numbers[i] == match) {
            bingo = i;
            break;
         }
         else {
            bingo = -1;
         }                   
      }
      return bingo;         
   }

   public static int binarySearch(int[] numbers, int match) {

      int high = numbers.length - 1;
      int low = 0;
      int mid = 0;
    
      while (high >= low) {
    
         mid = (high + low) / 2;
    
         if (numbers[mid] > match)
            high = mid - 1;
         else if ( numbers[mid] < match)
            low = mid + 1;
         else
            return mid;
          
      }
      return -1;
   }

   public static int[] sortSelection(int[] numbers) {

      for (int i = 0; i < numbers.length - 1; i++) {
    
         int currentMin = numbers[i];
       
         int currentMinIndex = i;
       
         for (int j = i + 1; j < numbers.length; j++) {       
                      
            if (currentMin > numbers[j]) {
             
               currentMin = numbers[j];
             
               currentMinIndex = j;             
            }
         }
       
         if (currentMinIndex != i) {
       
            numbers[currentMinIndex] = numbers[i];
          
            numbers[i] = currentMin;
         }
      }
      return numbers;
   }

   public static Comparable[] sortSelection(Comparable[] numbers) {

      for (int i = 0; i < numbers.length - 1; i++) {
    
         Comparable currentMin = numbers[i];
       
         int currentMinIndex = i;
       
         for (int j = i + 1; j < numbers.length; j++) {       
                      
            if (currentMin.compareTo(numbers[j]) > 0) {
             
               currentMin = numbers[j];
             
               currentMinIndex = j;             
            }
         }
       
         if (currentMinIndex != i) {
       
            numbers[currentMinIndex] = numbers[i];
          
            numbers[i] = currentMin;
         }
      }
      return numbers;
   }
}

Welcome to Integer Array Fun

Please enter a non-negative integer: 3

Please select from the following menu choices:

   1. Add a number to the array
   2. Display the mean
   3. Display the median
   4. Print the array to the screen
   5. Print the array in reverse order
   6. Quit the program

Choice--> 1

Please enter a non-negative integer: 4

Please select from the following menu choices:

   1. Add a number to the array
   2. Display the mean
   3. Display the median
   4. Print the array to the screen
   5. Print the array in reverse order
   6. Quit the program

Choice--> 2

The mean of the array is 3.5

Please select from the following menu choices:

   1. Add a number to the array
   2. Display the mean
   3. Display the median
   4. Print the array to the screen
   5. Print the array in reverse order
   6. Quit the program

Choice--> 6

Thank you and have a nice day.

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