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. Your
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). You must write your own selectionSort utility method to
sort your array. Place the method in an external file named SortSearchUtil.java. 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.
This is what my project looks like already: Please fix it thanks. The SortSearchUtil will also be
included.
import java.util.Scanner;
public class ArraySorting
{
public static void main(String[] args)
{
int option;
int integer = 0;
int optionOne;
int optionTwo;
int optionThree;
int optionFour;
int optionFive;
int[] numbers = new int[5];
Scanner kb = new Scanner(System.in);
System.out.println(\"Please enter a non-negative integer: \");
integer = kb.nextInt();
while((integer < 0))
{
System.out.println(\"I am sorry that is not a non-negative integer.\");
System.out.println(\"\");
System.out.println(\"Please enter a non-negative integer: \");
integer = kb.nextInt();
}
option = displayMenu(kb);
while (option != 6)
{
switch (option)
{
case 1:
optionOne(numbers);
System.out.println(\"\");
break;
case 2:
optionTwo(numbers);
System.out.println(\"\");
case 3:
//optionThree();
System.out.println(\"\");
break;
case 4:
//optionFour();
System.out.println(\"\");
break;
case 5:
//optionFive();
System.out.println(\"\");
break;
}
option = displayMenu(kb);
}
if (option == 6)
{
System.out.println();
System.out.println(\"Thank you. Have a nice day.\");
}
}
private static int displayMenu(Scanner kb)
{
int option = 0;
while (option != 1 && option != 2 && option != 3 && option != 4 && option !=5 && option
!=6)
{
System.out.println(\"\\t\\t1. Add a number to the array\ \\t\\t2. Display the mean\ \\t\.
Create a menu-driven program that will accept a collection of non-ne.pdf
1. 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. Your
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). You must write your own selectionSort utility method to
sort your array. Place the method in an external file named SortSearchUtil.java. 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.
This is what my project looks like already: Please fix it thanks. The SortSearchUtil will also be
included.
import java.util.Scanner;
public class ArraySorting
{
public static void main(String[] args)
{
int option;
int integer = 0;
int optionOne;
int optionTwo;
int optionThree;
int optionFour;
int optionFive;
int[] numbers = new int[5];
2. Scanner kb = new Scanner(System.in);
System.out.println("Please enter a non-negative integer: ");
integer = kb.nextInt();
while((integer < 0))
{
System.out.println("I am sorry that is not a non-negative integer.");
System.out.println("");
System.out.println("Please enter a non-negative integer: ");
integer = kb.nextInt();
}
option = displayMenu(kb);
while (option != 6)
{
switch (option)
{
case 1:
optionOne(numbers);
System.out.println("");
break;
case 2:
optionTwo(numbers);
System.out.println("");
case 3:
//optionThree();
System.out.println("");
break;
case 4:
//optionFour();
System.out.println("");
break;
case 5:
//optionFive();
System.out.println("");
3. break;
}
option = displayMenu(kb);
}
if (option == 6)
{
System.out.println();
System.out.println("Thank you. Have a nice day.");
}
}
private static int displayMenu(Scanner kb)
{
int option = 0;
while (option != 1 && option != 2 && option != 3 && option != 4 && option !=5 && option
!=6)
{
System.out.println("tt1. Add a number to the array tt2. Display the mean tt3. Display
the median tt4. Print the array to the screen tt5. Print the array in reverse order tt6.
Quit");
option = kb.nextInt();
if (!(option == 1 || option == 2 || option == 3 || option == 4 || option == 5 || option == 6))
System.out.println("I am sorry that is an invalid choice. Please try again.");
}
return option;
}
private static int[] optionOne()
{
Scanner input = new Scanner(System.in);
int[] numbers = new int[1];
for (int i = 0; i < numbers.length; i++)
{
System.out.println("Please enter number");
numbers[i] = input.nextInt();
}
return numbers;
4. }
private static void optionTwo(int[] numbers)
{
int sum = 0;
for(int i=0; i < numbers.length; i++)
sum = sum + numbers[i];
double average = sum / numbers.length;
System.out.println("Average value of array element is: " + average);
}
public class SortSearchUtil
{
public static void selectionSort(int[] array)
{
int current, indexSmallest, posToFill;
int temp;
for(posToFill = 0; posToFill < array.length - 1; posToFill++)
{
indexSmallest = posToFill;
for(current = posToFill + 1; current < array.length; current++)
{
if(array[current] < array[indexSmallest])
{
indexSmallest = current;
}
}
temp = array[posToFill];
array[posToFill] = array[indexSmallest];
array[indexSmallest] = temp;
}
}
public static void selectionSort(String[] array)
{
int current, indexSmallest, posToFill;
5. String temp;
for (posToFill = 0; posToFill < array.length - 1; posToFill++)
{
indexSmallest = posToFill;
for (current = posToFill + 1; current < array.length; current++)
{
if (array[current].compareTo(array[indexSmallest]) <0)
{
indexSmallest = current;
}
}
temp = array[posToFill];
array[posToFill] = array[indexSmallest];
array[indexSmallest] = temp;
}
}
}
Solution
import java.util.Arrays;
import java.util.Scanner;
public class ArraySorting
{
public static void main(String[] args)
{
int option;
int integer = 0;
int optionOne;
int optionTwo;
int optionThree;
int optionFour;
int optionFive;
6. Scanner kb = new Scanner(System.in);
System.out.println("Enter size of array ");
int n=kb.nextInt();
int[] numbers = new int[n];
System.out.println("Enter elements of array ");
for(int i=0;i=0 ; i--)
System.out.println(" array element in reverse are: " + numbers[i]);
}
public class SortSearchUtil
{
public void selectionSort(int[] array)
{
int current, indexSmallest, posToFill;
int temp;
for(posToFill = 0; posToFill < array.length - 1; posToFill++)
{
indexSmallest = posToFill;
for(current = posToFill + 1; current < array.length; current++)
{
if(array[current] < array[indexSmallest])
{
indexSmallest = current;
}
}
temp = array[posToFill];
array[posToFill] = array[indexSmallest];
array[indexSmallest] = temp;
}
}
public void selectionSort(String[] array)
{
int current, indexSmallest, posToFill;
7. String temp;
for (posToFill = 0; posToFill < array.length - 1; posToFill++)
{
indexSmallest = posToFill;
for (current = posToFill + 1; current < array.length; current++)
{
if (array[current].compareTo(array[indexSmallest]) <0)
{
indexSmallest = current;
}
}
temp = array[posToFill];
array[posToFill] = array[indexSmallest];
array[indexSmallest] = temp;
}
}
}
}
==========================================================
Enter size of array
3
Enter elements of array
1
2
3
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
1
enter number
4
8. 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
5
array element in reverse are: 4
array element in reverse are: 3
array element in reverse are: 2
array element in reverse are: 1
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
4
array element are: 1
array element are: 2
array element are: 3
array element are: 4
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
6
Thank you. Have a nice day.