SlideShare a Scribd company logo
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("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;
}
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;
}
}
}
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;
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;
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.

More Related Content

Similar to Create a menu-driven program that will accept a collection of non-ne.pdf

ch07-arrays.ppt
ch07-arrays.pptch07-arrays.ppt
ch07-arrays.ppt
Mahyuddin8
 
The java code works, I just need it to display the results as in t.pdf
The java code works, I just need it to display the results as in t.pdfThe java code works, I just need it to display the results as in t.pdf
The java code works, I just need it to display the results as in t.pdf
akaluza07
 
This is a lab for a java program that I am very unsure of, it has to.pdf
This is a lab for a java program that I am very unsure of, it has to.pdfThis is a lab for a java program that I am very unsure of, it has to.pdf
This is a lab for a java program that I am very unsure of, it has to.pdf
optokunal1
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdf
optokunal1
 
DS LAB RECORD.docx
DS LAB RECORD.docxDS LAB RECORD.docx
DS LAB RECORD.docx
davinci54
 
9781439035665 ppt ch09
9781439035665 ppt ch099781439035665 ppt ch09
9781439035665 ppt ch09Terry Yoast
 
Write a program that will test a name) method no sorting routine from.docx
 Write a program that will test a name) method no sorting routine from.docx Write a program that will test a name) method no sorting routine from.docx
Write a program that will test a name) method no sorting routine from.docx
ajoy21
 
Sharable_Java_Python.pdf
Sharable_Java_Python.pdfSharable_Java_Python.pdf
Sharable_Java_Python.pdf
ICADCMLTPC
 
Program 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdfProgram 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdf
ezhilvizhiyan
 
ArrayTest.java import java.util.Arrays; import java.util.Scann.pdf
ArrayTest.java import java.util.Arrays; import java.util.Scann.pdfArrayTest.java import java.util.Arrays; import java.util.Scann.pdf
ArrayTest.java import java.util.Arrays; import java.util.Scann.pdf
deepua8
 
Oot practical
Oot practicalOot practical
Oot practical
Vipin Rawat @ daya
 
Computer java programs
Computer java programsComputer java programs
Computer java programs
ADITYA BHARTI
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
KimVeeL
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
info309708
 

Similar to Create a menu-driven program that will accept a collection of non-ne.pdf (15)

ch07-arrays.ppt
ch07-arrays.pptch07-arrays.ppt
ch07-arrays.ppt
 
The java code works, I just need it to display the results as in t.pdf
The java code works, I just need it to display the results as in t.pdfThe java code works, I just need it to display the results as in t.pdf
The java code works, I just need it to display the results as in t.pdf
 
This is a lab for a java program that I am very unsure of, it has to.pdf
This is a lab for a java program that I am very unsure of, it has to.pdfThis is a lab for a java program that I am very unsure of, it has to.pdf
This is a lab for a java program that I am very unsure of, it has to.pdf
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdf
 
DS LAB RECORD.docx
DS LAB RECORD.docxDS LAB RECORD.docx
DS LAB RECORD.docx
 
9781439035665 ppt ch09
9781439035665 ppt ch099781439035665 ppt ch09
9781439035665 ppt ch09
 
Write a program that will test a name) method no sorting routine from.docx
 Write a program that will test a name) method no sorting routine from.docx Write a program that will test a name) method no sorting routine from.docx
Write a program that will test a name) method no sorting routine from.docx
 
Sharable_Java_Python.pdf
Sharable_Java_Python.pdfSharable_Java_Python.pdf
Sharable_Java_Python.pdf
 
Program 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdfProgram 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdf
 
ArrayTest.java import java.util.Arrays; import java.util.Scann.pdf
ArrayTest.java import java.util.Arrays; import java.util.Scann.pdfArrayTest.java import java.util.Arrays; import java.util.Scann.pdf
ArrayTest.java import java.util.Arrays; import java.util.Scann.pdf
 
Oot practical
Oot practicalOot practical
Oot practical
 
Computer java programs
Computer java programsComputer java programs
Computer java programs
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
 
Java Unit 1 Project
Java Unit 1 ProjectJava Unit 1 Project
Java Unit 1 Project
 

More from rajeshjangid1865

write Ocaml programe to add all numbers in a list the solution .pdf
write Ocaml programe to add all numbers in a list the solution .pdfwrite Ocaml programe to add all numbers in a list the solution .pdf
write Ocaml programe to add all numbers in a list the solution .pdf
rajeshjangid1865
 
why is lifelong learning important for Engineers Give an example to.pdf
why is lifelong learning important for Engineers Give an example to.pdfwhy is lifelong learning important for Engineers Give an example to.pdf
why is lifelong learning important for Engineers Give an example to.pdf
rajeshjangid1865
 
Which of the following is true of aldol reactions1.The thermodyna.pdf
Which of the following is true of aldol reactions1.The thermodyna.pdfWhich of the following is true of aldol reactions1.The thermodyna.pdf
Which of the following is true of aldol reactions1.The thermodyna.pdf
rajeshjangid1865
 
Using at least two examples (whenever applicable), concisely discuss .pdf
Using at least two examples (whenever applicable), concisely discuss .pdfUsing at least two examples (whenever applicable), concisely discuss .pdf
Using at least two examples (whenever applicable), concisely discuss .pdf
rajeshjangid1865
 
Transforming Cultures from Consumerism to Sustainability - Essay.pdf
Transforming Cultures from Consumerism to Sustainability - Essay.pdfTransforming Cultures from Consumerism to Sustainability - Essay.pdf
Transforming Cultures from Consumerism to Sustainability - Essay.pdf
rajeshjangid1865
 
Trane has 145 marbles. He gives 20 to Katie, 52 to Gwen, and 31 to Yu.pdf
Trane has 145 marbles. He gives 20 to Katie, 52 to Gwen, and 31 to Yu.pdfTrane has 145 marbles. He gives 20 to Katie, 52 to Gwen, and 31 to Yu.pdf
Trane has 145 marbles. He gives 20 to Katie, 52 to Gwen, and 31 to Yu.pdf
rajeshjangid1865
 
This project should be done in C# using Visual Studio - Windows Form.pdf
This project should be done in C# using Visual Studio - Windows Form.pdfThis project should be done in C# using Visual Studio - Windows Form.pdf
This project should be done in C# using Visual Studio - Windows Form.pdf
rajeshjangid1865
 
The table below gives the probabilities of combinations of religion a.pdf
The table below gives the probabilities of combinations of religion a.pdfThe table below gives the probabilities of combinations of religion a.pdf
The table below gives the probabilities of combinations of religion a.pdf
rajeshjangid1865
 
The effects Poverty in SocietySolution Poor children are at gre.pdf
The effects Poverty in SocietySolution  Poor children are at gre.pdfThe effects Poverty in SocietySolution  Poor children are at gre.pdf
The effects Poverty in SocietySolution Poor children are at gre.pdf
rajeshjangid1865
 
Suppose 1.01g of FeCl3 is placed in a 10.0ml volumetric glass, water.pdf
Suppose 1.01g of FeCl3 is placed in a 10.0ml volumetric glass, water.pdfSuppose 1.01g of FeCl3 is placed in a 10.0ml volumetric glass, water.pdf
Suppose 1.01g of FeCl3 is placed in a 10.0ml volumetric glass, water.pdf
rajeshjangid1865
 
Specialized regions on the cell surface through which cells are joine.pdf
Specialized regions on the cell surface through which cells are joine.pdfSpecialized regions on the cell surface through which cells are joine.pdf
Specialized regions on the cell surface through which cells are joine.pdf
rajeshjangid1865
 
Section 404 of the Sarbanes Oxley Act requires auditors of a public .pdf
Section 404 of the Sarbanes Oxley Act requires auditors of a public .pdfSection 404 of the Sarbanes Oxley Act requires auditors of a public .pdf
Section 404 of the Sarbanes Oxley Act requires auditors of a public .pdf
rajeshjangid1865
 
Reiji and Tuneko Okazaki conducted a now classic experiment in 1968 .pdf
Reiji and Tuneko Okazaki conducted a now classic experiment in 1968 .pdfReiji and Tuneko Okazaki conducted a now classic experiment in 1968 .pdf
Reiji and Tuneko Okazaki conducted a now classic experiment in 1968 .pdf
rajeshjangid1865
 
Problem 2-1A Suppose the following items are taken from the 2017 bala.pdf
Problem 2-1A Suppose the following items are taken from the 2017 bala.pdfProblem 2-1A Suppose the following items are taken from the 2017 bala.pdf
Problem 2-1A Suppose the following items are taken from the 2017 bala.pdf
rajeshjangid1865
 
Prepare a 2017 income statement for Shanta Corporation based on the f.pdf
Prepare a 2017 income statement for Shanta Corporation based on the f.pdfPrepare a 2017 income statement for Shanta Corporation based on the f.pdf
Prepare a 2017 income statement for Shanta Corporation based on the f.pdf
rajeshjangid1865
 
Organizations need to have a pool of managerial talent to take on jo.pdf
Organizations need to have a pool of managerial talent to take on jo.pdfOrganizations need to have a pool of managerial talent to take on jo.pdf
Organizations need to have a pool of managerial talent to take on jo.pdf
rajeshjangid1865
 
Objective Manipulate the Linked List Pointer.Make acopy of LList..pdf
Objective Manipulate the Linked List Pointer.Make acopy of LList..pdfObjective Manipulate the Linked List Pointer.Make acopy of LList..pdf
Objective Manipulate the Linked List Pointer.Make acopy of LList..pdf
rajeshjangid1865
 
Militarism Alliances Imperialism Nationalism Class, the powder keg.pdf
Militarism Alliances Imperialism Nationalism Class, the powder keg.pdfMilitarism Alliances Imperialism Nationalism Class, the powder keg.pdf
Militarism Alliances Imperialism Nationalism Class, the powder keg.pdf
rajeshjangid1865
 
In the subject of cryptography, what policy or organizational challe.pdf
In the subject of cryptography, what policy or organizational challe.pdfIn the subject of cryptography, what policy or organizational challe.pdf
In the subject of cryptography, what policy or organizational challe.pdf
rajeshjangid1865
 
is Google making us stupid Nicholas Carr Summarize Article. https.pdf
is Google making us stupid Nicholas Carr Summarize Article. https.pdfis Google making us stupid Nicholas Carr Summarize Article. https.pdf
is Google making us stupid Nicholas Carr Summarize Article. https.pdf
rajeshjangid1865
 

More from rajeshjangid1865 (20)

write Ocaml programe to add all numbers in a list the solution .pdf
write Ocaml programe to add all numbers in a list the solution .pdfwrite Ocaml programe to add all numbers in a list the solution .pdf
write Ocaml programe to add all numbers in a list the solution .pdf
 
why is lifelong learning important for Engineers Give an example to.pdf
why is lifelong learning important for Engineers Give an example to.pdfwhy is lifelong learning important for Engineers Give an example to.pdf
why is lifelong learning important for Engineers Give an example to.pdf
 
Which of the following is true of aldol reactions1.The thermodyna.pdf
Which of the following is true of aldol reactions1.The thermodyna.pdfWhich of the following is true of aldol reactions1.The thermodyna.pdf
Which of the following is true of aldol reactions1.The thermodyna.pdf
 
Using at least two examples (whenever applicable), concisely discuss .pdf
Using at least two examples (whenever applicable), concisely discuss .pdfUsing at least two examples (whenever applicable), concisely discuss .pdf
Using at least two examples (whenever applicable), concisely discuss .pdf
 
Transforming Cultures from Consumerism to Sustainability - Essay.pdf
Transforming Cultures from Consumerism to Sustainability - Essay.pdfTransforming Cultures from Consumerism to Sustainability - Essay.pdf
Transforming Cultures from Consumerism to Sustainability - Essay.pdf
 
Trane has 145 marbles. He gives 20 to Katie, 52 to Gwen, and 31 to Yu.pdf
Trane has 145 marbles. He gives 20 to Katie, 52 to Gwen, and 31 to Yu.pdfTrane has 145 marbles. He gives 20 to Katie, 52 to Gwen, and 31 to Yu.pdf
Trane has 145 marbles. He gives 20 to Katie, 52 to Gwen, and 31 to Yu.pdf
 
This project should be done in C# using Visual Studio - Windows Form.pdf
This project should be done in C# using Visual Studio - Windows Form.pdfThis project should be done in C# using Visual Studio - Windows Form.pdf
This project should be done in C# using Visual Studio - Windows Form.pdf
 
The table below gives the probabilities of combinations of religion a.pdf
The table below gives the probabilities of combinations of religion a.pdfThe table below gives the probabilities of combinations of religion a.pdf
The table below gives the probabilities of combinations of religion a.pdf
 
The effects Poverty in SocietySolution Poor children are at gre.pdf
The effects Poverty in SocietySolution  Poor children are at gre.pdfThe effects Poverty in SocietySolution  Poor children are at gre.pdf
The effects Poverty in SocietySolution Poor children are at gre.pdf
 
Suppose 1.01g of FeCl3 is placed in a 10.0ml volumetric glass, water.pdf
Suppose 1.01g of FeCl3 is placed in a 10.0ml volumetric glass, water.pdfSuppose 1.01g of FeCl3 is placed in a 10.0ml volumetric glass, water.pdf
Suppose 1.01g of FeCl3 is placed in a 10.0ml volumetric glass, water.pdf
 
Specialized regions on the cell surface through which cells are joine.pdf
Specialized regions on the cell surface through which cells are joine.pdfSpecialized regions on the cell surface through which cells are joine.pdf
Specialized regions on the cell surface through which cells are joine.pdf
 
Section 404 of the Sarbanes Oxley Act requires auditors of a public .pdf
Section 404 of the Sarbanes Oxley Act requires auditors of a public .pdfSection 404 of the Sarbanes Oxley Act requires auditors of a public .pdf
Section 404 of the Sarbanes Oxley Act requires auditors of a public .pdf
 
Reiji and Tuneko Okazaki conducted a now classic experiment in 1968 .pdf
Reiji and Tuneko Okazaki conducted a now classic experiment in 1968 .pdfReiji and Tuneko Okazaki conducted a now classic experiment in 1968 .pdf
Reiji and Tuneko Okazaki conducted a now classic experiment in 1968 .pdf
 
Problem 2-1A Suppose the following items are taken from the 2017 bala.pdf
Problem 2-1A Suppose the following items are taken from the 2017 bala.pdfProblem 2-1A Suppose the following items are taken from the 2017 bala.pdf
Problem 2-1A Suppose the following items are taken from the 2017 bala.pdf
 
Prepare a 2017 income statement for Shanta Corporation based on the f.pdf
Prepare a 2017 income statement for Shanta Corporation based on the f.pdfPrepare a 2017 income statement for Shanta Corporation based on the f.pdf
Prepare a 2017 income statement for Shanta Corporation based on the f.pdf
 
Organizations need to have a pool of managerial talent to take on jo.pdf
Organizations need to have a pool of managerial talent to take on jo.pdfOrganizations need to have a pool of managerial talent to take on jo.pdf
Organizations need to have a pool of managerial talent to take on jo.pdf
 
Objective Manipulate the Linked List Pointer.Make acopy of LList..pdf
Objective Manipulate the Linked List Pointer.Make acopy of LList..pdfObjective Manipulate the Linked List Pointer.Make acopy of LList..pdf
Objective Manipulate the Linked List Pointer.Make acopy of LList..pdf
 
Militarism Alliances Imperialism Nationalism Class, the powder keg.pdf
Militarism Alliances Imperialism Nationalism Class, the powder keg.pdfMilitarism Alliances Imperialism Nationalism Class, the powder keg.pdf
Militarism Alliances Imperialism Nationalism Class, the powder keg.pdf
 
In the subject of cryptography, what policy or organizational challe.pdf
In the subject of cryptography, what policy or organizational challe.pdfIn the subject of cryptography, what policy or organizational challe.pdf
In the subject of cryptography, what policy or organizational challe.pdf
 
is Google making us stupid Nicholas Carr Summarize Article. https.pdf
is Google making us stupid Nicholas Carr Summarize Article. https.pdfis Google making us stupid Nicholas Carr Summarize Article. https.pdf
is Google making us stupid Nicholas Carr Summarize Article. https.pdf
 

Recently uploaded

PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 

Recently uploaded (20)

PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 

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.