Develop a menu driven program in java to accomplish the task specified below. 1.
ID: 3769186 • Letter: D
Question
Develop a menu driven program in java to accomplish the task specified below.
1. Help- Brief explanation of program
2. Create an Array - Create an array of size 100 and fill it up with 100 numbers from 1 to 100 using random class.
3. Sort- Sort the array in assenting order
4. Search – Use binary Search a value in your array.
5. Display - Display the content of array
6. Quit- Quit the Program Illegal menu selection must be handled properly without terminating the program
JGrasp is the program used to compile and run
Explanation / Answer
<code>
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class rand {
public static void main(String[]args){
Scanner in = new Scanner(System.in);
int arr[] = null;
while(true){
System.out.println("Press 1 for Help");
System.out.println("Press 2 for Create an Array");
System.out.println("Press 3 for Sort");
System.out.println("Press 4 for Search");
System.out.println("Press 5 for Display");
System.out.println("Press 6 for Quit");
int n = in.nextInt();
switch(n){
case 1:
System.out.println("First create an array by pressing 2 and then proceed to various operations");
break;
case 2:
Random rand = new Random();
arr = new int[100];
for(int i = 0;i < 100;i++){
arr[i] = rand.nextInt(100) + 1;
}
break;
case 3:
Arrays.sort(arr);
break;
case 4:
System.out.println("Enter the number to be searched");
int number = in.nextInt();
int retVal = Arrays.binarySearch(arr,number);
if (retVal < 0){
System.out.println("Element not found");
}else{
System.out.println("Element found at posistion:"+retVal);
}
break;
case 5:
for(int i = 0;i < 100;i++){
System.out.print(arr[i] + " ");
}
break;
case 6:
System.exit(0);
break;
}
}
}
}
</code>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.