Write a program that allows students to schedule appointments at either 1, 2, 3, 4, 5, or 6 o’clock pm. Use an array of six strings to store the names for the time slots. Write a loop that iterates as long as the array has a free space. Within a try block, allow the user to enter a time and a name. If the time is free, put the name in the array. If the time is not free, throw a TimeInUseException. If the time is not valid, throw an InvalidTimeException. Use a catch block for each different kind of exception.
Explanation of Solution
Creating “Main.java”:
- Import required packages.
- Declare the “Main” class.
- Define the “main ()” method.
- Create an object for the “Scanner” class.
- Create a string array to store names.
- Declare a variable “totalAppointments”.
- Do the below steps till “totalAppointments” reaches 6 using “while” condition.
- Get the name from the user.
- Assign “false” to the Boolean variable “flag”.
- Do until “flag” is false using “while” condition.
- Inside “try” block,
- Get the appointment time from the user.
- Check whether the time is less than 1 or greater than 6.
- Throw an exception with a message.
- Check whether “names [appointmentTime]” is not equal to null.
- Throw an exception with a message.
- Assign “name” to the index “names[appointmentTime]”.
- Assign “true” to the variable “flag”.
- Increment the variable “totalAppointments”.
- Catch the exception “InvalidTimeException”.
- Print the message
- Catch the exception “TimeInUseException”.
- Print the message.
- Catch the exception “Exception”.
- Print the message.
- Loop from 1 to 6 using “for” loop.
- Print the makes and time for each person.
- Define the “main ()” method.
Creating “InvalidTimeException.java”:
- Define a class “InvalidTimeException” that extends “Exception”.
- Define a parameterized constructor.
- Call the parent class’s method using “super ()” by passing the message.
- Define a parameterized constructor.
Creating “TimeInUseException.java”:
- Define a class “TimeInUseException” that extends “Exception”.
- Define a parameterized constructor.
- Call the parent class’s method using “super ()” by passing the message.
- Define a parameterized constructor.
Program:
Main.java:
//Import required package
import java.util.*;
//Define the main class
public class Main
{
//Define the main method
public static void main(String[] args)
{
//Create an object for the scanner class
Scanner sc = new Scanner(System.in);
//Create a string array to store names
String names[] = new String[7];
//Declare a variable
int totalAppointments = 0;
//Do until the value reaches 6
while(totalAppointments < 6)
{
//Get the name from the user
System.out.print("\nWhat is your name? ");
String name = sc.next();
//Declare a Boolean variable
boolean flag = false;
//Do until flag is false
while(!flag)
{
//Try block
try
{
//Get the appointment time from the user
System.out.print("When would you like to make an appointment? ");
int appointmentTime = sc.nextInt();
//Check whether the appointment time is less than 1 or greater than 6
if(appointmentTime < 1 || appointmentTime > 6)
//Throw an exception
throw new InvalidTimeException("Time value not in range");
//Check if the appointment time is already fixed
if (names[appointmentTime] != null)
//Throw an exception
throw new TimeInUseException("appointment already made at that time");
//Save the name at the scheduled time index
names[appointmentTime] = name;
//Assign true
flag = true;
//Increment the variable
totalAppointments ++;
}
//Catch block
catch (InvalidTimeException e)
{
//print the message
System.out.println("Sorry, that is not a legal time");
}
//Catch block
catch (TimeInUseException e)
{
//print the message
System.out.println ("Sorry, that time is already in use");
}
//Catch block
catch (Exception e)
{
//print the message
System.out.println("Bad time format, should just be an integer");
// Use up the rest of the line
sc.nextLine();
}
}
}
//Print the statement
System.out.println("-----------------------------------\nScheduled Time\n\n");
for(int i=1; i<=6; i++)
{
//Print the names and the appointmentTime for each person
System.out.println("At " + i +"PM is " + names[i]);
}
}
}
InvalidTimeException.java:
//Define a class that throws Exception
public class InvalidTimeException extends Exception
{
//Define a parameterized constructor
public InvalidTimeException(String reason)
{
//Call the parent class's method by passing the message
super(reason);
}
}
TimeInUseException.java:
//Define a class that throws Exception
public class TimeInUseException extends Exception
{
//Define a parameterized constructor
public TimeInUseException(String reason)
{
//Call the parent class's method by passing the message
super(reason);
}
}
Output:
What is your name? Charles
When would you like to make an appointment? 6
What is your name? Adams
When would you like to make an appointment? 6
Sorry, that appointmentTime is already in use
When would you like to make an appointment? 5
What is your name? Bekkie
When would you like to make an appointment? 1
What is your name? David
When would you like to make an appointment? 4
What is your name? Sam
When would you like to make an appointment? 3
What is your name? Iris
When would you like to make an appointment? 2
-----------------------------------
Scheduled Time
At 1PM is Bekkie
At 2PM is Iris
At 3PM is Sam
At 4PM is David
At 5PM is Adams
At 6PM is Charles
Want to see more full solutions like this?
Chapter 9 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Additional Engineering Textbook Solutions
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Modern Database Management
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
SURVEY OF OPERATING SYSTEMS
Database Concepts (8th Edition)
- 14. Show that the language L= {wna (w) < Nь (w) < Nc (w)} is not context free.arrow_forward7. What language is accepted by the following generalized transition graph? a+b a+b* a a+b+c a+b 8. Construct a right-linear grammar for the language L ((aaab*ab)*).arrow_forward5. Find an nfa with three states that accepts the language L = {a^ : n≥1} U {b³a* : m≥0, k≥0}. 6. Find a regular expression for L = {vwv: v, wЄ {a, b}*, |v|≤4}.arrow_forward
- 15. The below figure (sequence of moves) shows several stages of the process for a simple initial configuration. 90 a a 90 b a 90 91 b b b b Represent the action of the Turing machine (a) move from one configuration to another, and also (b) represent in the form of arbitrary number of moves.arrow_forward12. Eliminate useless productions from Sa aA BC, AaBλ, B→ Aa, C CCD, D→ ddd Cd. Also, eliminate all unit-productions from the grammar. 13. Construct an npda that accepts the language L = {a"b":n≥0,n‡m}.arrow_forwardYou are given a rope of length n meters and scissors that can cut the rope into any two pieces. For simplification, only consider cutting the rope at an integer position by the meter metric. Each cut has a cost associated with it, c(m), which is the cost of cutting the rope at position m. (You can call c(m) at any time to return the cost value.) The goal is to cut the rope into k smaller pieces, minimizing the total cost of cutting. B Provide the pseudo-code of your dynamic programming algorithm f(n,k) that will return the minimum cost of cutting the rope of length n into k pieces. Briefly explain your algorithm. What is the benefit of using dynamic programming for this problem? What are the key principles of dynamic programming used in your algorithm?arrow_forward
- Determine whether each of the problems below is NP-Complete or P A. 3-SAT B. Traveling Salesman Problem C. Minimum Spanning Tree D. Checking if a positive integer is prime or not. E. Given a set of linear inequalities with integer variables, finding a set of values for the variables that satisfies all inequalities and maximizes or minimizes a given linear objective function.arrow_forward1. Based on our lecture on NP-Complete, can an NP-Complete problem not have a polynomial-time algorithm? Explain your answer. 2. Prove the conjecture that if any problem in NP is not polynomial-time solvable, then no NP-Complete problem is polynomial-time solvable. (You can't use Theorem 1 and 2 directly) 3. After you complete your proof in b), discuss how this conjecture can be used to solve the problem of whether P=NP.arrow_forwardBased on our lectures and the BELLMAN-FORD algorithm below, answer the following questions. BELLMAN-FORD (G, w, s) 1 INITIALIZE-SINGLE-SOURCE (G, s) 2 for i = 1 to |G. VI - 1 3 4 5 6 7 8 for each edge (u, v) = G.E RELAX(u, v, w) for each edge (u, v) = G.E if v.d> u.d+w(u, v) return FALSE return TRUE 1. What does the algorithm return? 2. Analyze the complexity of the algorithm.arrow_forward
- (Short-answer) b. Continue from the previous question. Suppose part of the data you extracted from the data warehouse is the following. Identify the missing values you think exist in the dataset. Use Column letter and Row number to refer to each missing value in the dataset. Please write down how you want to address each particular missing value (you can group them if they receive same treatment). For imputation, you do not need to calculate the exact imputed values but just describe what kind of value you want to use to impute.arrow_forwardPlease original work Locate data warehousing solutions offered by IBM, Oracle, Microsoft, and Amazon Compare and contrast the capabilities of each solution and provide several names of some organizations that utilize each of these solutions. Please cite in text references and add weblinksarrow_forwardNeed Help: Which of the following statements about confusion matrix is wrong A) Confusion matrix is a performance measure for probability prediction techniques B) Confusion matrix is derived based on classification rules with cut-off value 0.5 C) Confusion matrix is derived based on training partition to measure a model’s predictive performance D) None of the abovearrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage