Question

Write a program that allows students to schedule appointments at either 1, 2, 3, 4, 5,...

Write a program that allows students to schedule appointments at either 1, 2, 3, 4, 5, or 6 o�clock pm.

Use an array to store each of the six (strings) of names for the 6 time slots.

Write a loop that iterates as long as the array has a freespace (program should end when all time slots are taken).

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.

The exceptions should NOT exit the program but perform actions to help user:

TimeInUseException - could be used to ask user to input a different time.

InvalidTimeException - could be used to enter a valid time.

Please test your program with valid data, along with data which will cause the two exceptions to be thrown.

Write a program that allows students to schedule appointments at either 1, 2, 3, 4, 5, or 6 o�clock pm.

Use an array to store each of the six (strings) of names for the 6 time slots.

Write a loop that iterates as long as the array has a freespace (program should end when all time slots are taken).

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.

The exceptions should NOT exit the program but perform actions to help user:

TimeInUseException - could be used to ask user to input a different time.

InvalidTimeException - could be used to enter a valid time.

Please test your program with valid data, along with data which will cause the two exceptions to be thrown.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

Program

import java.util.Scanner;

public class ExceptionThrownContinue {

   public static void main(String[] args) {
       //Input read
       Scanner sc=new Scanner(System.in);
       //Array to store name os students in each slot
       String slot[]=new String[6];
       //Loop until all slots fill
       int i=0;
       while(i<6) {
           //Prompt for name of the student
           System.out.print("Enter your name: ");
           String name=sc.nextLine();
           //Prompt for the time slot
           System.out.print("Enter the slot you want: ");
           int slotNum=sc.nextInt();
           sc.nextLine();
           try {
               //Exception check for invalid slot number
                if(slotNum>6 || slotNum<1) {
                   throw new TimeInvalidException("InValid time slot!!!");
                }
              //Exception check for already booked
               else if(slot[slotNum-1]!=(null)) {
               throw new TimeInUseException("This time already taken!!!");
              }
                //Otherwise alocate slot
              else {
               slot[slotNum-1]=name;
               i++;
              }
                //Exceptions
           } catch (TimeInvalidException e) {
                   continue;
           }
           catch (TimeInUseException e) {
               continue;
           }
       }
       //All slots filled
       System.out.println("All slots filled!!!");
   }

   }
//Invalid exception generator
   class TimeInvalidException extends Exception{
       public TimeInvalidException(String e) {
           System.out.println(e);
       }
   }
   //Already taken exception generator
   class TimeInUseException extends Exception{
       public TimeInUseException(String e) {
           System.out.println(e);
       }
   }

-----------------------------------------------------------------

Output

Enter your name: Macheal John
Enter the slot you want: 1
Enter your name: Milly Mark
Enter the slot you want: 1
This time already taken!!!
Enter your name: Milly Mark
Enter the slot you want: 2
Enter your name: Adorn Adrian
Enter the slot you want: 0
InValid time slot!!!
Enter your name: Adorn Adrian
Enter the slot you want: 3
Enter your name: Alwin Albert
Enter the slot you want: 7
InValid time slot!!!
Enter your name: Alwin Albert
Enter the slot you want: 4
Enter your name: Becca Benny
Enter the slot you want: 5
Enter your name: Boby Bay
Enter the slot you want: 6
All slots filled!!!

Add a comment
Know the answer?
Add Answer to:
Write a program that allows students to schedule appointments at either 1, 2, 3, 4, 5,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For practice using exceptions, this exercise will use a try/catch block to validate integer input from...

    For practice using exceptions, this exercise will use a try/catch block to validate integer input from a user. Write a brief program to test the user input. Use a try/catch block, request user entry of an integer, use Scanner to read the input, throw an InputMismatchException if the input is not valid, and display "This is an invalid entry, please enter an integer" when an exception is thrown. InputMismatchException is one of the existing Java exceptions thrown by the Scanner...

  • What to submit: your answers to exercises 1, and 2 and separate the codes to each...

    What to submit: your answers to exercises 1, and 2 and separate the codes to each question. Create a MyTime class which is designed to contain objects representing times in 12-hour clock format. Eg: 7:53am, 10:20pm, 12:00am (= midnight), 12:00pm (= noon). You can assume the user will enter times in the format given by the previous examples. Provide a default constructor, a set method which is given a String (eg: “7:53am”), a getHours method, a getMinutes method and a...

  • You are to write a program (BookExceptionsDemo.java) that will create and, using user input, populate an...

    You are to write a program (BookExceptionsDemo.java) that will create and, using user input, populate an array of instances of the class Book. The class Book is loaded in our Canvas files: Book.java The user will enter a number n (n must be > 0, trap the user until they input a valid value for n), Your program will declare and create an array of size n of instances of the class Book. The user will be asked to enter...

  • Please help I am confused to where start. It C++ program thank you The stoi function...

    Please help I am confused to where start. It C++ program thank you The stoi function converts a string to an integer, but does not check whether or not the value of the string is a good integer. If the stoi function fails, an exception is thrown and the program terminates. Write a program that asks the user to enter two strings, one called sNumerator and the other sDenominator. Using the stoi function, convert the two strings to numbers but...

  • 1. Create a MyTime class which is designed to contain objects representing times in 12-hour clock...

    1. Create a MyTime class which is designed to contain objects representing times in 12-hour clock format. Eg: 7:53am, 10:20pm, 12:00am (= midnight), 12:00pm (= noon). You can assume the user will enter times in the format given by the previous examples. Provide a default constructor, a set method which is given a String (eg: “7:53am”), a getHours method, a getMinutes method and a boolean isAm method. Also provide a method for returning a string “morning”, “afternoon”, “evening” or “night”...

  • Write a program that prompts the user to enter a person's birth date in numeric form...

    Write a program that prompts the user to enter a person's birth date in numeric form (e.g. 8-27-1980) and outputs the date of birth in the format of month day, year (e.g. August 27, 1980). Your program must work for three types of exceptions - invalid day, invalid month and invalid year (year must be between 1915 and 2015). Make these as class driven exceptions. Use one try-catch block with separate catches for the exception classes to handle errors for...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

  • JAVA 1. Create a MyTime class which is designed to contain objects representing times in 12-hour...

    JAVA 1. Create a MyTime class which is designed to contain objects representing times in 12-hour clock like 7:53am, 10:20pm, 12:00am (= midnight), 12:00pm (= noon). Provide a default constructor, a set method which is given a String (eg, “7:53am”), a getHours method, a getMinutes method and a boolean isAm method. Also provide a method for returning a string “morning”, “afternoon”, “evening” or “night” appropriate (in your opinion) to the time. Please see the NOTE below concerning input validation and...

  • .)   Write a program to implement the exception handling with multiple (at least 3) catch statements....

    .)   Write a program to implement the exception handling with multiple (at least 3) catch statements. Any type of exception may be thrown. Catch statements must display (cout) the type of exception thrown. .)   Write a function which accepts an index and returns the corresponding element from an array. If the index is out of bounds, the function should throw an exception. Handle this exception in "main()". (This is pretty open ended, so anything from a calculator or "what have...

  • Java Store Time Clock Objects

    1. Create a Time class which is designed to contain objects representing times in 12-hour clock format. Eg: 6:58am, 11:13pm, 12:00am (= midnight), 12:00pm (= noon). You can assume the user will enter times in the format given by the previous examples.Provide a default constructor, a set method which is given a String (eg: “9:42am”), a getHours method, a getMinutes method and a boolean isAm method. Also provide a method for returning a string “morning”, “afternoon”, “evening” or “night” appropriate...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT