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
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)
- Write a program that declares an array of 15 integers and reads the values from the user. Make sure that the user doesn’t enter a value greater than 10 (if the user enters a value not in the allowed range, discard that value and read another to put into the array). Now tell the number that occur maximum number of times in the array. For example for the following 7 has maximum occurrence. 4,7,3,3,2,7,1,5,7,9,9,9,7,8,10 Use both linear and binary search.arrow_forwardLottery.java, to generate a lottery of a two-digitnumber. The two digits in the number are distinct. (Hint: Generate the first digit.Use a loop to continuously generate the second digit until it is different from thefirst digit.)arrow_forwardWrite an application that displays a series of at least five student ID numbers (that you have stored in an array) and asks the user to enter a numeric test score for the student. Create a ScoreException class, and throw a ScoreException for the class if the user does not enter a valid score (less than or equal to 100). Catch the ScoreException and then display an appropriate message. In addition, store a 0 for the student’s score. At the end of the application, display all the student IDs and scores. Save the files as ScoreException.java and TestScore.java.arrow_forward
- I am just started to learn Java. My professor has gone over all our projects so I feel comfortable with the honor code to ask for help in designing this. Thank you. In Java, Design a program that generates a 7-digit lottery number. The program should have an integer array with 7 elements. Write a loop that steps through the array, randomly generating a number in the range of 0 through 9 for each element of the array. Write another loop that displays the contents of the array.arrow_forwardProgramming in C language.arrow_forwardI could really use some help with a problem I got for coding. A screenshot showing an example would help.arrow_forward
- In 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_forwardDesign a program that asks the user to enter a store’s sales for each day of a 7 day week. The amounts should be stored in an array. Use a loop to calculate the total sales for the week, the average sales per day, and also display the list of values. This is for java.arrow_forwardWrite a program that simulates rolling two dice. Prompt the user to enter the number of dice rolls. Use a loop to repeatedly call a method that simulates a dice roll and returns the total of the two dice to main. Keep track of the rolls in an array in main, and end the program by showing the results of the rolls.arrow_forward
- Programming in C language. AskForNumbers Declare an integer array lacally with the size of 200. Create a program that asks the user how many numbers the have. Use your getChoice() function from before. Make sure it does not exceed 200 as the locally declared array has the size of 200. Use a for loop and ask the user to enter each value that must be stored in the array. Use a second loop to display each number, and also determine the average of all values in the array. After the for loop, display the average of all numbers. This program will let you enter a list of numbers into an array. It will then display all of the numbers, and finally display the average of all numbers. How many numbers would you like to enter?5 Please enter a number:22 Please enter a number:33 Please enter a number:44 Please enter a number:55 Please enter a number:66 Number Number 2 is 33 Number 3 is 44 Number 4 is 55 Number 5 is 66 The average is 44 is 22arrow_forwardMake an application that displays a series of at least five student ID numbers (that you have stored in an array) and asks the user to enter a numeric test score for the student. Make a ScoreException class, and throw a ScoreException for the class if the user does not enter a valid score (less than or equal to 100). Catch the ScoreException, display the message Score over 100, and then store a 0 for the student's score. At the end of the application, display all the student IDs and scores. public class ScoreException extends Exception { public ScoreException(String s) { } } import java.util.*; public class TestScore { public static void main(String args[]) throws Exception { Scanner input = new Scanner(System.in); int[] ids = {1234, 2345, 3456, 4567, 5678}; int[] scores = {0, 0, 0, 0, 0}; String scoreString = new String(); final int HIGHLIMIT = 100; String inString, outString = ""; for (int x = 0; x < ids.length; ++x)…arrow_forwardCreate a game similar to Hangman named GuessAWord in which a player guesses letters to try to replicate a hidden word. Store at least eight words in an array, and randomly select one to be the hidden word. (The statements needed to generate a random number are shown in the Exercises in the “Decision Making” and “Looping” chapters.) Initially, display the hidden word using asterisks to represent each letter. Allow the user to guess letters to replace the asterisks in the hidden word until the user completes the entire word. If the user guesses a letter that is not in the hidden word, display an appropriate message. If the user guesses a letter that appears multiple times in the hidden word, make sure that each correct letter is placed. The program should be done in C#arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT