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

Hello! I was wondering if I could get help writing a code. I have to Create a me

ID: 3694343 • Letter: H

Question

Hello! I was wondering if I could get help writing a code.
I have to 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

import java.util.*;
class Calculate
{
    int choice;
    static int size=0;
    boolean flag=true;
    static int nos[]=new int[10];
    Scanner read=new Scanner(System.in);
    public void getChoice()
    {
        while(flag)
        {
        System.out.println(" *****Enter your choice:****");
        System.out.println("1. Add a number to the array");
        System.out.println("2. Display the mean");
        System.out.println("3. Display the median");
        System.out.println("4. Print the array to the screen");
        System.out.println("5. Print the array in reverse order");
        System.out.println("6. Quit ");
        choice=read.nextInt();
        doAction(choice);
         if(choice==6)
         flag=false;
        }
    }
  
    public void doAction(int choice)
    {
        switch(choice)
        {
            case 1:boolean nflag=true;
                   int tmp;
                   while(nflag)
                   {
                    System.out.print("Enter number: ");
                    tmp=read.nextInt();
                    if(tmp>0)
                    {
                        nos[size]=tmp;
                        size++;
                        nflag=false;
                    }
                    else
                    System.out.println("Enter a valid no");
                    }
                break;
            case 2: System.out.println("Mean of array: "+calMean(nos));
                    break;
            case 3: System.out.println("Median of array: "+calMedian(nos));
                    break;
            case 4: System.out.println("Elements in array: ");
                    display(nos);
                    break;   
            case 5: System.out.println("Elements of array in reverse order: ");
                    displayReverse(nos);
                    break;
             case 6: System.out.println("Thanks for participating");
                    break;      
        }
    }
    public double calMean(int nos[])
    {
        double total=0;
        for(int i=0;i<nos.length;i++)
        {
            total+=nos[i];
        }
        return total/nos.length;
    }
    public int calMedian(int nos[])
    {
        Arrays.sort(nos);
        return nos[size/2];
    }
    public void display(int nos[])
    {
        for(int i=0;i<size;i++)
        System.out.print(nos[i]+" ");
    }
     public void displayReverse(int nos[])
    {
        for(int i=size-1;i>=0;i--)
        System.out.print(nos[i]+" ");
    }
}
public class HelloWorld{

     public static void main(String []args){
        Calculate test=new Calculate();
        test.getChoice();
     }
}

U didn't post SortSearchUril.java , hence i have provide my own logic for median

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