Java: Introduction to Problem Solving and Programming
Java: Introduction to Problem Solving and Programming
7th Edition
ISBN: 9780133834604
Author: SAVITCH
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 9, Problem 1E

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.

Expert Solution & Answer
Check Mark

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.

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.

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.

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);

    }

}

Sample Output

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?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Write a program that allows students to schedule appointments at either 1,2,3,4,5 as days of the week; 1 for Monday, 2 for Tuesday and so on. Use an array of five 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 day and a name. If the day is free, put the name in the array. If the day is not free, throw an Exception. If the day is not valid, throw another exception. Use catch block for each different kind of exception.
Write a program that displays the pattern show in the* sample executable.** Type of loop-> your favorite (you must use a pair of* nested loops)* Hints:* 1) Use variables that increments* 2) When running the sample, use values between 61 and* 76 for the number of columns, values between 16 and* 21 the number of rows* 3) Start with a rectangular array of "X" then figure out* how to create one of the upward lines and then the* backwards lines and then both lines*
Write a program called Words that: prints your name followed by three asterisks; reads the words in a text file into a String array, where each array entry contains a word; prints the number of words in the file; counts and prints the number of five-letter words; prints all of the five-letter words starting with a vowel (that is, 'a', 'e', 'i', 'o', or 'u'); finds and prints the alphabetically first five-letter word and the alphabetically last five-letter word.   it was the best of times it was the worst of timesit was the age of wisdom it was the age of foolishnessit was the epoch of belief it was the epoch of incredulityit was the season of light it was the season of darknessit was the spring of hope it was the winter of despairwe had everything before us we had nothing before uswe were all going direct to heaven we were all going directthe other wayin short the period was so far like the presentperiod that some of its noisiest authorities insisted on itsbeing received for good or…

Chapter 9 Solutions

Java: Introduction to Problem Solving and Programming

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License