hello,
this is the task i need to do:
For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The search algorithms that can be used are Linear Search and Binary Search. The sorting algorithms are bubble, selection and Insertion sort.
First, the user is asked whether he/she wants to perform a search option, a sort operation, or exit the program. If the user chooses to do searching, the program will create an array consisting of 20 randomly generated integers between 1 and 100. The users then provide a search key between 1 and 100. The program should then display the contents of the array created and inform the user if the search key is in the array or not. If it is in the array, the index where it is located should also be displayed.
If the user chooses to perform sorting, then he/she will be asked to provide the size of the array that heeds to be sorted. The size should be between 5 and 30. The user is also given an option to choose whether he/she wants to supply the numbers (between 1 and 100) to be sorted or these are to be generated randomly. If these are to be randomly generated then the program should generate the appropriate numbers, store them in an array, and display the contents of the array. Otherwise, the user will be asked to enter or input the numbers one at a time. The program should also store these numbers in an array and display the contents.
Afterwards, the user is asked to which sorting algorithm he/she wants to use. Upon selection the program then proceeds to perform the sorting algorithm and display both the unsorted and the sorted arrays.
The program should not exit after performing an operation desired by the user. It should again present the user the option of performing an operation or exit the program. It will only end or exit if the user chooses the exit option.
im kinda stuck in my code here.. and taking a hard time to solve this my binary search is not working well and i want my menu to first appear 1.Searching 2. Sorting 3. Exit im trying to modify this code to accomplish the task but im taking a hard time keeping on the track Please help me..
this is the task i need to do:
For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The search algorithms that can be used are Linear Search and Binary Search. The sorting algorithms are bubble, selection and Insertion sort.
First, the user is asked whether he/she wants to perform a search option, a sort operation, or exit the program. If the user chooses to do searching, the program will create an array consisting of 20 randomly generated integers between 1 and 100. The users then provide a search key between 1 and 100. The program should then display the contents of the array created and inform the user if the search key is in the array or not. If it is in the array, the index where it is located should also be displayed.
If the user chooses to perform sorting, then he/she will be asked to provide the size of the array that heeds to be sorted. The size should be between 5 and 30. The user is also given an option to choose whether he/she wants to supply the numbers (between 1 and 100) to be sorted or these are to be generated randomly. If these are to be randomly generated then the program should generate the appropriate numbers, store them in an array, and display the contents of the array. Otherwise, the user will be asked to enter or input the numbers one at a time. The program should also store these numbers in an array and display the contents.
Afterwards, the user is asked to which sorting algorithm he/she wants to use. Upon selection the program then proceeds to perform the sorting algorithm and display both the unsorted and the sorted arrays.
The program should not exit after performing an operation desired by the user. It should again present the user the option of performing an operation or exit the program. It will only end or exit if the user chooses the exit option.
im kinda stuck in my code here.. and taking a hard time to solve this my binary search is not working well and i want my menu to first appear 1.Searching 2. Sorting 3. Exit im trying to modify this code to accomplish the task but im taking a hard time keeping on the track Please help me..
Code:
import java.util.Scanner;
import java.util.Arrays;
import java.util.Random;
/** Generate 10 random integers in the range 0..99. */
public final class Main {
public int first, middle, last;
public int[] list;
public Scanner lim = new Scanner(System.in);
public int binarysearch(int[] list, int searchTarget) {
last = list.length - 1;
first = 0;
// while there are still elements to search through
while (first <= last) {
middle = (first + last) / 2;
// if current middle value is the search target
if (list[middle] == searchTarget) {
return middle;
} // if current middle value is less than the search target
else if (list[middle] < searchTarget) {
first = middle + 1;
} // if current middle value is larger than the search target
else {
last = middle - 1;
}
}
// return 0 if search target not found
return 0;
}
public int linearSearch(int[] a, int first, int upto, int key) {
for (int i = first; i < upto; i++) {
if (key == a[i]) {
return i; // Found key, return index.
}
}
return -1; // Failed to find key
}
public void showWhatToDoMenu(int[] arrNum, int y) {
while(true){
System.out.println("What do you want to perform choose :\n 1. Searching\n 2. Sorting\n 3. Exit\n ");
Scanner input = new Scanner(System.in);
int result;
switch(input.nextInt()){
case 1 : System.out.println("1. Linear Searching\n2. Binary Searching\n");
switch(input.nextInt()){
case 1 : result=linearSearch(arrNum, 0, arrNum.length, y);
if (result!=-1) System.out.println("I found number "+y+" on index "+result+".");
else System.out.println("Number not found.");
continue;
case 2 : result=binarysearch(arrNum, y);
if (result!=0) System.out.println("I found number "+y+" on index "+result+".");
else System.out.println("Number not found.");
continue;
}
//continue; //starts the loop again
case 2 : System.out.println("1. Bubble Sorting \n2. Selection Sorting\n3. Insertion\n");
switch (input.nextInt()){
case 1 : bublesort();
continue;
case 2 : insertion();
continue;
case 3 : selection();
continue;
}
break; //calls your sortingmethod
//continue; //starts the loop again
case 3 : System.exit(0);
break; //exists the loop
default: System.out.println(input.nextInt()+ "is not a valid charachter , please try again");
}
}//end of while loop
}
public void showMainMenu() {
System.out.println("Please Enter a Search Key: ");
int y = lim.nextInt();
int[] arrNum = new int[20];
Random randomGenerator = new Random();
for (int idx = 1; idx <= 20; ++idx) {
int randomInt = randomGenerator.nextInt(100);
arrNum[idx - 1] = randomInt;
}
System.out.println(Arrays.toString(arrNum));
System.out.println("\n");
showWhatToDoMenu(arrNum, y);
}
public static void main(String[] args) {
new Main().showMainMenu();
}
public static void binarysearch() {
throw new UnsupportedOperationException("Not yet implemented");
}
private static void bublesort() {
throw new UnsupportedOperationException("Not yet implemented");
}
private static void insertion() {
throw new UnsupportedOperationException("Not yet implemented");
}
private static void selection() {
throw new UnsupportedOperationException("Not yet implemented");
}
}
Comment