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)
- This is the question I am stuck on - In the exercises in Chapter 6, you created a class named Purchase. Each Purchase contains an invoice number, amount of sale, amount of sales tax, and several methods. Add get methods for the invoice number and sale amount fields so their values can be used in comparisons. Next, write a program that declares an array of five Purchase objects and prompt a user for their values. Then, in a loop that continues until a user inputs a sentinel value of Z, ask the user whether the Purchase objects should be sorted and displayed in invoice number order or sale amount order. This is the code I have - public class Purchase { private int invoiceNumber; private double saleAmount; private double tax; private static final double RATE = 0.05; public void setInvoiceNumber(int num) { invoiceNumber = num; } public void setSaleAmount(double amt) { saleAmount = amt; tax = saleAmount * RATE; } public double…arrow_forwardWrite 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_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_forward1. Write a JavaScript program that calls these three methods. a. method 1 should print: 54321 4321 321 21 1 b. method 2 should print: 12345 1234 123 12 1 c. method 3 should create and array and store 10 multiples of 5 and print it out. Use for loop to generate the multiples and then print them out using a second for loop.arrow_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_forward
- Write 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_forwardProgramming 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_forwardThis is a java assignment. I have attached it as an image file.arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT