Create a menu-driven program that will accept a collection of non-negative integ
ID: 3790611 • 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. 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(" 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");
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;
}
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;
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;
}
}
}
Explanation / Answer
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;
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<n;i++)
{
integer=kb.nextInt();
if(integer<0)
{
System.out.println("Enter positive number ");
i=i-1;
}
else
{
numbers[i]=integer;
}
}
option = displayMenu(kb);
while (option != 6)
{
switch (option)
{
case 1:
numbers=optionOne(numbers);
System.out.println("");
break;
case 2:
optionTwo(numbers);
System.out.println("");
case 3:
optionThree(numbers);
System.out.println("");
break;
case 4:
optionFour(numbers);
System.out.println("");
break;
case 5:
optionFive(numbers);
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(" 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");
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(int numbers[])
{
System.out.println("enter number");
Scanner input = new Scanner(System.in);
int newn=input.nextInt();
Arrays.sort(numbers);
int index = Arrays.binarySearch(numbers, newn);
int newIndex = -index - 1;
numbers = insertElement(numbers, newn, newIndex);
return numbers;
}
private static int[] insertElement(int original[], int element, int index) {
int length = original.length;
int destination[] = new int[length + 1];
System.arraycopy(original, 0, destination, 0, index);
destination[index] = element;
System.arraycopy(original, index, destination, index + 1, length - index);
return destination;
}
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);
}
private static void optionThree(int[] numbers)
{
System.out.println("Median value of array element is: " + numbers[(numbers.length-1)/2]);
}
private static void optionFour(int[] numbers)
{
for(int i=0; i <numbers.length; i++)
System.out.println(" array element are: " + numbers[i]);
}
private static void optionFive(int[] numbers)
{
for(int i=numbers.length-1; 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;
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
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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.