Advertisement
Guest User

Untitled

a guest
May 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 23.29 KB | None | 0 0
  1. /*********************************************************************************************************************
  2.  *********************************************************************************************************************
  3.  *****                                       Programming Assignment #1                                           *****
  4.  *****___________________________________________________________________________________________________________*****
  5.  *****                                                                                                           *****
  6.  *****   This assignment will test your ability to define your own Exception classes, use exception handling,    *****
  7.  *****                               define an Interface and implement interfaces.                               *****
  8.  *****                                                                                                           *****
  9.  *****                      The basic idea of this assignment is to write your own Date class                    *****
  10.  *****                     (since one exists in Java, call it something different like MyDate).                  *****
  11.  *****                                                                                                           *****
  12.  *****                                        The UML is given below.                                            *****
  13.  *****-----------------------------------------------------------------------------------------------------------*****
  14.  *****                                                                                                           *****
  15.  *****                                        -month, day, year: int                                             *****
  16.  *****                                        +MyDate()                                                          *****
  17.  *****                                        +MyDate(fullDate: String) throws IllegalDateException              *****
  18.  *****                                        +getMonth(): String                                                *****
  19.  *****                                        -getMonthAsInt() : int                                             *****
  20.  *****                                        +getDay(): int                                                     *****
  21.  *****                                        +getYear(): int                                                    *****
  22.  *****                                        +setMonth(month: int) throws IllegalDateException                  *****
  23.  *****                                        +setDay(day: int) throws IllegalDateException                      *****
  24.  *****                                        +setYear(year: int)                                                *****
  25.  *****                                        +toString(): String                                                *****
  26.  *****                                                                                                           *****
  27.  *****-----------------------------------------------------------------------------------------------------------*****
  28.  *****       The no-arg constructor should set the MyDate’s date to the date of when you write the program.      *****
  29.  *****   For instance, if the date is May 8, 2018 then the no-arg constructor will set the date to 5, 8, 2018.   *****
  30.  *****                                                                                                           *****
  31.  *****                    The 1-arg constructor receives a String in the form “01/02/2003”.                      *****
  32.  *****  You will have to parse this String using substring to get the three parts of it month, day and year and  *****
  33.  *****                  then parse each item as an int to store the three in their instance data                 *****
  34.  *****                         (e.g., store “01” as 1 in month and “2003” as 2003 in year).                      *****
  35.  *****         The MyDate will be stored as three int values, so “01/02/2003” would be stored as 1, 2, 2003.     *****
  36.  *****                                                                                                           *****
  37.  *****                                                                                                           *****
  38.  *****          The MyDate class will have accessors and mutators for all three values (month, day, year).       *****
  39.  *****                           The getMonth() method will return the month as a String,                        *****
  40.  *****          so you have to convert from the int value (e.g., 5) to the name of the month (e.g., “May”).      *****
  41.  ***** All three mutators (setMonth, setDay, setYear) will receive an int value (month, day, year respectively). *****
  42.  *****                        Both setMonth and setDay can throw an IllegalDateException                         *****
  43.  *****                       if the month or day is not correct (as can the constructor).                        *****
  44.  *****                          The month is invalid if it is not between 1 and 12 and                           *****
  45.  *****          the day is invalid if it is less than 1 or greater than the maximum date for that month.         *****
  46.  *****                 NOTE: do not worry about leap years, always assume February has 28 days.                  *****
  47.  *****             Assume year is always valid so that setYear will not throw an IllegalDateException.           *****
  48.  *****                                                                                                           *****
  49.  *****                                                                                                           *****
  50.  *****            The toString method should return the String in the form “01/02/03” or “01/02/1998”.           *****
  51.  *****                   If the month and day are single digits, prepend the digit with a ‘0’.                   *****
  52.  *****              If the year is 2000, use the String “00”, if the year is between 2001 and 2009,              *****
  53.  *****           output the year as two digits as in “03” (prepend a 0) and if the year is after 2009,           *****
  54.  *****     output it as two digits, however if the year is before 2000, then output the year in four digits,     *****
  55.  *****                       so you get “03” for 2003, “18” for 2018 and “1998” for 1998.                        *****
  56.  *****                                                                                                           *****
  57.  *****                                                                                                           *****
  58.  *****                         The IllegalDateException will require two constructors,                           *****
  59.  *****                  a no-arg constructor which will simply do super(“some useful message”);                  *****
  60.  *****                                   such as “Bad date specified”;                                           *****
  61.  *****                                                                                                           *****
  62.  *****                    and a 1-arg constructor which will pass on the message it receives.                    *****
  63.  *****   The IllegalDateException can be thrown from the MyDate constructor, setMonth and setDay methods only.   *****
  64.  *****     In each case, throw a useful message such as “Bad Month: 13” or “Bad Day: there is no April 31”.      *****
  65.  *****                                                                                                           *****
  66.  *****                                                                                                           *****
  67.  *****                    MyDate will implement two interfaces, Comparable and Incrementable.                    *****
  68.  *****                 Comparable requires implementing a compareTo method to compare two MyDates.               *****
  69.  *****   To compare two MyDates, obtain the other MyDate’s month, day and year using MyDate’s accessor methods.  *****
  70.  *****                    To compare this MyDate to another MyDate, use the following logic.                     *****
  71.  *****                          This MyDate > other MyDate if this year > other year, or                         *****
  72.  *****                          if the years are the same, this month > other month, or                          *****
  73.  *****                         if the year and month are the same, this day > other day.                         *****
  74.  *****                     If all three instance data are the same, the two MyDates are equal.                   *****
  75.  *****                                  Note that getMonth returns a String.                                     *****
  76.  ***** For compareTo, you will need other’s month, but rather than having to convert it from a String to an int, *****
  77.  *****                    implement a second getMonth method which returns month as an int.                      *****
  78.  *****          You cannot overload the two getMonth methods because neither has a parameter to                  *****
  79.  *****     indicate which one should be called, so call this method something else like getMonthAsInt.           *****
  80.  *****                Make this method private since it will only be used internally (by compareTo).             *****
  81.  *****                                                                                                           *****
  82.  *****                                                                                                           *****
  83.  *****                  Incrementable does not exist so you must first define it as an Interface.                *****
  84.  *****                      The interface will contain one abstract method called increment,                     *****
  85.  *****                             which is a void method and receives no parameters.                            *****
  86.  *****               Implement the Incrementable interface in MyDate by having an increment method which         *****
  87.  *****                  increments the MyDate by 1 day. If the day goes beyond the end of this month,            *****
  88.  *****                        increment the month by 1 and if this moves the month passed 12,                    *****
  89.  *****                              make the month January and increment the year by 1.                          *****
  90.  *****                  For instance, incrementing May 31, 2018 would change the date to June 1, 2018.           *****
  91.  *****                                 Again, ignore the possibility of leap years.                              *****
  92.  *****                                                                                                           *****
  93.  *****                                                                                                           *****
  94.  *****     Once you have your Incrementable interface, IllegalDateException and MyDate classes implemented,      *****
  95.  *****                            implement a user program to create and use a MyDate.                           *****
  96.  *****               The program will open a text file and input a series of commands from the file.             *****
  97.  *****                                     The commands will be of the form:                                     *****
  98.  *****-----------------------------------------------------------------------------------------------------------*****
  99.  *****                                                                                                           *****
  100.  *****           create newdate – where newdate will be a String in the form “01/02/03”                          *****
  101.  *****           setmonth newmonth – where newmonth will an int                                                  *****
  102.  *****           setday day – where day will be an int                                                           *****
  103.  *****           setyear year – where year will be an int (assumed to be 4 digits long as in 1998 or 2008)       *****
  104.  *****           compare newDate – where newDate will be a String expressing another date,                       *****
  105.  *****                             to handle this instruction, you will have to construct another MyDate with    *****
  106.  *****                             this String and compare the current MyDate                                    *****
  107.  *****           output – this will output the existing MyDate using its toString method                         *****
  108.  *****           outputmonth – same but only the month using getMonth                                            *****
  109.  *****           outputday – same but just the day using getDay                                                  *****
  110.  *****           outputyear – same but just the year using getYear                                               *****
  111.  *****           increment – this will invoke the increment method of the existing MyDate                        *****
  112.  *****                                                                                                           *****
  113.  *****-----------------------------------------------------------------------------------------------------------*****
  114.  *****                         The input file will contain a series of such commands.                            *****
  115.  *****                           For each command, parse the command itself and if                               *****
  116.  *****                          the command is one that requires a second parameter                              *****
  117.  *****                     (create, setmonth, setday, setyear, compare), input it as well.                       *****
  118.  *****                If the command is create, set the MyDate variable to a new MyDate object,                  *****
  119.  *****                                otherwise use the existing MyDate object.                                  *****
  120.  *****                                       If the command is compare,                                          *****
  121.  *****              then pass to the existing MyDate object the message compareTo(new MyDate(newDate)).          *****
  122.  *****                                                                                                           *****
  123.  *****                                                                                                           *****
  124.  *****    Open the input file and use a while loop that iterates through the file using while(input.hasNext().   *****
  125.  *****               Some of the commands will be erroneous (either an illegal command such as delete,           *****
  126.  *****                                   or a misspelled command like creat).                                    *****
  127.  *****           Upon receiving a String that is not a legal command, throw an IllegalCommandException.          *****
  128.  *****                         This is another Exception class you will have to write.                           *****
  129.  *****         Assume every legal command has correct parameters and illegal commands have no parameters.        *****
  130.  *****                             The reason for this is to simplify your code.                                 *****
  131.  *****                                                                                                           *****
  132.  *****                                                                                                           *****
  133.  *****                The IllegalCommandException should have a no-arg and a 1-arg constructor,                  *****
  134.  *****  just like the IllegalDateException. In your user program, when you throw a new IllegalCommandException,  *****
  135.  *****          the message should include that the input command was not known and include that command,        *****
  136.  *****                as in “Illegal command input: incr”. Organize your user program as follows.                *****
  137.  *****-----------------------------------------------------------------------------------------------------------*****
  138.  *****                                                                                                           *****
  139.  *****                    // any declarations                                                                    *****
  140.  *****                    try {                                                                                  *****
  141.  *****                        // open the input file here                                                        *****
  142.  *****                        while(input.hasNext()) {                                                           *****
  143.  *****                            try {                                                                          *****
  144.  *****                                // get the next command and process it (e.g., call the appropriate         *****
  145.  *****                                // method of your MyDate instance datum                                    *****
  146.  *****                                If illegal, throw IllegalCommandException                                  *****
  147.  *****                                If legal and has a parameter, input parameter                              *****
  148.  *****                                Based on instruction, execute the appropriate MyDate method                *****
  149.  *****                            }                                                                              *****
  150.  *****                            catch blocks                                                                   *****
  151.  *****                        }                                                                                  *****
  152.  *****                    }                                                                                      *****
  153.  *****                    catch(IOException) {…}                                                                 *****
  154.  *****                                                                                                           *****
  155.  *****-----------------------------------------------------------------------------------------------------------*****
  156.  *****                 The catch blocks in the inner try should catch IllegalCommandException,                   *****
  157.  *****               IllegalDateException and NumberFormatException (three separate catch blocks).               *****
  158.  *****          The NumberFormatException block is used if an input is expected to be an int and is not.         *****
  159.  *****                                   For instance, setMonth(“February”).                                     *****
  160.  *****                                                                                                           *****
  161.  *****                                                                                                           *****
  162.  *****                After writing and compiling this second Exception class and your user class,               *****
  163.  *****                   run the program on the two input files posted with this assignment.                     *****
  164.  *****           The first input file also has the output that is expected for you to test your program.         *****
  165.  *****               Once you have fully debugged your program and are getting the expected output,              *****
  166.  *****       run it on the second input file, collect the output and submit all of your program code along       *****
  167.  *****                                with the output from the second input file.                                *****
  168.  *****                                                                                                           *****
  169.  *****                                                                                                           *****
  170.  *****     NOTE: when you examine my output, you will see that in my user class after incrementing the date,     *****
  171.  *****          I also output the new date. Also, when comparing two dates, I output what the dates are.         *****
  172.  *****                            These additions to the output are strictly optional.                           *****
  173.  *****                                                                                                           *****
  174.  *****                                                                                                           *****
  175.  *****          Submission of this assignment should be by email to foxr@nku.edu by the listed due date.         *****
  176.  *****             For convenience, please either place all files into a single zip file or copy and             *****
  177.  *****      paste all code and output into a Word document. Make sure all of your code is well commented.        *****
  178.  *********************************************************************************************************************
  179.  *********************************************************************************************************************/
  180.  
  181. // IMPORTS of needed tools and plug-ins
  182. import java.io.*;
  183. import java.util.ArrayList;
  184. import java.util.Scanner;
  185.  
  186. public class Main {
  187.  
  188.     public static void main(String[] args) {
  189.  
  190.         // String variables that hold input and output file pathways.
  191.         String input1FileName = "prog1-1.txt";
  192.         String input2FileName = "prog1-2.txt";
  193.         String output1FileName = "prog1-output1.txt";
  194.         String testOutputFileName = "test-Output.txt";
  195.         ArrayList<MyDate> myDateObjects = new ArrayList<>();
  196.  
  197.         Scanner input1 = null;
  198.         Scanner input2 = null;
  199.         PrintWriter output = null;
  200.         PrintWriter testOutput = null;
  201.  
  202.         try{
  203.             input1 = new Scanner(new File(input1FileName));
  204.             input2 = new Scanner(new File(input2FileName));
  205.             output = new PrintWriter(output1FileName);
  206.             testOutput = new PrintWriter(testOutputFileName);
  207.  
  208.  
  209.             MyDate tmpDate = null;
  210.  
  211.             ArrayList<String> words = new ArrayList<>();
  212.  
  213.             int counter = 0;
  214.             int myDateNumber = 0;
  215.  
  216.             int myDateMonth = 0;
  217.             int myDateDay = 0;
  218.             int myDateYear = 0;
  219.  
  220.             String stringMonth = null;
  221.             String stringDay = null;
  222.             String stringYear = null;
  223.             String stringFullDate = null;
  224.  
  225.             boolean createNewDate = false;
  226.  
  227.             while(input1.hasNext()){
  228.                 String command = input1.next().toLowerCase().trim();
  229.                 try {
  230.                     if(command.equals("create")){
  231.                         tmpDate = new MyDate(input1.next());
  232.                         myDateObjects.add(tmpDate);
  233.                         System.out.println("size"+myDateObjects.size());
  234.  
  235.                     }else if(command.equals("outputmonth")){
  236.                         System.out.println("Month: " + tmpDate.getMonth());
  237.                         // input1.nextLine();
  238.                     }
  239.                 } catch (IllegalDateException e){
  240.                     System.out.println("There was an error: " + e);
  241.                 }
  242.             }
  243.  
  244.         }catch (Exception e){
  245.             System.out.println("There was an error: "+ e);
  246.         }
  247.     }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement