Use the template files provided in zyBooks 12.4 (and at the end of this file) to
ID: 3713193 • Letter: U
Question
Use the template files provided in zyBooks 12.4 (and at the end of this file) to complete the following exercises.
Each step of the instructions is associated with specific tests, so you can test your program as you go along.
Program Requirements
A party planner needs to do calculations for an event, based on the number of guests and the size of tables. Class files are PartyPlanner.java and Party.java
Documentation: Only the top of file descriptions an author tags are required for this program.
Test 1 (5 pts):
Within the PartyPlanner class:
Write the code to read the number of guests and table capacity (i.e. number of people that each table will hold) from the user as integers.
After reading the first input, display a "Guest input valid integer" message ? After reading the second input, display a "Table input valid integer" message
Example:
Enter number of guests:
10
Guest input valid integer Enter capacity of one table:
5
Table input valid integer
Tests 2 through 4 (10 pts, 10 pts, 5 pts):
Within the PartyPlanner class:
Add an exception handler that includes:A try block around the code that reads the user inputs.
If no exception is thrown after reading the first input, display the same "Guest input valid integer" message as before
If no exception is thrown after reading the second input, display the same "Table input valid integer" message as before
A catch block to catch the InputMismatchException thrown if the user does not enter an integer for one of the inputs.
The catch block should display "Error - did not enter an integer value"
Test 5 (10 pts):
Within the Party class:
Add a constructor that has parameters for both data fields. ? Add getters for both data fields.
Test 6 (5 pts):
Within the Party class:
Add a calcFullTables() method that will divide numGuests by tableCapacity to determine and return an integer containing how many full tables would be needed for the event (NOTE: use integer division to ignore any remainder).
Test 7 (10 pts):
Within the PartyPlanner class:
Add code to the try block in the main method to:
Create an object with the user entered values as arguments to the constructor o Use the object created to call the calcFullTables() method.
Display the results, using the getter to access numGuests, in the format:
3 full table(s) needed for party of 30 guests
Test 8 (10 pts):
Within the PartyPlanner class:
Add a second catch block that will catch the ArithmeticException that may be thrown when the calcFullTables() method is called and the table capacity is 0 during the division.
The catch block should display "Error - cannot determine number of tables when table capacity is 0"
Test 9 through 12 (25 pts total):
Within the Party class:
Within the calcFullTables() method, before doing any calculations, add code to throw an IllegalArgumentException when either data field value is negative. o The throw should create a new IllegalArgumentException with the message "Error - cannot compute tables using negative value(s)"
Within the PartyPlanner class:
Add a third catch block that will catch the IllegalArgumentException that may be thrown when the calcFullTables() method is called.
The catch block should display the message from the IllegalArgumentException object
§ Warning: Do NOT display a quoted String here
– display the message from the IllegalArgumentException object
Party.java template
public class Party {
private int numGuests;
private int tableCapacity;
// Insert your code here
}
PartyPlanner.java template
import java.util.InputMismatchException;
import java.util.Scanner;
public class PartyPlanner {
public static void main(String[] args) {
// Insert your code here
}
}
Explanation / Answer
Here is the completed code for Party and PartyPlanner classes. Implemented all the specifications mentioned in the question. Comments are included, drop a comment if you have any doubts. Thanks
// Party.java
public class Party {
private int numGuests;
private int tableCapacity;
/**
* constructor to initialize both values
*/
public Party(int numGuests, int tableCapacity) {
this.numGuests = numGuests;
this.tableCapacity = tableCapacity;
}
//getter methods for both attributes
public int getNumGuests() {
return numGuests;
}
public int getTableCapacity() {
return tableCapacity;
}
/**
* calculates and return the number of full tables needed for the party
*/
public int calcFullTables() {
if (numGuests < 0 || tableCapacity < 0) {
//negative value
throw new IllegalArgumentException(
"Error - cannot compute tables using negative value(s)");
}
int fullTablesNeeded = numGuests / tableCapacity;
return fullTablesNeeded;
}
}
// PartyPlanner.java
import java.util.InputMismatchException;
import java.util.Scanner;
public class PartyPlanner {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter number of guests: ");
//getting value for number of guests
int numGuests = scanner.nextInt();
System.out.println("Guest input valid integer");
System.out.print("Enter capacity of one table: ");
//getting value for table capacity
int tableCapacity = scanner.nextInt();
System.out.println("Table input valid integer");
/**
* creating a party object
*/
Party party = new Party(numGuests, tableCapacity);
/**
* calculating the tables needed and displaying it
*/
int tablesNeeded = party.calcFullTables();
System.out.println(tablesNeeded
+ " full table(s) needed for a party of "
+ party.getNumGuests() + " guests");
} catch (InputMismatchException e) {
System.out.println("Error - did not enter an integer value");
} catch (ArithmeticException e) {
System.out.println("Error - cannot determine number of tables"
+ " when table capacity is 0");
}catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
/*different outputs*/
Enter number of guests: 120
Guest input valid integer
Enter capacity of one table: 5
Table input valid integer
24 full table(s) needed for a party of 120 guests
Enter number of guests: -12
Guest input valid integer
Enter capacity of one table: 6
Table input valid integer
Error - cannot compute tables using negative value(s)
Enter number of guests: 345
Guest input valid integer
Enter capacity of one table: s
Error - did not enter an integer value
Enter number of guests: 980
Guest input valid integer
Enter capacity of one table: 0
Table input valid integer
Error - cannot determine number of tables when table capacity is 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.