JAVA PROGRAM: Create a java program that allows the user to choose between the f
ID: 3780175 • Letter: J
Question
JAVA PROGRAM:
Create a java program that allows the user to choose between the following menu choices (menu-driven program):
1. Linear Search ?
2. Binary Search ?
3. Bubble Sort ?
4. Selection Sort ?
5. Quit ?
Keep running the program until the user chooses to Quit. Your program should have the following parts:
Searching Algorithms (Linear and Binary Search)?You are the owner of a book store. You have the following information available in your stock (information is stored in the form of parallel arrays):
String[] bookTitle = {“Starting out with Java”, “Java Programming”, “Software Structures”, “Design and Analysis of Algorithms”, “Computer Graphics”, “Artificial Intelligence: A Modern Approach”, “Probability and Statistics”, “Cognitive Science”, “Modern Information Retrieval”, “Speech and Language Processing”};
int[] bookID = {1101, 1211, 1333, 1456, 1567, 1642, 1699, 1755, 1800, 1999};
double[] bookPrice = {112.32, 73.25, 54.00, 67.32, 135.00, 173.22, 120.00, 42.25, 32.11, 123.75};
First display() the above information to the user in a tabular format, using a void method. Your program should then ask for the book ID and the number of books the user wishes to purchase. Based on the book ID provided by the user, display the following information (if the ID is found):
Book ID ?
Book Title ?
Number of books bought ?
Total cost of the purchase ?
If the book ID is not found, display a message saying so. The book ID needs to be searched based on linearSearch()or binarySearch() (based on user choice from the menu). ?
Sorting Algorithms (Bubble and Selection Sort)
Your program should generate 1000 random numbers in the range of 1 to 500 and store them in an array. Use bubbleSort() or selectionSort() (based on the menu choice) to sort the array of numbers. Display both the unsorted and the sorted array.
Instructions:?Please make sure your code has following functions:
display(): To display the contents of parallel array in a tabular format. Take in the three different arrays as parameters.
linearSearch(): To apply the linear search algorithm to search for the book ID. Returns the index position, or -1 if not found
binarySearch(): To apply the binary search algorithm to search for the book ID. Returns the index position of the found book ID, or -1 if not found.
bubbleSort(): To apply the bubble sort algorithm to sort the elements of an unsorted array ?
selectionSort(): To apply the selection sort algorithm to sort the elements of an unsorted array ?
Sample Run: 1. Linear Search 2. Binary Search 3. Bubble Sort 4. Selection Sort 5. Quit Please enter a number between 1-5: 1 LINEAR SEARCH Book ID Title 1101 Starting out with C++ 112.32 Java Programming 1211 73.25 54 1333 Data Structures 1456 Design and Analysis of Algorithms 67.32 1567 135 Computer Graphics 1642 Artificial Intelligence: A Modern Approach 173.22 120 1699 Probability and Statistics 1755 Cognitive Science 42.25 1800 Modern Information Retrieval 32.11 123.75 1999 Speech and Language Processing CostExplanation / Answer
import java.util.Random;
import java.util.Scanner;
public class MenuDriverSearchAndSort {
public static void main(String[] args) {
String[] bookTitles = { "Starting out with Java", "Java Programming",
"Software Structures", "Design and Analysis of Algorithms",
"Computer Graphics",
"Artificial Intelligence: A Modern Approach",
"Probability and Statistics", "Cognitive Science",
"Modern Information Retrieval",
"Speech and Language Processing" };
int[] bookIDs = { 1101, 1211, 1333, 1456, 1567, 1642, 1699, 1755, 1800,
1999 };
double[] bookPrices = { 112.32, 73.25, 54.00, 67.32, 135.00, 173.22,
120.00, 42.25, 32.11, 123.75 };
int choice;
int[] numbers = new int[1000];
Scanner scanner = new Scanner(System.in);
do {
System.out.println(" ");
System.out
.println(" 1. Linear Search 2. Binary Search 3. Bubble Sort 4. Selection Sort 5. Quit ");
System.out.print("Please enter number between 1-5: ");
choice = scanner.nextInt();
int bookId, bookCount, resultIndex;
long startTime, executionTime;
Random random;
switch (choice) {
case 1:
display(bookTitles, bookIDs, bookPrices);
System.out.println(" *** LINEAR SEARCH ***");
System.out
.print("Please enter the book ID you wish to purchase from the list above: ");
bookId = scanner.nextInt();
System.out.println(" ");
System.out.print("How many books do you wish to purchase: ");
bookCount = scanner.nextInt();
startTime = System.currentTimeMillis();
resultIndex = linearSearch(bookIDs, bookId);
executionTime = System.currentTimeMillis() - startTime;
if(resultIndex !=-1){
System.out.println(" Book ID: " + bookIDs[resultIndex]);
System.out.println("Book Title: " + bookTitles[resultIndex]);
System.out.println("Book Count: " + bookCount);
System.out.println("Total Cost: " + (bookCount * bookPrices[resultIndex]) +" $");
System.out.println(" Execution of this function took " + executionTime + " milliseconds");
} else {
System.out.println("Book ID not found. Please enter a valid Book Id");
}
break;
case 2:
display(bookTitles, bookIDs, bookPrices);
System.out.println(" *** BINARY SEARCH ***");
System.out
.print("Please enter the book ID you wish to purchase from the list above: ");
bookId = scanner.nextInt();
System.out.println(" ");
System.out.print("How many books do you wish to purchase: ");
bookCount = scanner.nextInt();
startTime = System.currentTimeMillis();
resultIndex = binarySearch(bookIDs, bookId);
executionTime = System.currentTimeMillis() - startTime;
if(resultIndex !=-1){
System.out.println(" Book ID: " + bookIDs[resultIndex]);
System.out.println("Book Title: " + bookTitles[resultIndex]);
System.out.println("Book Count: " + bookCount);
System.out.println("Total Cost: " + (bookCount * bookPrices[resultIndex]) +" $");
System.out.println(" Execution of this function took " + executionTime + " milliseconds");
} else {
System.out.println("Book ID not found. Please enter a valid Book Id");
}
break;
case 3:
System.out.println(" *** BUBBLE SORT ***");
random = new Random();
for(int i=0; i<1000; i++){
numbers[i] = random.nextInt(500);
}
System.out.println("The unsorted array is: ");
for(int i=0; i<1000; i++){
System.out.print(numbers[i] + " ");
}
startTime = System.currentTimeMillis();
numbers = bubbleSort(numbers);
executionTime = System.currentTimeMillis() - startTime;
System.out.println("Sorted array is: ");
for(int i=0; i<1000; i++){
System.out.print(numbers[i] + " ");
}
System.out.println(" Execution of this function took " + executionTime + " milliseconds");
break;
case 4:
System.out.println(" *** SELECTION SORT ***");
random = new Random();
for(int i=0; i<1000; i++){
numbers[i] = random.nextInt(500);
}
System.out.println("The unsorted array is: ");
for(int i=0; i<1000; i++){
System.out.print(numbers[i] + " ");
}
startTime = System.currentTimeMillis();
numbers = selectionSort(numbers);
executionTime = System.currentTimeMillis() - startTime;
System.out.println("Sorted array is: ");
for(int i=0; i<1000; i++){
System.out.print(numbers[i] + " ");
}
System.out.println(" Execution of this function took " + executionTime + " milliseconds");
break;
}
} while (choice != 5);
scanner.close();
}
static void display(String[] bookTitle, int[] bookID, double[] bookPrice) {
System.out
.println("Book ID Title Cost");
int maxTitleLength = 50;
for (int i = 0; i < bookTitle.length; i++) {
System.out.print(bookID[i] + " " + bookTitle[i]);
int spaces = maxTitleLength - bookTitle[i].length();
for (int k = 0; k < spaces; k++) {
System.out.print(" ");
}
System.out.println(bookPrice[i]);
}
}
static int linearSearch(int[] bookIDs, int bookId) {
int resultIndex = -1;
for (int i = 0; i < bookIDs.length; i++) {
if (bookIDs[i] == bookId) {
resultIndex = i;
}
}
return resultIndex;
}
static int binarySearch(int[] bookIDs, int bookId) {
int resultIndex = -1;
int first = 0;
int last = bookIDs.length - 1;
int middle = (first + last) / 2;
while (first <= last) {
if (bookIDs[middle] < bookId) {
first = middle + 1;
} else if (bookIDs[middle] == bookId) {
resultIndex = middle;
break;
} else {
last = middle - 1;
}
middle = (first + last) / 2;
}
if (first > last)
resultIndex = -1;
return resultIndex;
}
static int[] bubbleSort(int[] arr) {
for (int i = arr.length - 1; i >= 0; i--){
// bubble up
for (int j = 0; j <= i - 1; j++) {
if (arr[j] > arr[j + 1])
swap(arr, j, j + 1);
}
}
return arr;
}
static int[] selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int min = i; // record the position of the smallest
for (int j = i + 1; j < arr.length; j++) {
// update min when finding a smaller element
if (arr[j] < arr[min])
min = j;
}
// put the smallest element at position i
swap(arr, i, min);
}
return arr;
}
public static void swap (int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.