Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN JAVA NOT C++ In this assignment, you will create a program that allows the us

ID: 3573462 • Letter: I

Question

IN JAVA NOT C++

In this assignment, you will create a program that allows the user to choose between the following menu choices (menu-driven program):

Linear Search

Binary Search

Bubble Sort

Selection Sort

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 onlinearSearch() 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

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++)
        {
            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 c;
      


       static int a[]=new int[10000];
       int i;
        Random r = new Random();
        for(i=0;i<1000;i++)
        {
            a[i]= (r.nextInt(501))+1;
        }

System.out.println("Random array: ");
        int l;
      
        for(l=0;l<a.length;l++)
                {
                    System.out.print(a[l]+" ");
              
                }
                System.out.println();
              
        Scanner sc = new Scanner(System.in);
        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;
                System.out.print("Enter book id:");
                n =sc.nextInt();
                if(linearSearch(n)==-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[n]);
                }
            }
            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 ;
            }
        }
      
    }
}