Site Search

CS 111: Program 5 main content

Based on Programming Challenges #4: Month Class Exceptions on page 765 of your textbook.

Write a class named Month. The class should have an int field named monthNumber that holds the number of the month. The UML class diagram for Month is given below.

Write two exception classes for the following error conditions.

Design the Month class so it throws the appropriate exception when either of these errors occurs. Use the Driver code below to test your Month class.

Month Class

   public class Month
   {
      private int monthNumber;

      private static String[] monthNames = 
         {"January","February","March","April","May", "June",
          "July","August","September","October","November","December"};

   // No-arg constructor: Sets monthNumber to 1
      public Month() 

   // Constructor: accepts month number as integer
      public Month(int number) throws InvalidMonthNumberException

   // Constructor: accepts month name as String
      public Month(String name) throws InvalidMonthNameException

   // Setter method: accepts month number as integer
      public void setMonth(int number) throws InvalidMonthNumberException

   // Setter method: accepts month name as String
      public void setMonth(String name) throws InvalidMonthNameException

   // Getter method: returns monthNumber as int
      public int getMonthNumber()

   // Getter method: returns appropriate month name as String
      public String getMonthName()
   }

Driver Class

 import java.util.Scanner;

   public class Driver
   {
      public static void main(String[] args)
      {
      // Declare and instantiate a Scanner object for user input
         Scanner keyboard = new Scanner(System.in);
         
      // Declare a Month object to test program
         Month nextMonth;
      
      // Prompt for month and read from user
         System.out.print("Enter a month (either 1..12, a month name, or quit to stop): ");
         String userInput = keyboard.nextLine();
         
      // Keep looping until the user wants to quit
         while (!userInput.equalsIgnoreCase("quit"))
         {
            try
            {
               try
               {
               // try to accept month as an integer value
                  int monthNumber = Integer.parseInt(userInput);
                  nextMonth = new Month(monthNumber);
                  System.out.println("Created month " + 
                                    nextMonth.getMonthNumber()	+ " as " +
                                    nextMonth.getMonthName());
               }
                  catch (NumberFormatException e)
                  {
                  // Month not valid integer, process as month name
                     nextMonth = new Month(userInput);
                     System.out.println("Created month " + 
                                       nextMonth.getMonthName()	+ " as " +
                                       nextMonth.getMonthNumber());	
                  }
            }
               catch (InvalidMonthNumberException e)
               {
                  System.out.println(e.getMessage());
               }
               catch (InvalidMonthNameException e)
               {
                  System.out.println(e.getMessage());
               }
               
         // Prompt for and read in next month
            System.out.print("\nEnter a month (either 1..12, a month name, or quit to stop): ");
            userInput = keyboard.nextLine();   
         }
      }
   }

Sample Session

Enter a month (either 1..12, a month name, or quit to stop): 4
Created month 4 as April

Enter a month (either 1..12, a month name, or quit to stop): May
Created month May as 5

Enter a month (either 1..12, a month name, or quit to stop): 13
Error: 13 is an invalid month number

Enter a month (either 1..12, a month name, or quit to stop): Monday
Error: Monday is an invalid month name

Enter a month (either 1..12, a month name, or quit to stop): quit

Programming Style Requirements

What to turn in

  1. Source program printouts stapled together with file header comments on all printouts
    • Note: you do not need to include the Driver.java printout
  2. Be sure your project is saved in your u:\Program5\ folder
  3. The assignment is due at the start of lab 6. You program must compile and produce output for you to receive any credit.
    • You are allowed one late programming assignment for the quarter.

Grading Criteria

50 points maximum