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
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Programming in C
Digital Fundamentals (11th Edition)
Starting Out with Java: From Control Structures through Objects (6th Edition)
Starting Out with C++ from Control Structures to Objects (8th Edition)
Database Concepts (7th Edition)
- I could really use some help with a problem I got for coding. A screenshot showing an example would help.arrow_forwardIn Java Write an application that inputs five numbers, each between 10 and 100, inclusive. As each number is read, display it only if it’s not a duplicate of a number already read. Provide for the “worst case,” in which all five numbers are different. Use the smallest possible array to solve this problem. Display the complete set of unique values input after the user enters each new value. Rough output in screenshotarrow_forwardThis is a java assignment. I have attached it as an image file.arrow_forward
- JAVA Write a program that plays a number-guessing game to guess a secret number randomly generated within the range 1 and 10. The user will get as many numbers of tries as he/she needs to guess the number. Tell the users if their guess is right or wrong and if their guess is wrong let them try to guess the number again. You have solved a similar problem in a previous lab using a while loop. You must generate the secret number using the Java Random class. When the user guesses a number he/she will have to take an input of his guessed number from the keyboard. The user will have to take as many inputs as needed to guess the correct secret-number. That means the code will keep looping as long as the guess is different from the secret number. You also need to count the number of tries and report the count at the end. Use a do-while loop to prompt the user to guess again if the guess is wrong. Make sure that it doesn't mess up the output if you guess the secret number on the first try.…arrow_forwardIn java, create a program that will use a do while loop with a "yes/no" prompt. The program should include user input and output with getters and setters. Once the end of the loop is reached, ask the user if they'd like to do the program again. Answering "Yes" will start the program over again. Answering "No" will end the program. Answering with anything else will be an invalid input. Once the loop begins again, the program should also discard all previous inputs so that there won't be any overlapping. Use Scanner.nextLine();, it will help.arrow_forwardWrite a java program that declares an array to hold 12 integers. The contents of the array should be the values 2,4,6,8,10,12,14,16,18,20,22,24. Use a for-loop to initialize the contents of the array to these values. After initializing the array to these values, the program should display the contents of the array. Suggested output: The contents of the array is: Index Value 0 2 1 4 Etc…..arrow_forward
- Write a program that grades arithmetic quizzes as follows: (Use the below startup code for Quizzes.java and provide code as indicated) Ask the user how many questions are in the quiz. Use a for loop to load the array. Ask the user to enter the key (that is, the correct answers). There should be one answer for each question in the quiz, and each answer should be an integer. They can be entered on a single line, e.g., 34, 7, 13, 100, 8 might be the key for a 5-question quiz. You will need to store the key in an array called “key”. Ask the user to enter the student’s answers for the quiz to be graded. There needs to be one answer for each question. Note that each answer can simply be compared to the key as it is entered. If the answer is correct, add 1 to a correct answer counter. Do this in a separate for loop from the loop used to load the array. When the user has entered all of the answers to be graded, print the number correct and the percent correct. When this works, nest your for…arrow_forwardUse Java programming language Write a program that asks the user to enter 5 test grades (use an array to store them). Output the grades entered, the lowest and highest grade, the average grade, how many grades are above the average and how many are below and the letter grade for the average grade. Create a method that returns the lowest grade. Create a method that returns the highest grade. Create a method that returns the average grade. Create a method that returns how many grades were above the average. Create a method that returns how many grades were below the average. Create a method that returns the letter grade of the average (90-100 – A, 80-89 – B, 70-79 – C, < 70 – F)arrow_forwardJavaarrow_forward
- Write a program that prompts the user to enter a sequence of numbers until a -999 is entered. It will print the numbers the user entered, five number in a row,with a before the first number and after the last number in each row. For example, Enter a number (-999 to stop): 23Enter a number (-999 to stop): 15Enter a number (-999 to stop): 1Enter a number (-999 to stop): 7Enter a number (-999 to stop): 9Enter a number (-999 to stop): 21Enter a number (-999 to stop): 17Enter a number (-999 to stop): 33Enter a number (-999 to stop): -999[23 15.17 9] [21 17 33]arrow_forwardcan you plz write it in java.util.scanner formarrow_forwardUse Java code to completearrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage