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 (7th 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)
- What are triggers and how do you invoke them on demand? Give one reference with your answer.arrow_forwardDiscuss with appropriate examples the types of relationships in a database. Give one reference with your answer.arrow_forwardDetermine the velocity error constant (k,) for the system shown. + R(s)- K G(s) where: K=1.6 A(s+B) G(s) = as²+bs C(s) where: A 14, B =3, a =6. and b =10arrow_forward
- • Solve the problem (pls refer to the inserted image)arrow_forwardWrite .php file that saves car booking and displays feedback. There are 2 buttons, which are <Book it> <Select a date>. <Select a date> button gets an input from the user, start date and an end date. Book it button can be pressed only if the start date and ending date are chosen by the user. If successful, it books cars for specific dates, with bookings saved. Booking should be in the .json file which contains all the bookings, and have the following information: Start Date. End Date. User Email. Car ID. If the car is already booked for the selected period, a failure message should be displayed, along with a button to return to the homepage. In the booking.json file, if the Car ID and start date and end date matches, it fails Use AJAX: Save bookings and display feedback without page refresh, using a custom modal (not alert).arrow_forwardWrite .php file with the html that saves car booking and displays feedback. Booking should be in the .json file which contains all the bookings, and have the following information: Start Date. End Date. User Email. Car ID. There are 2 buttons, which are <Book it> <Select a date> Book it button can be pressed only if the start date and ending date are chosen by the user. If successful, book cars for specific dates, with bookings saved. If the car is already booked for the selected period, a failure message should be displayed, along with a button to return to the homepage. Use AJAX: Save bookings and display feedback without page refresh, using a custom modal (not alert). And then add an additional feature that only free dates are selectable (e.g., calendar view).arrow_forward
- • Solve the problem (pls refer to the inserted image) and create line graph.arrow_forwardwho started the world wide webarrow_forwardQuestion No 1: (Topic: Systems for collaboration and social business The information systems function in business) How does Porter's competitive forces model help companies develop competitive strategies using information systems? • List and describe four competitive strategies enabled by information systems that firms can pursue. • Describe how information systems can support each of these competitive strategies and give examples.arrow_forward
- Data communıcatıon digital data is transmitted via analog ASK and PSK are used together to increase the number of bits transmitted a)For m=8,suggest a solution and define signal elements , and then draw signals for the following sent data data = 0 1 0 1 1 0 0 0 1 0 1 1arrow_forwardDatacommunicationData = 1 1 0 0 1 0 0 1 0 1 1 1 1 0 0a) how many bıts can be detected and corrected by this coding why prove?b)what wıll be the decision of the reciever if it recieve the following codewords why?arrow_forwardpattern recognitionPCA algor'thmarrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT