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

//asked how to do this 2 days ago and no response... Need to know how for a test

ID: 3686100 • Letter: #

Question

//asked how to do this 2 days ago and no response... Need to know how for a test tommorow. Thanks

Write an error-free Java program to do the following things. This program approximates the roster for a college course. There are several people who want to be enrolled in a closed section of a very popular programming course. They will be placed on a waiting list. The program should prompt the user to input a group of names (strings). The strings will refer to people’s last names and will be entered by lastname. The user should be able to input up to 15 student names but if the user enters “done” (no quotes) then the data input ceases. The longest name will be less than 30 characters long. The program should put the student names in alphabetical order based on the last name. The program should print out the names in alphabetical order. After the names have been placed in alphabetical order, the program should prompt the user to input a name. The program checks to see if the name is on the list. If the name is on the list, then the program prints a message with the name and how many names are ahead of it on the list. If the name is not on the list, then the program should print a kind message that says that student is not on the waiting list for the course. Two bonus points if the program should calls a method that conducts a binary search of the list looking for the name. At the end, the program should print a friendly message.

Explanation / Answer

import java.util.*;

class strarr_srch
{
   public static void main(String args[])
   {
       String names[]=new String[100];
       int i=0,j;
       String s;
       Scanner scan = new Scanner(System.in);

       while(true)
       {
           System.out.println("Enter last name");
           names[i]=scan.nextLine();
  
           if(names[i].equals("done"))
               break;
           i++;
       }
       int shortestStringIndex;

       for(j=0; j < names.length - 1;j++)
        {
            shortestStringIndex = j;

            for (i=j+1 ; i<names.length; i++)
            {
//We keep track of the index to the smallest string
            if(names[i].trim().compareTo(names[shortestStringIndex].trim())<0)
            {
                shortestStringIndex = i;
            }
            }
//We only swap with the smallest string
            if(shortestStringIndex != j)
            {
            String temp = names[j];
            names[j] = names[shortestStringIndex];
            names[shortestStringIndex] = temp;
            }
        }

       System.out.println("Sorted array is: ");
      
       for(i=0;i<names.length;i++)
           System.out.println(names[i]);

       System.out.println("Enter name to search");
       s=scan.nextLine();

       for(j=0;j<=names.length;j++)
       {
                      
           if(names[j].equals(s))  
           {
               System.out.println("Name present in list. "+"Number of names before this are "+j+1);
               break;
           }
       }
       if(j==names.length)
           System.out.println("Name not found");
   }
}