Title: Date Class
1Date Class
- public class Date
-
- private int month
- private int day
- private int year
- private boolean valid
- // true if stored date is valid
- public Date (int inDay, int inMonth, int
inYear) -
- day inDay
- month inMonth
- year inYear
-
- ...
-
2- public class Date
-
- private int month
- private int day
- private int year
- private boolean valid
- // true if stored date is valid ...
- private void setValidity()
-
- // Check day, month, year
- // Handle 30 day months and leap year
- // Leap year expression
- // year 400 0 (year 4 0
year 100 ! 0) - if ( . . . )
- valid true
- else
- valid false
-
3- public class Date
-
- private int month
- private int day
- private int year
- private boolean valid // true if stored date
is valid - public Date (int inDay, int inMonth, int
inYear) -
- day inDay
- month inMonth
- year inYear
- setValidity()
-
- public Date (String s)
-
- // Code to parse string to date
4- public class Date
-
- ...
- public Date (int inDay, int inMonth, int
inYear) - ...
- public Date (String s)
- ...
- public Date ()
-
- day 1
- month 1
- year 2000
- valid true
-
- public Date (Date d) // Copy constructor
-
- day d.day
- month d.month
- year d.year
5- public class Date
-
- private int month
- private int day
- private int year
- private boolean valid
-
- public Date (int inDay, int inMonth, int
inYear) - ...
- public Date (String s)
- ...
- public Date ()
- ...
- public Date (Date d)
- ...
- private void setValidity()
- ...
- . . .
6- public class Date
-
- . . .
- public String toString()
-
- return month / day / year
-
- public boolean isValid()
-
- return valid
-
- // More methods
-
7- public class Date
-
- . . .
- // Object is a base class
- // All classes are a sub-class of Object
- // An instance of any class inherits the
methods of - // Object class
- _at_Override
- public String toString()
-
- return month / day / year
-
- //public _at_Override String toString()
8Date Class
public class Date ... public _at_Override
String toString() return month /
day / year public void setFormat
(int format) // Allow different
formats ...
9Date Class
public class Date ... // constants that
can be accessed by the user public final int
SHORT_DATE 0 public final int LONG_DATE
1 public final int WEEKDAY 2
private int dateFormat SHORT_DATE ...
10Date Class
public class Date ... public void
setFormat (int format) // Check
validity of format dateFormat format
public _at_Override String toString()
if (dateFormat shortDate) return
month / day / year else if
(dateFormat longDate) ... else
... ...
11Date Class
- Date d new Date()
- System.out.println(d.toString())
- d.setFormat(d.longDate)
- System.out.println(d.toString())
- // Cannot compile!
- d.setFormat(Date.weekDay )
12Date Class
public class Date ... // constants that
can be accessed by the user // adding modifier
static public static final int shortDate
0 public static final int longDate 1
public static final int weekDay 2
private int dateFormat shortDate
... Date d new Date() // Compile and
run! d.setFormat(Date.weekDate)
13Instance and Static Data
- Instance data
- private int day, month, year
- // Difference instances (objects) usually
- // have different values
- Static data
- private static int count 0
- // One value for the class,
- // no matter how many instances (could be 0)
- // Each constructor must update the value of
count - public Date()
-
- ...
- count
-
14Static Methods
public class Date private static int count
0 . . . public Date() ...
count // static method //
for the class public static int getCount()
// Cannot access instance fields
return count
15Static Methods
public static int getCount() return
count int theCount Date.getCount() //
theCount 0 Date d theCount
Date.getCount() // theCount 0 d new
Date() theCount Date.getCount() // theCount
1 //Static method can be referenced from
non-static context theCount d.getCount()
16Instance Methods
public class Date . . . private static
int count 0 public Date() ...
count // instance method
public int getCount() // can access
static fields return count
17Instance Methods
public int getCount() return count int
theCount Date.getCount() // non-static method
cannot be referenced // from a static
context Date d theCount Date.getCount() //
NO d new Date() theCount Date.getCount() //
No theCount d.getCount() // YES!
18- public class Date
-
- ...
- private final int LEAP_DIV 4
- private final int LEAP_OUT_DIV 100
- private final int LEAP_IN_DIV 400
- public boolean isLeapYear ()
-
- return ( year LEAP_IN_DIV 0
- (year LEAP_DIV 0
- year LEAP_OUT_DIV ! 0))
-
-
19- public class Date
-
- // constants for months
- public int lastDayOfMonth()
-
- switch (month)
-
- case APR case JUN case SEP case NOV
- return SHORT_MONTH_DAYS
- case FEB
- if (this.isLeapYear())
- return FEB_LEAP_DAYS
- else
- return FEB_DAYS
- default
- return LONG_MONTH_DAYS
-
-
20- public class Date
-
- private boolean valid
- ...
- private void setValidity()
-
- valid month gt JAN month lt DEC
- day gt 1 day lt
lastDayOfMonth() -
- ...
-
21- public class Date
-
- . . .
- public Date ()
- public Date tomorrow ()
-
- Date d new Date(month, day, year)
- if (day lt lastDayOfMonth())
- d.day
- else
-
- d.day 1
- d.month
- if (d.month gt DEC)
-
- d.year
- d.month 1
-
22- public class Date
-
- private int day, month, year
- ...
- public boolean lessThan ( Date d )
-
- if ( year lt d.year )
- return true
- if ( year gt d.year )
- return false
- if ( month lt d.month )
- return true
- if ( month gt d.month )
- return false
- return day lt d.day
-
- ...
23- public class Date
-
- private int day, month, year
- ...
- public boolean lessThan ( Date d )
-
- return ( (year lt d.year)
- (year d.year month lt
d.month) - (year d.year month
d.month day lt d.day) -
- ...
-
23
24- public class Date
-
- public static final int SHORT_DATE 0
- public static final int LONG_DATE 1
- public static final int WEEKDAY 2
- private static final int LEAP_DIV 4
- private static final int LEAP_OUT_DIV 100
- private static final int LEAP_IN_DIV 400
- private static int count 0
- private int dateFormat SHORT_DATE
- private int month
- private int day
- private int year
- private boolean valid
-
25- public class Date
-
- . . .
- public static int getCount()
- ...
- public Date (int inDay, int inMonth, int
inYear) - ...
- public Date (String s)
- ...
- public Date ()
- ...
- public Date (Date d)
- ...
- private void setValidity()
- ...
- public boolean isLeapYear ()
- ...
- public int lastDayOfMonth()
- ...
26Some Example Usage
- Date d1 new Date( 12, 25, 2006)
- Date d2 new Date( September 12, 2005)
- Date d3 new Date()
- Date d4
- . . .
- if (d1.isValid())
- d4 d1.tomorrow()
- else if (d2.isValid())
- d4 d2
- else if (d3.isValid())
- d4 new Date(d3)
- else
- d4 new Date()
27Some Example Usage
- if (d1.isValid())
- System.out.println(d1.toString())
- else
- System.out.println(Invalid date!)
- if (d1.isValid())
- System.out.println(d1)
- // method toString is used to convert d1 to
string - else
- System.out.println(Invalid date!)
- if ( d1.lessThan( d2 ))
- ...
28Tuesday
- Last day to drop
- Without a W
- No Drop Fee
28
29Quiz 1
- Wednesday
- Lab 206
- 10 points
- Basic concepts of OOP
- Simple Java code
- Java arrays
30