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

In this assignment, we will be making a program that reads in guests\' informati

ID: 3816842 • Letter: I

Question

In this assignment, we will be making a program that reads in guests' information, and create a graduation ceremony seating with a number of rows and columns specified by a user. Then it will attempt to assign each guest to a seat in an auditorium.


Download the following files and use them for this assignment.
Assignment7.java (Do not change the content of this file.)

Save all files in the same folder.

------------------------------------------------------------------------------

After compiling Guest.java, AuditoriumSeating.java, and Assignment7.java files, you need to execute Assignment7 class.

Sample Output: (the inputs entered by a user are shown in bold)
(Make sure that your program works at least with this scenario.)

C:MyJavapplications>java Assignment7  

Please enter a number of rows for an auditorium seating.
3
Please enter a number of columns for an auditorium seating.
3
Please enter a guest information or enter "Q" to quit.
Mickey/Mouse

A guest information is read.
Mickey/Mouse
Please enter a row number where the guest wants to sit.
1
Please enter a column number where the guest wants to sit.
2

The seat at row 1 and column 2 is assigned to the guest M.M.

The current seating
--------------------
?.?. ?.?. ?.?.
?.?. ?.?. M.M.
?.?. ?.?. ?.?.

Please enter a guest information or enter "Q" to quit.
Daisy/Duck

A guest information is read.
Daisy/Duck
Please enter a row number where the guest wants to sit.
2
Please enter a column number where the guest wants to sit.
0

The seat at row 2 and column 0 is assigned to the guest D.D.

The current seating
--------------------
?.?. ?.?. ?.?.
?.?. ?.?. M.M.
D.D. ?.?. ?.?.

Please enter a guest information or enter "Q" to quit.
Clarabelle/Cow

A guest information is read.
Clarabelle/Cow
Please enter a row number where the guest wants to sit.
2
Please enter a column number where the guest wants to sit.
1

The seat at row 2 and column 1 is assigned to the guest C.C.

The current seating
--------------------
?.?. ?.?. ?.?.
?.?. ?.?. M.M.
D.D. C.C. ?.?.

Please enter a guest information or enter "Q" to quit.
Max/Goof
A guest information is read.
Max/Goof
Please enter a row number where the guest wants to sit.
0
Please enter a column number where the guest wants to sit.
0

The seat at row 0 and column 0 is assigned to the guest M.G.

The current seating
--------------------
M.G. ?.?. ?.?.
?.?. ?.?. M.M.
D.D. C.C. ?.?.

Please enter a guest information or enter "Q" to quit.
Horace/Horsecollar
A guest information is read.
Horace/Horsecollar
Please enter a row number where the guest wants to sit.
5
Please enter a column number where the guest wants to sit.
1

row or column number is not valid.
A guest Horace Horsecollar is not assigned a seat.
Please enter a guest information or enter "Q" to quit.
Sylvester/Shyster

A guest information is read.
Sylvester/Shyster
Please enter a row number where the guest wants to sit.
2
Please enter a column number where the guest wants to sit.
0

The seat at row 2 and column 0 is taken.
Please enter a guest information or enter "Q" to quit.
Snow/White

A guest information is read.
Snow/White
Please enter a row number where the guest wants to sit.
-1
Please enter a column number where the guest wants to sit.
0

row or column number is not valid.
A guest Snow White is not assigned a seat.
Please enter a guest information or enter "Q" to quit.
Jiminy/Criket

A guest information is read.
Jiminy/Criket
Please enter a row number where the guest wants to sit.
0
Please enter a column number where the guest wants to sit.
2

The seat at row 0 and column 2 is assigned to the guest J.C.

The current seating
--------------------
M.G. ?.?. J.C.
?.?. ?.?. M.M.
D.D. C.C. ?.?.

Please enter a guest information or enter "Q" to quit.
Q

1. First, you need to create Guestjava file by defining Guest class. It should have two instance variables, lastName (String) and firstName (String). In addition, the following methods should be defined. Method Description of the Method Constructs a Guest object by assigning the default string to both instance public Guest variables, lastName and firstName. Constructs a Guest object using the string containing guest's info. Use the split public method of the String class to extract first name and last name, then assign them to Guest (String guestInfo) each instance variable of the Guest class. An example of the input string is David Johnson public String get LastName O It should return the instance variable lastName. public String getFirstName O It should return the instance variable firstName. It should constructor a string containing the initial character of the first name, a period, the initial character of the last name, and a period, then it returns it. public String toString An example of such string for the guest David Johnson is: D.

Explanation / Answer

//java tester class of Guest and seating classes
//Assignment7.java
import java.util.Scanner;
public class Assignment7
{
   public static void main(String[] args)
   {
       //declare variables
       AuditoriumSeating seating;
       Guest tempGuest;
       int row, col, rowNum, columnNum;
       String guestInfo;
       //create an instance of Scanner
       Scanner scanner = new Scanner(System.in);
       // prompt row number
       System.out.println("Please enter a number of rows for an auditorium seating.");
       rowNum = scanner.nextInt();


       // prompt col number
       System.out.println("Please enter a number of columns for an auditorium seating.");
       columnNum = scanner.nextInt();

       // instantiate a seating object
       seating = new AuditoriumSeating(rowNum, columnNum);

       System.out.println("Please enter a guest information or enter "Q" to quit.");      
       guestInfo = scanner.next();

       // continue until user enter Q to stop
       while (!guestInfo.equalsIgnoreCase("Q"))
       {
           System.out.println(" A guest information is read.");
           //print guest information
           System.out.println(guestInfo);

           // instantiate the Guest object with guestInfo
           tempGuest = new Guest(guestInfo);

           // Ask a user to decide where to seat a guest by asking for row and column of a seat
           System.out.println("Please enter a row number where the guest wants to sit.");
           row = scanner.nextInt();

           System.out.println("Please enter a column number where the guest wants to sit.");
           col = scanner.nextInt();

           //check if seat is valid
           if (seating.checkBoundaries(row, col) == false)
           {
               System.out.println(" row or column number is not valid.");
               System.out.println("A guest " + tempGuest.getFirstName() + " " + tempGuest.getLastName() + " is not assigned a seat.");
           }
           else
           {
               // Assigning a seat for a guest
               if (seating.assignGuestAt(row, col, tempGuest) == true)
               {
                   System.out.println(" The seat at row " + row + " and column " + col + " is assigned to the guest " + tempGuest.toString());
                   System.out.println(seating);
               }
               else
               {
                   System.out.println(" The seat at row " + row + " and column " + col + " is taken.");
               }
           }          
           System.out.println("Please enter a guest information or enter "Q" to quit.");          
           guestInfo = scanner.next();

       }
   }

}

-------------------------------------------------------------------------------------------------------------------


//Guest.java
public class Guest
{
   private String firstName;
   private String lastName;

   //constructor Guess
   public Guest()
   {
       firstName="???";
       lastName="???";
   }

   //constructor Guess
   public Guest(String guestInfo)
   {
       String names[]=guestInfo.split("/");
       this.firstName=names[0];
       this.lastName=names[1];
   }
  
   //Returns last name
   public String getLastName()
   {
       return lastName;
   }
  
   //Returns first name
   public String getFirstName()
   {
       return firstName;
   }
  
   //Returns the lastname and first intialils
   public String toString() {      
       return lastName.charAt(0)+"."+
               firstName.charAt(0);
   }

}

-------------------------------------------------------------------------------------------------------------------

//AuditoriumSeating.java
public class AuditoriumSeating
{  
   //instance variables of class
   private int row;
   private int col;
   private Guest seating[][];
  
  
   //default Constructor
   public AuditoriumSeating(int rowNum,int columnNum)
   {
       row=rowNum;
       col=columnNum;
       seating=new Guest[row][col];
      
       for (int i = 0; i < seating.length; i++)
       {
           for (int j = 0; j < seating[i].length; j++)
           {
               seating[i][j]=new Guest();
           }
       }
   }

   //Returns the guest at
   private Guest getGuestAt(int row, int col)
   {
       return seating[row][col];
   }

   //Method to set guest at given location
   public boolean assignGuestAt(int row,int col,
           Guest tempGuest)
   {
       if(!checkBoundaries(row, col))
       {
           if(seating[row][col].getFirstName().equals("???")&&
                   seating[row][col].getLastName().equals("???"))
               return false;              
       }
       seating[row][col]=tempGuest;

       return true;

   }
  
   //Method that returns true if row and col is within
   //boundaries
   public boolean checkBoundaries(int row,int col)
   {
       if(row<this.row || col>this.col)
           return true;
       else
           return false;
   }
  
   //Returns the string represenation of Auditorum
   public String toString() {
       String seatingInfo="";
      
       for (int i = 0; i < seating.length; i++)
       {
           for (int j = 0; j < seating[i].length; j++)
           {
               seatingInfo+=getGuestAt(i, j);
           }
           seatingInfo+=" ";
       }
       return seatingInfo;
   }
}//end of the class

-------------------------------------------------------------------------------------------------------------------

Sample Output:

Please enter a number of rows for an auditorium seating.
3
Please enter a number of columns for an auditorium seating.
3
Please enter a guest information or enter "Q" to quit.
Mickey/Mouse

A guest information is read.
Mickey/Mouse
Please enter a row number where the guest wants to sit.
1
Please enter a column number where the guest wants to sit.
2

The seat at row 1 and column 2 is assigned to the guest M.M
?.??.??.?
?.??.?M.M
?.??.??.?

Please enter a guest information or enter "Q" to quit.
Daisy/Duck

A guest information is read.
Daisy/Duck
Please enter a row number where the guest wants to sit.
2
Please enter a column number where the guest wants to sit.
0

The seat at row 2 and column 0 is assigned to the guest D.D
?.??.??.?
?.??.?M.M
D.D?.??.?

Please enter a guest information or enter "Q" to quit.
Clarabelle/Cow

A guest information is read.
Clarabelle/Cow
Please enter a row number where the guest wants to sit.
2
Please enter a column number where the guest wants to sit.
1

The seat at row 2 and column 1 is assigned to the guest C.C
?.??.??.?
?.??.?M.M
D.DC.C?.?

Please enter a guest information or enter "Q" to quit.
Max/Goof

A guest information is read.
Max/Goof
Please enter a row number where the guest wants to sit.
0
Please enter a column number where the guest wants to sit.
0

The seat at row 0 and column 0 is assigned to the guest G.M
G.M?.??.?
?.??.?M.M
D.DC.C?.?

Please enter a guest information or enter "Q" to quit.
Horace/Horsecollar

A guest information is read.
Horace/Horsecollar
Please enter a row number where the guest wants to sit.
5
Please enter a column number where the guest wants to sit.
1

row or column number is not valid.
A guest Horace Horsecollar is not assigned a seat.
Please enter a guest information or enter "Q" to quit.
Jimmy/Criket

A guest information is read.
Jimmy/Criket
Please enter a row number where the guest wants to sit.
0
Please enter a column number where the guest wants to sit.
2

The seat at row 0 and column 2 is assigned to the guest C.J
G.M?.?C.J
?.??.?M.M
D.DC.C?.?

Please enter a guest information or enter "Q" to quit.
Q