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

In this assignment, you will create a program that allows the user to choose bet

ID: 3777070 • Letter: I

Question

In this assignment, you will create a 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) String0 bookTitle f 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"); into bookID {1212, 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).

Explanation / Answer

import java.util.Random;
import java.util.Scanner;


public class SearchSort {

    public static void main(String[] args) {
      Scanner input=new Scanner(System.in);
      int choice;
      String []bookTitle={"Starting out with java", "Java Programming",
          "Software Structures", "Design and Analysis of Algorithms", "Computer Grphics",
          "Artificial Intelligence: A Modern Approach","Probability and Statistics","Cognitive Science",
          "Modern Information Retrieval","Speech ans Language Processing"};
      int[] bookID={1212,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};
     display(bookTitle,bookID,bookPrice);
     int ID;
     int numBooks;
     int n=1000;
     int []randNumbers=new int[n];
     Random rn;
     while(true)
     {
         System.out.println("1.Linear Search");
         System.out.println("2.Binary Search");
         System.out.println("3.Bubble Sort");
         System.out.println("4.Selection Sort");
         System.out.println("5.Quit");
         System.out.println("Enter your choice: ");
         choice=input.nextInt();
         switch(choice)
         {
             case 1: System.out.println("Enter the book ID you want to purchase: ");
                     ID=input.nextInt();
                     System.out.println("Enter the number of books you want to purchase: ");
                     numBooks=input.nextInt();
                     int p=linearSearch(bookID,ID);
                 if(p>=0)
                 {
                     System.out.println("Book found.");
                     System.out.println("Book ID: "+bookID[p]);
                     System.out.println("Book Title: "+bookTitle[p]);
                     System.out.println("Number of books bought: "+numBooks);
                     System.out.println("Total cost of the purchase: $"+bookPrice[p]*numBooks);
                 }
                 else
                 {
                     System.out.println("This book ID is not available");
                 }
             break;
             case 2: System.out.println("Enter the book ID you want to purchase: ");
                     ID=input.nextInt();
                     System.out.println("Enter the number of books you want to purchase: ");
                     numBooks=input.nextInt();
                     int index=binarySearch(bookID,ID);
                     if(index>=0)
                 {
                     System.out.println("Book found.");
                     System.out.println("Book ID: "+bookID[index]);
                     System.out.println("Book Title: "+bookTitle[index]);
                     System.out.println("Number of books bought: "+numBooks);
                     System.out.println("Total cost of the purchase: $"+bookPrice[index]*numBooks);
                 }
                 else
                 {
                     System.out.println("This book ID is not available");
                 }
             break;
             case 3:
                  rn = new Random();
                 for(int i=0;i<n;i++)
                 {
                    randNumbers[i]=( rn.nextInt() % 500)+ 1;
                 }
                 bubbleSort(randNumbers);
                 break;
             case 4:
                  rn = new Random();
                 for(int i=0;i<n;i++)
                 {
                    randNumbers[i]=( rn.nextInt() % 500)+ 1;
                 }
                 selectionSort(randNumbers);
                 break;
             case 5:
                 System.exit(0);
         }
     }
      }

    private static void display(String[] bookTitle, int[] bookID, double[] bookPrice) {
        System.out.println("Title ID Price");
        for(int i=0;i<bookTitle.length;i++)
        {
            System.out.println(bookTitle[i]+" "+bookID[i]+" "+bookPrice[i]);
        }
    }

    private static int binarySearch(int[] bookID,int ID) {
      int start = 0;
        int end = bookID.length - 1;
        while (start <= end) {
            int mid = (start + end) / 2;
            if (ID == bookID[mid]) {
                return mid;
            }
            if (ID < bookID[mid]) {
                end = mid - 1;
            } else {
                start = mid + 1;
            }
        }
        return -1;
    }

    private static int linearSearch(int[] bookID, int ID) {
         int size = bookID.length;
        for(int i=0;i<size;i++){
            if(bookID[i] == ID){
                return i;
            }
        }
        return -1;

    }

private static void selectionSort(int[] randNumbers) {
        System.out.println("Before Sorting");
for(int i=0;i<randNumbers.length;i++)
{
    System.out.println(randNumbers[i] +" ");
}
for (int i = 0; i < randNumbers.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < randNumbers.length; j++)
if (randNumbers[j] < randNumbers[index])
index = j;
int smallerNumber = randNumbers[index];
randNumbers[index] = randNumbers[i];
randNumbers[i] = smallerNumber;
}
System.out.println("After Sorting");
for(int i=0;i<randNumbers.length;i++)
{
    System.out.println(randNumbers[i] +" ");
}
    }

    private static void bubbleSort(int[] randNumbers) {
          System.out.println("Before Sorting");
for(int i=0;i<randNumbers.length;i++)
{
    System.out.println(randNumbers[i] +" ");
}
       for (int c = 0; c < ( randNumbers.length - 1 ); c++) {
      for (int d = 0; d < randNumbers.length - c - 1; d++) {
        if (randNumbers[d] > randNumbers[d+1]) /* For descending order use < */
        {
          int t       = randNumbers[d];
          randNumbers[d]   = randNumbers[d+1];
          randNumbers[d+1] = t;
        }
      }
    }

    System.out.println("After Sorting");
for(int i=0;i<randNumbers.length;i++)
{
    System.out.println(randNumbers[i] +" ");
}
    }
   
}