Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab4App.Java package lab4; import java.util.Scanner; /** * Title: Lab 4 - Restaurant Program * Description: This program creates a Restaurant object, adds 3 Tables to

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Lab4App.Java

package lab4; import java.util.Scanner; /** * 

Title: Lab 4 - Restaurant Program

*

Description: This program creates a Restaurant object, adds 3 Tables to the * Restaurant and displays information about the state of the Restaurant.

* @author CSC 120 Instructor */ public class Lab4App { public static void main(String[] args) { Restaurant myFavoritePlace = new Table(); myFavoritePlace.setRestaurantName("Best Food Ever"); myFavoritePlace.determineServers(); Restaurant table1 = myFavoritePlace.makeReservation(6); table1.assignServer(); table1.calcBill(25.00); table1.calcTip(15.0); System.out.println(myFavoritePlace.toString()); } }

Restaurant.Java

package lab4; import java.util.Random; /** * 

Title: Restaurant Class

*

Description: This class defines a restaurant object by specifying the * restaurant's name, tables and servers. It can make a reservation, assign * a server to a table, determine the the number of guests and calculate * the total amount the restaurant has taken in in bills and tips.

* @author CSC 120 Instructor */ public class Restaurant { // instance variables private String rName; private Table[] tables; private int numTables; private String[] servers; /** * Restaurant default constructor * sets the instance variables to their default values */ public Restaurant() { rName = new String("No name"); tables = new Table[10]; servers = new String[3]; numTables = 0; } /** * setRestaurantName method * mutator method that sets the instance variable rName to * the parameter * @param name - the name of the restaurant */ public void setRestaurantName(String name) { rName = name; } /** * getRestaurantName method * accessor method that gets the value stored in the * instance variable rName * @return the name of the restaurant */ public String getRestaurantName() { return rName; } /** * determineServers method * specifies the restaurant's servers for the shift */ public void determineServers() { servers[0] = new String("Alex"); servers[1] = new String("Jordan"); servers[2] = new String("Chris"); } /** * assignServer method * randomly assigns a server from the list of servers * to the current table * @param t the table that needs a server */ public void assignServer(Table t) { Random generator = new Random(); t.setServer(servers[generator.nextInt(3)]); } /** * makeReservation method * creates a new table based upon the number of people and * updates the number of full tables * @param numPeople the number of people at the table * @return a reference to the Table object */ public Table makeReservation(int numPeople) { Table tbl = new Table(numPeople); if (numTables

Table.java

package lab4; /** * 

Title: Table Class

*

Description: This class defines a table object by specifying the * number of guests at the table, the server's name and table's bill and * tip left. It can calculate the bill, tip and total owed.

* @author CSC 120 Instructor */ public class Table { // instance variables private String server; private int numGuests; private double billAmt; private double tipAmt; /** * default constructor * sets default values for each of the instance variables */ public Table() { server = new String("None assigned"); numGuests = 0; billAmt = 0.0; tipAmt = 0.0; } /** * parameterized constructor * sets default values for all the instance variables * except the number of guests * @param nGuests the number of guests at this table */ public Table(int nGuests) { server = new String("None assigned"); numGuests = nGuests; billAmt = 0.0; tipAmt = 0.0; } /** * setServer method * mutator method that sets the instance variable server to * the parameter * @param sName - the name of the server */ public void setServer(String sName) { server = sName; } /** * getServer method * accessor method that gets the value stored in the * instance variable server * @return the name of the server */ public String getServer() { return server; } /** * setNumGuests method * mutator method that sets the instance variable numGuests to * the parameter * @param guests - the number of guests at the table */ public void setNumGuestsServer(int guests) { numGuests = guests; } /** * getNumGuests method * accessor method that gets the value stored in the * instance variable numGuests * @return the number of guests at the table */ public int getNumGuests() { return numGuests; } /** * calcBill method * multiplies the cost per person by the number of guests * to determine the bill amount * @param costPerPerson the cost of the meal per person */ public void calcBill(double costPerPerson) { billAmt = numGuests * costPerPerson; } /** * calcTip method * multiplies the amount of the bill by the tip percent to * determine the tip amount * @param tipPercent the percent the table wants to leave * as a tip */ public void calcTip(double tipPercent) { tipAmt = billAmt * tipPercent/100; } /** * determineTotalOwed method * adds the tip amount to the bill amount to determine the * total amount owed * @return the total amount the table owes */ public double determineTotalOwed() { return billAmt + tipAmt; } /** * toString method * creates and returns a String with the values stored in the * instance variables * @return the state of the object */ public String toString() { String str = new String(server + " has a table with " + numGuests + " guests. The bill amount is $" + billAmt + " and " + server + " has earned a $" + tipAmt + " tip."); return str; } }

Understanding Syntax Errors The Lab4App file contains the main method and is known as the application class. It is where program execution begins. It will declare reference variables for objects of the Restaurant and Table classes, store addresses of objects in those variables as well as call methods on those objects. In this lab you will only be modifying the Lab4App class. Do NOT make changes to the Restaurant and Table classes. The main method in the Lab4App class contains a number of statements. Several of these statements contain syntax errors. When debugging a program, it is important to start with the first syntax error and work your way down through the program as sometimes fixing one error will eliminate another or potentially cause another. Use the information below to answer questions #7-21 (on pages 12 & 13) when specified and to debug the program. Write a comment above each incorrect statement and explain how you fixed it. To help with readability, please put the comment on its own line directly above the statement you are referring to and place a blank line above the comment. Continue using the documentation for the Restaurant (page 2) and Table (page 3) classes to help you fix the program. 1. The first statement in the main method of the Lab4App class Restaurant my Favorite Place = new Table(); is supposed to declare and instantiate a Restaurant object. Notice that there is a red x in the left-hand margin. Place the mouse cursor over the x to read the message. Fix this statement so that it properly instantiates a Restaurant object using the default constructor and stores the reference to this object in the reference variable myFavorite Place. Save the program. Make sure that the red x disappears. Write a comment above this statement to explain the change you made and why you made it. 2. The next statement that has an error is Restaurant tablel = myFavorite Place.makeReservation(); This statement is supposed to make a reservation at the restaurant for 6 people. The method is supposed to create and return a reference to a Table object. Notice that there is a red x in the left- hand margin. Place the mouse cursor over the x to read the message. Fix this statement so that it properly stores the address of the Table object returned by the makeReservation method. Save the program. Make sure that the red x disappears. Write a comment above this statement to explain the change you made and why you made it. 3. The next statement that has an error is table1.assignServer(); If it doesn't please ask your instructor to look at the changes you made in 1 & 2. This statement is supposed to have the restaurant assign a server to the table. Place the mouse cursor over the x to read the message. Sometimes there are multiple syntax errors in a single statement. When you fix the first error, the next one will appear. Fix each of the syntax errors so that this statement is a proper method call. Save the program. Make sure the red x disappears. Write a comment above this statement to explain the changes you made and why you made them. 4. Run the program. If you fixed the syntax errors correctly, you should see something like: Best Food Ever Alex has a table with 6 guests. The bill amount is $150.0 and Alex has earned a $22.5 tip. in the console window. The server name will vary each time you run the program as it is assigned randomly. If you get red text in the console window you have a run-time error. Go back and re-read steps 1-3 to see if you can figure out your error. 5. The program has 8 statements in the main method. Read through the program to complete Writing Method Calls Now that you have the beginning of a program, write statements in the Lab4App.java file as specified in each step below. Pay attention to where in the program each statement should be placed and follow the instructions on running and debugging the program carefully. You should run your program frequently to make sure it is working properly before moving on to subsequent steps. Remember, you are only making changes in the Lab4App class - do not change the Restaurant or Table classes. 7. Locate the determine TotalOwed method in the documentation for the Table class. 8. Write statement(s) to call the determine Totalowed method on an appropriate object and save the value returned into a variable named billTotal (which needs to be declared). This statement(s) should go after the existing statements in the main method. 9. Write statement(s) to display a blank line and then the value stored in billTotal along with an appropriate label such as "Table l's bill total is $". Use the existing code for to help you with steps #10-13. 10. Write statement(s) to make a reservation for 4 guests and store the value returned into a variable named table2 (which needs to be declared). Use the existing code for tablel as an example. 11. Write statement(s) to assign a server to table2. 12. Write statement(s) to set the cost per person to 40.00 when calculating the bill and 20.0 when calculating the tip. 13. Write statement(s) to display a blank line and then the current state of the Restaurant object by calling the toString method. Your output should look similar to (realize the server names will change each execution): Best Food Ever Jordan has a table with 6 guests. The bill amount is $150.0 and Jordan has earned a $22.5 tip. Table 1's bill total is $172.5 Best Food Ever Jordan has a table with 6 guests. The bill amount is $150.0 and Jordan has earned a $22.5 tip. Alex has a table with 4 guests. The bill amount is $160.0 and Alex has earned a $32.0 tip. 14. Write statement(s) to determine the total number of guests at the restaurant by calling the calcTotalGuests method on an appropriate object and save the value returned into a variable named numGuests (which needs to be declared). 15. Write statement(s) to display a blank line and then the value stored in numGuests with an appropriate label. 16. Write statement(s) to determine the total amount the restaurant has collected by calling the calcTotalCollected method on an appropriate object and save the value returned into a variable named totalAmount (which needs to be declared). 17. Write statement(s) to display the value stored in totalAmount with an appropriate label. Your output should look similar to (realize the server names will change each execution): Best Food Ever Chris has a table with 6 guests. The bill amount is $150.0 and Chris has earned a $22.5 tip. Table 1's bill total is $172.5 Best Food Ever Chris has a table with 6 guests. The bill amount is $150.0 and Chris has earned a $22.5 tip. Alex has a table with 4 guests. The bill amount is $160. and Alex has earned a $32.0 tip. The restaurant has 10 guests. The restaurant has collected $364.5 today. 18. Write statement(s) to declare and create a Scanner object named scnr that will read input from the keyboard. Refer to your Lab2 App.java file for the code necessary to do this. 19. Write statement(s) to prompt the user to enter the number of guests, read the user input and store it in an appropriate variable. 20. Write statement(s) to make a reservation for the number of guests entered in #19. Store the address returned into a variable named table3. 21. Write statement(s) to prompt the user to enter the cost per guest, read the user input and store it in an appropriate variable. 22. Write statement(s) to calculate the bill for table3. 23. Write statement(s) to prompt the user to enter the percent tip they would like to leave, read the user input and store it in an appropriate variable. 24. Write statement(s) to calculate the tip for table3. 25. Write statement(s) to display a blank line and then the current state of the Restaurant object. 26. Run your program. Enter 3 for the number of guests, 35.0 for the cost per guest and 18.0 for the tip percent. 28. Write statement(s) to determine the total number of guests at the restaurant. Be sure to store the value returned in an appropriate variable. 29. Write statement(s) to display a blank line and then the total number of guests at the restaurant along with an appropriate label. 30. Write statement(s) to determine the total amount the restaurant has collected. Be sure to store the value returned in an appropriate variable. 31. Write statement(s) to display the total amount the restaurant has collected along with an appropriate label. When you've completed all the statements for your program, the console window should look similar to: Best Food Ever Chris has a table with 6 guests. The bill amount is $150.0 and Chris has earned a $22.5 tip. Table 1's bill total is $172.5 Best Food Ever Chris has a table with 6 guests. The bill amount is $150.0 and Chris has earned a $22.5 tip. Jordan has a table with 4 guests. The bill amount is $160.0 and Jordan has earned a $32.0 tip. The restaurant has 10 guests. The restaurant has collected $364.5 today. Please enter the number of guests 3 Please enter the cost per guest 35 Please enter the percent of the tip you would like to leave 18 Best Food Ever Chris has a table with 6 guests. The bill amount is $150.0 and Chris has earned a $22.5 tip. Jordan has a table with 4 guests. The bill amount is $160.0 and Jordan has earned a $32.0 tip. Alex has a table with 3 guests. The bill amount is $105.0 and Jordan has earned a $18.9 tip. The restaurant has 13 guests. The restaurant has collected $488.4 today. Understanding Syntax Errors The Lab4App file contains the main method and is known as the application class. It is where program execution begins. It will declare reference variables for objects of the Restaurant and Table classes, store addresses of objects in those variables as well as call methods on those objects. In this lab you will only be modifying the Lab4App class. Do NOT make changes to the Restaurant and Table classes. The main method in the Lab4App class contains a number of statements. Several of these statements contain syntax errors. When debugging a program, it is important to start with the first syntax error and work your way down through the program as sometimes fixing one error will eliminate another or potentially cause another. Use the information below to answer questions #7-21 (on pages 12 & 13) when specified and to debug the program. Write a comment above each incorrect statement and explain how you fixed it. To help with readability, please put the comment on its own line directly above the statement you are referring to and place a blank line above the comment. Continue using the documentation for the Restaurant (page 2) and Table (page 3) classes to help you fix the program. 1. The first statement in the main method of the Lab4App class Restaurant my Favorite Place = new Table(); is supposed to declare and instantiate a Restaurant object. Notice that there is a red x in the left-hand margin. Place the mouse cursor over the x to read the message. Fix this statement so that it properly instantiates a Restaurant object using the default constructor and stores the reference to this object in the reference variable myFavorite Place. Save the program. Make sure that the red x disappears. Write a comment above this statement to explain the change you made and why you made it. 2. The next statement that has an error is Restaurant tablel = myFavorite Place.makeReservation(); This statement is supposed to make a reservation at the restaurant for 6 people. The method is supposed to create and return a reference to a Table object. Notice that there is a red x in the left- hand margin. Place the mouse cursor over the x to read the message. Fix this statement so that it properly stores the address of the Table object returned by the makeReservation method. Save the program. Make sure that the red x disappears. Write a comment above this statement to explain the change you made and why you made it. 3. The next statement that has an error is table1.assignServer(); If it doesn't please ask your instructor to look at the changes you made in 1 & 2. This statement is supposed to have the restaurant assign a server to the table. Place the mouse cursor over the x to read the message. Sometimes there are multiple syntax errors in a single statement. When you fix the first error, the next one will appear. Fix each of the syntax errors so that this statement is a proper method call. Save the program. Make sure the red x disappears. Write a comment above this statement to explain the changes you made and why you made them. 4. Run the program. If you fixed the syntax errors correctly, you should see something like: Best Food Ever Alex has a table with 6 guests. The bill amount is $150.0 and Alex has earned a $22.5 tip. in the console window. The server name will vary each time you run the program as it is assigned randomly. If you get red text in the console window you have a run-time error. Go back and re-read steps 1-3 to see if you can figure out your error. 5. The program has 8 statements in the main method. Read through the program to complete Writing Method Calls Now that you have the beginning of a program, write statements in the Lab4App.java file as specified in each step below. Pay attention to where in the program each statement should be placed and follow the instructions on running and debugging the program carefully. You should run your program frequently to make sure it is working properly before moving on to subsequent steps. Remember, you are only making changes in the Lab4App class - do not change the Restaurant or Table classes. 7. Locate the determine TotalOwed method in the documentation for the Table class. 8. Write statement(s) to call the determine Totalowed method on an appropriate object and save the value returned into a variable named billTotal (which needs to be declared). This statement(s) should go after the existing statements in the main method. 9. Write statement(s) to display a blank line and then the value stored in billTotal along with an appropriate label such as "Table l's bill total is $". Use the existing code for to help you with steps #10-13. 10. Write statement(s) to make a reservation for 4 guests and store the value returned into a variable named table2 (which needs to be declared). Use the existing code for tablel as an example. 11. Write statement(s) to assign a server to table2. 12. Write statement(s) to set the cost per person to 40.00 when calculating the bill and 20.0 when calculating the tip. 13. Write statement(s) to display a blank line and then the current state of the Restaurant object by calling the toString method. Your output should look similar to (realize the server names will change each execution): Best Food Ever Jordan has a table with 6 guests. The bill amount is $150.0 and Jordan has earned a $22.5 tip. Table 1's bill total is $172.5 Best Food Ever Jordan has a table with 6 guests. The bill amount is $150.0 and Jordan has earned a $22.5 tip. Alex has a table with 4 guests. The bill amount is $160.0 and Alex has earned a $32.0 tip. 14. Write statement(s) to determine the total number of guests at the restaurant by calling the calcTotalGuests method on an appropriate object and save the value returned into a variable named numGuests (which needs to be declared). 15. Write statement(s) to display a blank line and then the value stored in numGuests with an appropriate label. 16. Write statement(s) to determine the total amount the restaurant has collected by calling the calcTotalCollected method on an appropriate object and save the value returned into a variable named totalAmount (which needs to be declared). 17. Write statement(s) to display the value stored in totalAmount with an appropriate label. Your output should look similar to (realize the server names will change each execution): Best Food Ever Chris has a table with 6 guests. The bill amount is $150.0 and Chris has earned a $22.5 tip. Table 1's bill total is $172.5 Best Food Ever Chris has a table with 6 guests. The bill amount is $150.0 and Chris has earned a $22.5 tip. Alex has a table with 4 guests. The bill amount is $160. and Alex has earned a $32.0 tip. The restaurant has 10 guests. The restaurant has collected $364.5 today. 18. Write statement(s) to declare and create a Scanner object named scnr that will read input from the keyboard. Refer to your Lab2 App.java file for the code necessary to do this. 19. Write statement(s) to prompt the user to enter the number of guests, read the user input and store it in an appropriate variable. 20. Write statement(s) to make a reservation for the number of guests entered in #19. Store the address returned into a variable named table3. 21. Write statement(s) to prompt the user to enter the cost per guest, read the user input and store it in an appropriate variable. 22. Write statement(s) to calculate the bill for table3. 23. Write statement(s) to prompt the user to enter the percent tip they would like to leave, read the user input and store it in an appropriate variable. 24. Write statement(s) to calculate the tip for table3. 25. Write statement(s) to display a blank line and then the current state of the Restaurant object. 26. Run your program. Enter 3 for the number of guests, 35.0 for the cost per guest and 18.0 for the tip percent. 28. Write statement(s) to determine the total number of guests at the restaurant. Be sure to store the value returned in an appropriate variable. 29. Write statement(s) to display a blank line and then the total number of guests at the restaurant along with an appropriate label. 30. Write statement(s) to determine the total amount the restaurant has collected. Be sure to store the value returned in an appropriate variable. 31. Write statement(s) to display the total amount the restaurant has collected along with an appropriate label. When you've completed all the statements for your program, the console window should look similar to: Best Food Ever Chris has a table with 6 guests. The bill amount is $150.0 and Chris has earned a $22.5 tip. Table 1's bill total is $172.5 Best Food Ever Chris has a table with 6 guests. The bill amount is $150.0 and Chris has earned a $22.5 tip. Jordan has a table with 4 guests. The bill amount is $160.0 and Jordan has earned a $32.0 tip. The restaurant has 10 guests. The restaurant has collected $364.5 today. Please enter the number of guests 3 Please enter the cost per guest 35 Please enter the percent of the tip you would like to leave 18 Best Food Ever Chris has a table with 6 guests. The bill amount is $150.0 and Chris has earned a $22.5 tip. Jordan has a table with 4 guests. The bill amount is $160.0 and Jordan has earned a $32.0 tip. Alex has a table with 3 guests. The bill amount is $105.0 and Jordan has earned a $18.9 tip. The restaurant has 13 guests. The restaurant has collected $488.4 today

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems 12th Asian Conference ACIIDS 2020 Phuket Thailand March 23 26 2020 Proceedings

Authors: Pawel Sitek ,Marcin Pietranik ,Marek Krotkiewicz ,Chutimet Srinilta

1st Edition

9811533792, 978-9811533792

Students also viewed these Databases questions