Java

OOP GUIDE / WORK

 

DATE CLASS

 

Topics

  • Advanced class design
  • Constructors
  • Instance methods
  • Encapsulation

 

 

TASK – PART 1 – DATE CLASS: INSTANCE VARIABLES

 

INCOMPLETE


Include the following methods inside the class:

 

-        isLeapYear() – true if the year is a leap year

Note: A leap year is a year that is divisible by 4, but not divisible by 100, unless it is divisible also by 400.

Examples: The years 1204, 1720 and 2000 are leap years.  The years 2022 and 1900 are not leap years.


-        setNextDay() – moves the date to the next day (ie: Jan 2, 1972 becomes Jan 3, 1972)

-        getNextYear() – returns the number of the next year

-        getNextDay() – returns the number of the next day

-        nextMonth() – returns the number of the next month

-        toString() – returns a date in the form of January 3rd, 1972

-        maybe: calculate weekday?  Ie: Monday

-        isBefore(Date d) – returns true of the date is before Date d

-        isAfter(Date d) - …

 

 

Consider using something like this for Date’s constructor:

 

public class Person
{
    int age;
    public Person(int age) throws IllegalArgumentException
    {
       if(age <= 0)
       {
 
          throw new IllegalArgumentException("Age is not allowed");
       }
       // Do some stuffs
       this.age = age;
    }
}