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

TO BE WRITTEN