57.0k views
4 votes
At a certain restaurant the hostess performs a manual search of the guest list to find guest reservations. Rudie has decided to write a Java program to store the list of guests for a night and search for the guest using the list. Rudie’s program had two classes Guest, which is a template for guest objects and GuestMain, which stores an array of guests called GuestList and code to search GuestList for a guest. The code for the classes that Rudie has already written is shown below. Help Rudie complete the program by: (a) Creating/ Initializing the array of objects that are stored in the one dimensional array called GuestList. There are three guests so far for today. name (string) time (integer) Deborah Henry 8 Jennifer Logan 8 Tailor Swift 7 (b) Write code to search the one dimensional array called GuestList. If the guest is on the list print the name only, otherwise print the message "Name not found" . public class Guest { private String name; private int time; /************************* Constructors **********************/ public Guest(String theName, int theTime) { name = theName; hours = theTime; } public String getName() { return name; } public String toString() { return name + ": \t" + hours ; } }// end of Guest class import java.util.Scanner; public class GuestMain { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); (a) /* Initialize an array of objects to store objects of the Guest class. The three objects are shown above. Name the array GuestList */ boolean found = false; System.out.println( "Enter a name"); String name = keyboard.nextLine(); (b) /* Write code to search the array GuestList for a name entered by a user or hostess. If the guest is on the list print the name only, otherwise print the message "Name not found" . */

1 Answer

5 votes

Final answer:

To complete Rudie's program, we initialize an array of Guest objects with three guests and write a loop to search for a guest's name in the GuestList. If found, the name is printed; otherwise, a message indicating the name was not found is displayed.

Step-by-step explanation:

To help Rudie complete their Java program for storing and searching a guest list in a restaurant, we need to perform two tasks. First, we will create and initialize an array of Guest objects. Then, we will write code to search the GuestList array for a name entered by the user and display the result.

Initializing GuestList Array

(a) We can declare the array and initialize it with the given guest information:

Guest[] GuestList = new Guest[3];
GuestList[0] = new Guest("Deborah Henry", 8);
GuestList[1] = new Guest("Jennifer Logan", 8);
GuestList[2] = new Guest("Tailor Swift", 7);

Searching the GuestList

(b) To search for a guest, we will iterate over the GuestList array:

for (Guest guest : GuestList) {
if (guest.getName().equalsIgnoreCase(name)) {
System.out.println(guest.getName());
found = true;
break;
}
}
if (!found) {
System.out.println("Name not found");
}
User Grasdy
by
8.3k points