In this assignment, you will create a JAVA program that allows the user to choos
ID: 3573730 • Letter: I
Question
In this assignment, you will 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):
o Book ID
o Book Title
o Number of books bought
o 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 10 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
You can use additional methods (optional) for other operations. Make sure your program runs until the user decides to quit the program. Your program should validate (input validation) the menu choice entered by the user, and force them to reenter a menu choice if their original input was invalid.
A bonus programming part has been provided below for extra points.
Bonus Part (Optional): 20 points We want to test the efficiency of our searching and sorting algorithms. To test the efficiency, calculate and display the execution (elapsed) time (in seconds) required for each of the Searching and Sorting techniques. Execution time is calculated from the start of a function call to the end of the function. Look up online resources on how to calculate elapsed time and using the system time library. Can you tell which searching technique is better/faster (linear search vs. binary search) and which sorting technique is better/faster (bubble sort vs. selection sort)?
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
public class BOOK {
static 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"};
static int[] bookID = {1101, 1211, 1333, 1456, 1567, 1642, 1699, 1755, 1800, 1999};
static double[] bookPrice = {112.32, 73.25, 54.00, 67.32, 135.00, 173.22, 120.00, 42.25, 32.11, 123.75};
static void display()//to display contents of book in tabular format
{
int i;
System.out.println("--------------------------------------------------------------");
for(i=0;i<bookID.length;i++)
{
System.out.println("Book Title :"+bookTitle[i]);
System.out.println("Book id :"+bookID[i]);
System.out.println("Book Price :"+bookPrice[i]);
}
System.out.println("--------------------------------------------------------------");
}
static int linearSearch(int s)
{
int i,d=-1;
for(i=0;i<bookID.length;i++)
{
if(bookID[i]==s)d=i;
}
return d;
}
static int BinarySearch(int s)
{
int a[] = new int[bookID.length];
sort(a);
int lo=0,high=bookID.length-1,mid=(lo+high)/2;
while(lo<=high)
{
if(a[mid]==s)return mid;
else if(a[mid]>s){
lo=mid+1;
}
else if(a[mid]<s)
{
high= mid-1;
}
mid = (lo+high)/2;
}
return -1;
}
static void bubblesort(int a[])//sorting bubble sort
{
int i,j,k;
for(i=0;i<a.length;i++)
{
System.out.println(a[i]);
for(j=0;j<a.length-1;j++)
{
if(a[j]>a[j+1])
{
k=a[j];//swapping
a[j]=a[j+1];
a[j+1]=k;
}
}
}
System.out.println("List sorted ");
}
static void selectionsort(int a[])//sorting selection sort
{
int i,j,min,k;
for(i=0;i<a.length;i++)
{
min =i;
for(j=i;j<a.length;j++)
{
if(a[min]>a[j])min=j;
}
k=a[i];//swapping
a[i]=a[min];
a[min]=k;
}
System.out.println("List sorted ");
}
static void sort(int[] b)
{
int i,j,k;
for(i=0;i<b.length;i++)
{
for(j=0;j<b.length-1;j++)
{
if(b[j]>b[j+1])
{
k=b[j];
b[j]=b[j+1];
b[j+1]=k;
}
}
}
}
public static void main(String[] argv)
{
int a[]=new int[10];
int i;
Random r = new Random();
for(i=0;i<10;i++)
{
a[i]= (r.nextInt(501))+1;
}
int c;
int l;
System.out.println("Random arra:");
for(l=0;l<a.length;l++)
{
System.out.println(a[l]+" ");
}
System.out.println();
Scanner sc = new Scanner(System.in);
//testing
while(true)
{ display();
System.out.println("1:To buy a book 2:Linear Search 3:Binary Search 4:Bubble Sort 5:Selection Sort 6:quit Enter choice 1|2|3|4|5|6 :");
c =sc.nextInt();
if(c==1)
{
int n,k;
System.out.print("Enter book id:");
n =sc.nextInt();
k=linearSearch(n);
if(k==-1){
System.out.println("Book not found...Visit again ");
}
else
{
int s;
System.out.print("Enter number of books you want to buy:");
s =sc.nextInt();
System.out.println("Total amount:"+s*bookPrice[k]);
}
}
else if(c==2)
{
int n;
System.out.print("Enter book id to search:");
n =sc.nextInt();
if(linearSearch(n)==-1){
System.out.println("Book not found... ");
}
else
{
System.out.println("Found");
}
}
else if(c==3)
{
int n;
System.out.print("Enter book id to search:");
n =sc.nextInt();
if(BinarySearch(n)==-1){
System.out.println("Book not found... ");
}
else
{
System.out.println("Found");
}
}
else if(c==4)
{
bubblesort(a);
int n;
for(n=0;n<a.length;n++)
{
System.out.print(a[n]+" ");
}
System.out.println();
}
else if(c==5)
{
selectionsort(a);
int n;
for(n=0;n<a.length;n++)
{
System.out.print(a[n]+" ");
}
System.out.println();
}
else
{
return ;
}
}
}
}
output:-
run:
Random arra:
193
207
99
87
219
408
462
270
333
258
--------------------------------------------------------------
Book Title :Starting out with Java
Book id :1101
Book Price :112.32
Book Title :Java Programming
Book id :1211
Book Price :73.25
Book Title :Software Structures
Book id :1333
Book Price :54.0
Book Title :Design and Analysis of Algorithms
Book id :1456
Book Price :67.32
Book Title :Computer Graphics
Book id :1567
Book Price :135.0
Book Title :Artificial Intelligence: A Modern Approach
Book id :1642
Book Price :173.22
Book Title :Probability and Statistics
Book id :1699
Book Price :120.0
Book Title :Cognitive Science
Book id :1755
Book Price :42.25
Book Title :Modern Information Retrieval
Book id :1800
Book Price :32.11
Book Title :Speech and Language Processing
Book id :1999
Book Price :123.75
--------------------------------------------------------------
1:To buy a book
2:Linear Search
3:Binary Search
4:Bubble Sort
5:Selection Sort
6:quit
Enter choice 1|2|3|4|5|6 :
4
193
99
193
207
219
258
270
333
408
462
List sorted
87 99 193 207 219 258 270 333 408 462
--------------------------------------------------------------
Book Title :Starting out with Java
Book id :1101
Book Price :112.32
Book Title :Java Programming
Book id :1211
Book Price :73.25
Book Title :Software Structures
Book id :1333
Book Price :54.0
Book Title :Design and Analysis of Algorithms
Book id :1456
Book Price :67.32
Book Title :Computer Graphics
Book id :1567
Book Price :135.0
Book Title :Artificial Intelligence: A Modern Approach
Book id :1642
Book Price :173.22
Book Title :Probability and Statistics
Book id :1699
Book Price :120.0
Book Title :Cognitive Science
Book id :1755
Book Price :42.25
Book Title :Modern Information Retrieval
Book id :1800
Book Price :32.11
Book Title :Speech and Language Processing
Book id :1999
Book Price :123.75
--------------------------------------------------------------
1:To buy a book
2:Linear Search
3:Binary Search
4:Bubble Sort
5:Selection Sort
6:quit
Enter choice 1|2|3|4|5|6 :
1
Enter book id:1642
Enter number of books you want to buy:2
Total amount:346.44
--------------------------------------------------------------
Book Title :Starting out with Java
Book id :1101
Book Price :112.32
Book Title :Java Programming
Book id :1211
Book Price :73.25
Book Title :Software Structures
Book id :1333
Book Price :54.0
Book Title :Design and Analysis of Algorithms
Book id :1456
Book Price :67.32
Book Title :Computer Graphics
Book id :1567
Book Price :135.0
Book Title :Artificial Intelligence: A Modern Approach
Book id :1642
Book Price :173.22
Book Title :Probability and Statistics
Book id :1699
Book Price :120.0
Book Title :Cognitive Science
Book id :1755
Book Price :42.25
Book Title :Modern Information Retrieval
Book id :1800
Book Price :32.11
Book Title :Speech and Language Processing
Book id :1999
Book Price :123.75
--------------------------------------------------------------
1:To buy a book
2:Linear Search
3:Binary Search
4:Bubble Sort
5:Selection Sort
6:quit
Enter choice 1|2|3|4|5|6 :6