Commit 34434b1

mo khan <mo@mokhan.ca>
2019-08-11 23:01:24
add error handling and fill out program profile
1 parent 30c5d77
Changed files (2)
src/Q6/README.md
@@ -5,28 +5,112 @@ Student ID: 3431709
 
 1. Problem Statement:
 
-```text
-Implement a Java method that prints out the day of the week for a given:
+  Implement a Java method that prints out the day of the week for a given:
 
-* day (1...31),
-* month (1...12) and
-* year in the range of March 1900 to February 2100.
+  * day (1...31),
+  * month (1...12) and
+  * year in the range of March 1900 to February 2100.
 
-Calculate the day of the week for the dates between
-March 1900 and February 2100 as follows:
+  Calculate the day of the week for the dates between
+  March 1900 and February 2100 as follows:
 
-1. Calculate the total number of days from 1900/1/1 to the given date (see below for details).
-1. Divide this number by 7 with an integer remainder: This now is the day of the week, with 0 as Sunday, 1 as Monday, and so on.
+    1. Calculate the total number of days from 1900/1/1 to the given date (see below for details).
+    1. Divide this number by 7 with an integer remainder: This now is the day of the week, with 0 as Sunday, 1 as Monday, and so on.
 
-To calculate the total number of days, you have to implement the following steps:
+  To calculate the total number of days, you have to implement the following steps:
 
-1. Subtract 1900 from the given year, and multiply the result by 365
-1. Add the missing leap years by adding (year − 1900) / 4.
-1. If the year itself is a leap year and the month is January or February, you have to subtract 1 from the previous result.
-1. Now add all the days of the months of the given year to the result (in the case of February, it is always 28 because the additional day for a leap year has already been added in the calculation).
-```
+    1. Subtract 1900 from the given year, and multiply the result by 365
+    1. Add the missing leap years by adding (year − 1900) / 4.
+    1. If the year itself is a leap year and the month is January or February, you have to subtract 1 from the previous result.
+    1. Now add all the days of the months of the given year to the result (in the case of February, it is always 28 because the additional day for a leap year has already been added in the calculation).
 
 1. Description of the Code:
+
+  The code implements the algorithm described above.
+  It attempts to validate the entered day, month and year.
+  It assumes that the entered input is an integer value.
+  The bounds of the input is January 1900 to February 2100.
+
 1. Errors and Warnings:
+
+  1. When the year is less than 1900, then an error is displayed.
+
+    ```bash
+    $ java -cp target/assignment2*.jar ca.mokhan.comp268.App 6
+    --- Question 6 ---
+    Enter year:
+    1899
+    Invalid year.
+    ```
+
+  1. When the year is greater than 2100, then an error is displayed.
+
+    ```bash
+    $ java -cp target/assignment2*.jar ca.mokhan.comp268.App 6
+    --- Question 6 ---
+    Enter year:
+    2101
+    Invalid year.
+    ```
+
+  1. When the year is 1900 and the month is before March, then an error is
+     displayed.
+
+    ```bash
+    $java -cp target/assignment2*.jar ca.mokhan.comp268.App 6
+    --- Question 6 ---
+    Enter year:
+    1900
+    Enter month:
+    2
+    Invalid month.
+    ```
+
+  1. When the year is 2100 and the month is after February, then an error is
+     displayed.
+
+    ```bash
+    $ java -cp target/assignment2*.jar ca.mokhan.comp268.App 6
+    --- Question 6 ---
+    Enter year:
+    2100
+    Enter month:
+    3
+    Invalid month.
+    ```
+
 1. Sample Input and Output:
+
+  1. March 1, 1900 was a Thursday.
+
+    ```bash
+    $ java -cp target/assignment2*.jar ca.mokhan.comp268.App 6
+    --- Question 6 ---
+    Enter year:
+    1900
+    Enter month:
+    3
+    Enter day:
+    1
+
+    Today is:
+    Thursday
+    ```
+
+  1. January 31, 2100 will be on a Sunday.
+
+    ```bash
+    $ java -cp target/assignment2*.jar ca.mokhan.comp268.App 6
+    --- Question 6 ---
+    Enter year:
+    2100
+    Enter month:
+    1
+    Enter day:
+    31
+
+    Today is:
+    Sunday
+    ```
+
 1. Discussion:
src/Q6/WeekDay.java
@@ -52,16 +52,30 @@ public class WeekDay {
     System.out.println("--- Question 6 ---");
     System.out.println("Enter year:");
     int year = in.nextInt();
+    if (year < 1900 || year > 2100) {
+      System.out.println("Invalid year.");
+      System.exit(-1);
+    }
 
     System.out.println("Enter month:");
     int month = in.nextInt();
+    if (month < 1 || month > 12 || (year == 1900 && month < 3) || (year == 2100 && month > 2)) {
+      System.out.println("Invalid month.");
+      System.exit(-1);
+    }
 
     System.out.println("Enter day:");
     int day = in.nextInt();
 
     System.out.println();
     System.out.println("Today is:");
-    System.out.println(new WeekDay().getWeekDay(day, month, year));
+
+    try {
+      System.out.println(new WeekDay().getWeekDay(day, month, year));
+    } catch (IllegalArgumentException error) {
+      System.out.println("Invalid date.");
+      System.exit(-1);
+    }
     System.out.println();
   }
 }