James Liang - ICS Blog

ICS4U Blogging

OOP - Multiplayer Coding

Multiplayer Coding

Part A: Draw a UML

  • Class called Date
  • Includes three pieces of information as instance variables — a month (type int, a day (type int), and a year (type int).
  • Use of a constant for default values
  • Static variable for the number of instances of Date objects
  • Your class should have 2 constructors,
    • one with no initial values provided;
    • and one that initializes all three instance variables.
  • Provide mutator and accessor methods for each instance variable. In your constructor and mutators, do some error checking to ensure that the argument(s) passed make sense. For example, some months have different number of days, leap year affects February — if not, set these instance variables to some reasonable values and display an error message.
  • Provide a toString() method that returns the date as a string in the format “dd/mm/yyyy”.
  • Create a static method to get the current number of instances of Date objects

Part B: Code your UML

Using your UML diagram, split up the work and program the Date class in Repl.it with your partner. You will need to create another file (Date class) in your Repl.it project for this. Add Javadoc comments. Write a test application named DateTest that demonstrates class Date‘s capabilities.

DateTest.java

/*
Using your UML diagram, split up the work and program the Date class in Repl.it with your partner. You will need to create another file (Date class) in your Repl.it project for this.  Add Javadoc comments. Write a test application named DateTest that demonstrates class Date‘s capabilities. 
*/

/**
* The DateTest program tries out varies capabilities of
* the Date class. This includes dates that fall outside
* the boundary as long as they are positive numbers. The
* year isn't checked as there are many possible years.
*
* @author James Liang, Anishan Siva
* @version 1.0
* @since 2021-11-23
*/
class DateTest {
  /**
  * This method is used to call all other methods and
  * to call onto the Date class.
  *
  * @param args required as part of main method
  * @return void returns nothing
  */
  public static void main(String[] args) {
    Date fCrisis = new Date(9, 28, 2008);
    System.out.println("Month: " + fCrisis.getMonth());
    System.out.println("Day: " + fCrisis.getDay());
    System.out.println("Year: " + fCrisis.getYear());
    System.out.println(fCrisis);
    System.out.println("# of Dates: " + Date.getNumberOfDates());
    System.out.println();

    Date endOfWorld = new Date(23, 64, 90291);
    System.out.println("Month: " + endOfWorld.getMonth());
    System.out.println("Day: " + endOfWorld.getDay());
    System.out.println("Year: " + endOfWorld.getYear());
    System.out.println(endOfWorld);
    System.out.println("# of Dates: " + Date.getNumberOfDates());
    System.out.println();

    Date neverForget = new Date();
    System.out.println("Month: " + neverForget.getMonth());
    System.out.println("Day: " + neverForget.getDay());
    System.out.println("Year: " + neverForget.getYear());
    System.out.println(neverForget);
    System.out.println("# of Dates: " + Date.getNumberOfDates());
    System.out.println();

    neverForget.setMonth(-13);
    neverForget.setDay(-43);
    neverForget.setYear(-2010);
    System.out.println("Month: " + neverForget.getMonth());
    System.out.println("Day: " + neverForget.getDay());
    System.out.println("Year: " + neverForget.getYear());
    System.out.println(neverForget);
    System.out.println("# of Dates: " + Date.getNumberOfDates());
    System.out.println();

    neverForget.setMonth(9);
    neverForget.setDay(11);
    neverForget.setYear(2001);
    System.out.println("Month: " + neverForget.getMonth());
    System.out.println("Day: " + neverForget.getDay());
    System.out.println("Year: " + neverForget.getYear());
    System.out.println(neverForget);
    System.out.println("# of Dates: " + Date.getNumberOfDates());
    System.out.println();
  }
}

Date.java

/**
* The Date class contains all the accessors, mutators,
* and constructors.
*
* @author James Liang, Anisan Siva
* @version 1.0
* @since 2021-11-23
*/
public class Date {
  private int month;
  private int day;
  private int year;
  private static final int DEFAULT_MONTH = 1;
  private static final int DEFAULT_DAY = 1;
  private static final int DEFAULT_YEAR = 2000;
  private static int numberOfDates = 0;
  /**
  * The Date constructor can be called onto to create
  * a new object.
  *
  * @param N/A there are no parameters
  * @return N/A constructors typically don't return
  */
  public Date() {
    month = DEFAULT_MONTH;
    day = DEFAULT_DAY;
    year = DEFAULT_YEAR;
    numberOfDates++;
  }
  /**
  * The Date constructor can be called onto to create
  * a new object. This is an overloaded constructor.
  *
  * @param monthChange this is the new month
  * @param dayChange this is the new day
  * @param yearChange this is the new year
  * @return N/A constructors typically don't return
  */
  public Date(int monthChange, int dayChange, int yearChange) {
    setMonth(monthChange);
    setDay(dayChange);
    setYear(yearChange);
    numberOfDates++;
  }
  /**
  * The getMonth accessor provides the current month value.
  *
  * @param N/A there are no parameters
  * @return int this is the month value
  */
  public int getMonth() {
    return month;
  }
  /**
  * The getDay accessor provides the current day value.
  *
  * @param N/A there are no parameters
  * @return int this is the day value
  */
  public int getDay() {
    return day;
  }
  /**
  * The getYear accessor provides the current yrear value.
  *
  * @param N/A there are no parameters
  * @return int this is the year value
  */
  public int getYear() {
    return year;
  }
  /**
  * The setMonth mutator changes the month.
  *
  * @param monthChange this is the new month
  * @return void this mutator returns nothing
  */
  public void setMonth(int monthChange) {
    if (monthChange > 12 || monthChange < 1) {
      month = DEFAULT_MONTH;
      System.out.println("Impossible month value\nMonth is now set to 1");
    }
    else {
      month = monthChange;
    }
  }
  /**
  * The setDay mutator changes the day.
  *
  * @param dayChange this is the new day
  * @return void this mutator returns nothing
  */
  public void setDay(int dayChange) {
    if (dayChange < 1) {
      day = DEFAULT_DAY;
      System.out.println("Impossible day value\nDay is now set to 1");
    }
    else if (month == 2 && dayChange > 28) {
      if (year%4 == 0 && year%100 != 0 && dayChange > 29) {
        day = DEFAULT_DAY;
        System.out.println("Impossible day value\nDay is now set to 1");
      }
      else if (year%400 == 0 && dayChange > 29) {
        day = DEFAULT_DAY;
        System.out.println("Impossible day value\nDay is now set to 1");
      }
      else {
        day = DEFAULT_DAY;
        System.out.println("Impossible day value\nDay is now set to 1");
      }
    }
    else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8|| month == 10|| month == 12) {
      if (dayChange > 31) {
        day = DEFAULT_DAY;
        System.out.println("Impossible day value\nDay is now set to 1");
      }
      else {
        day = dayChange;
      }
    }
    else if (month == 4 || month == 6 || month == 9 || month == 11) {
      if (dayChange > 30) {
        day = DEFAULT_DAY;
        System.out.println("Impossible day value\nDay is now set to 1");
      }
      else {
        day = dayChange;
      }
    }
    else {
      month = DEFAULT_MONTH;
      System.out.println("Impossible month value\nMonth is now set to 1");
      if (dayChange > 31) {
        day = DEFAULT_DAY;
        System.out.println("Impossible day value\nDay is now set to 1");
      }
      else {
        day = dayChange;
      }
    }
  }
  /**
  * The setYear mutator changes the year.
  *
  * @param yearChange this is the new year
  * @return void this mutator returns nothing
  */
  public void setYear(int yearChange) {
    if (yearChange < 1990 || yearChange > 3000) {
      year = DEFAULT_YEAR;
      System.out.println("Impossible year value\nYear is now set to 2000");
    }
    else {
      year = yearChange;
    }
  }
  /**
  * The toString method returns the current year, month,
  * and day values as a String
  *
  * @param N/A there are no parameters
  * @String this is the String representation of the object
  */
  public String toString() {
    return day + "/" + month + "/" + year;
  }
  /**
  * The getNumberOfDates method returns the number of 
  * dates that have been created.
  *
  * @param there are no parameters
  * @return int this is the number of dates
  */
  public static int getNumberOfDates() {
    return numberOfDates;
  }
}

Post the UML and the code on your Learning Blog with a DETAILED EXPLANATION of your code, try to use all the programming TERMINOLOGY from this topic

The UML was created with a lot of referencing to the slides. Since I wasn’t used to writing UMLs, I had to read through the instructions a few times. I had trouble deciding on the default year, I initially thought about using 1990, but resorted to 2000 as it felt not too old but not too recent.

The DateTest.java file is used to test the Date class file. I used JavaDoc comments to explain both my class and method. I instantiate a new object and called it fCrisis, the value set was September 28, 2008, the day the stock markets fell. I then used multiple println statements to print the values of the object. Accessors were used to retrieve the values of the day, month, and year. A toString method automatically called when I printed fCrisis. To get the number of dates, I had to use a static method since I was dealing with a static variable. I tested the error correction next by creating a new object with values that were much higher than reasonable. Similar to before, I printed out all of the values. I had used a constructor that used parameters to create the first two objects, with the third, I used the constructor without any parameters. I printed all of the values as with before to show the default values. After this, I used mutators to set the values of the object much lower than reasonable. I printed out the values as usual. Finally, I used the mutators to set an actual date and printed out all the values.

The Date.java file has all of the accessors, mutators, constructors, and methods that were called in the test file. Similar to the test file, I used JavaDoc comments to explain the class, constructors, methods, mutators, and accessors. In my date class, I created multiple private variables that varied depending on the UML. There were some constants and static variables as well that had set values.

Next was the Date constructor. Since it had no parameters, I set all of the dates to their default values. I also increased the numberOfDates by one to keep track of how many date objects have been created. After that is the overloaded constructor, it takes three parameters and then calls mutators to set the values. I probably could’ve used “this” so that the the parameters wouldn’t have had “change” at the end, but I wasn’t comfortable with “this” yet. If I were to code this again, I would probably use “this”. As with before I increased the numberOfDates.

Following the constructors are the accessors that get the values of the month, day, and year. These just return their relevant values (getDay returns day). Next are the mutators, the first one is setting the month. It takes one parameter, monthChange, which is the new month value. Since a user could input an unreasonable month value, I need to check whether it is possible. I check if the month value is in between 1-12, if it isn’t I set the month value to the default value and output a message explaining what happened. The setDay mutator is much more complicated compared to the setMonth mutator. It takes in the dayChange parameter which is the new day value. Any time the day value is unreasonable, I set it to the default value and output an explanation message. I first check if the value is less than one, I then check if the month is equal to 2 (February) and if the new day is equal to 29. I then check if the year is a leap year, if it is, the day is valid and set, else it is set to default. I then check if the month is equal to 2 and if the day is less than 29, any values in that scope should be valid and is set. I then check if the month is any of the months that have 31 days, if it is, I then check if the new day value is over 31. Since a day over 31 is unreasonable I set to default, otherwise I set the new day. I do the same to check for the 30 day months. I check if the new day value is over 30 and if it isn’t, the new value is set. If for some reason the month doesn’t fall into any of the previous checks, the month is set to default (January). With this new month I have to check if the day value is over 31 since January can’t have more than 31 days. If the new day value is valid, it is set. The setYear mutator is much simpler as it checks if the year is between 1990 and 3000. I thought that any year out of this scope would be unreasonable, so if a year is outside of this, it would be set to default (2000).

The toString method has no parameters and returns a String. The instructions state to return the date in the format “dd/mm/yyyy”, so I return the values in that order. Finally, the getNumberOfDates static method takes in no parameters and returns an integer. The numberOfDates variable has been keeping track of all new dates, so I just return that variable.