Develop a menu driven program to accomplish the task specified below. The menu i
ID: 3544100 • Letter: D
Question
Develop a menu driven program to accomplish the task specified below. The menu is shown below.
1. Help
2. Use random method to generate 100 integer from 1 to 1000. Save it to a file by name project.txt
3. Sort the project.txt value in assenting order
4. Use binary search to search for a value
5. Read the number from project.txt and display them.
6. Quit
* Illegal menu selection must be handled properly without terminating the program.
* Option 3 will invoke after 2.Your program will check for this. If option 3 selected before 2, the program must provide informational warning to the user, wait for the user to read the message and allow the user to strike a key, where the program clears the screen and displays the menu again. Apply same rule for option 4, 5. Option 3 must be selected before option 4 and option 2 must be selected before 5.
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class driver
{
public static boolean find(int arr[],int item)
{
int i=0,len = arr.length;
for(i=0;i<len;i++)
{
if(arr[i]==item)
return true;
}
return false;
}
public static void insertionSort(int array[])
{
int n = array.length;
for (int j = 1; j < n; j++)
{
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) )
{
array [i+1] = array [i];
i--;
}
array[i+1] = key;
}
}
public static int BS(int arr[],int low,int high,int item)
{
if(low>high) return -1;
else
{
int mid = (low+high)/2;
if(arr[mid]<item)
return BS(arr,mid+1,high,item);
else if(arr[mid]>item)
return BS(arr,low,mid-1,item);
else
return mid;
}
}
public static void main (String[] args) throws Exception
{
try{
File f = new File("E:/project.txt");
f.createNewFile();
int ind = 0;int check[] = new int[100];
Scanner scan = new Scanner(System.in);
Random rand = new Random();
boolean flag = true;int temp=0,c;
while(flag)
{
System.out.println("Please choose from the Menu:");
System.out.println("1. Help 2. Use random method to generate 100 integer from 1 to 1000. Save it to a file by name project.txt 3. Sort the project.txt value in assenting order 4. Use binary search to search for a value 5. Read the number from project.txt and display them. 6. Quit");
c = new Integer(scan.nextLine());
switch(c)
{
case 1:
check[ind]=1;
ind++;
break;
case 2:
check[ind] = 2;
ind++;
FileWriter W = new FileWriter(f);
int i;
for(i=0;i<100;i++)
{
W.write(Integer.toString(1+rand.nextInt(999)));
W.append(" ");
}
W.flush();
W.close();
ind++;
break;
case 3:
if(find(check,2)){}
else
{
for(c=0;c<100;c++)
System.out.println("");
System.out.println("Please select option 2 before option 3");
continue;
}
int j;
Scanner S = new Scanner(f);
int arr[] = new int[100];
for(j=0;j<100;j++)
{
arr[j] = S.nextInt();
}
S.close();
insertionSort(arr);
FileWriter w = new FileWriter(f);
for(j=0;j<100;j++)
{
System.out.println(arr[j]);
w.write(Integer.toString(arr[j]));
w.append(" ");
}
w.flush();
w.close();
check[ind]=3;
ind++;
break;
case 4:
if(find(check,3)){}
else
{
for(c=0;c<100;c++)
System.out.println("");
System.out.println("Please select option 3 before option 4");
continue;
}
Scanner s = new Scanner(f);
int A[] = new int[100];
for(j=0;j<100;j++)
{
A[j] = s.nextInt();
}
System.out.println("Please enter the number to be searched");
int item = new Integer(scan.nextLine());
int ans=BS(A,0,99,item);
if(ans==-1)
System.out.println("Element not found");
else
System.out.println("Element found at index: "+ans);
check[ind]=4;
ind++;
break;
case 5:
if(find(check,2)){}
else
{
for(c=0;c<100;c++)
System.out.println("");
System.out.println("Please select option 2 before option 5");
continue;
}
Scanner x = new Scanner(f);
int k=0,temp1 = 1+rand.nextInt(99);
for(k=0;k<temp1;k++)
{
x.nextInt();
}
System.out.println(x.nextInt());
check[ind]=5;
ind++;
break;
case 6:
flag = false;
check[ind]=7;
ind++;
break;
default:
System.out.println("Please select valid option");
break;
}
}
}catch(Exception e){}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.