/* To Do:
- scope
- methods
- this parameter
- toString
- equals
- methods that return boolean
- overloading methods
- constructors
*/
public class Date
{
private int month;
private int day;
private int year;
// We add constructors!!!!! 3 of them.....
public Date(int xmonth, int xday, int xyear)
{
month=xmonth;
day=xday;
year=xyear;
}
// zero parameter constructor. we default it to 01/01/1900
public Date( )
{
month=1;
day=1;
year=1900;
}
// COPY Constructor
public Date(Date otherDate)
{
month=otherDate.month;
day=otherDate.day;
year=otherDte.year;
}
// Note: with our 3 constructors, we no longer need the following method createDate
public void createDate(int xmonth, int xday, int xyear)
{
month=xmonth;
day=xday;
year=xyear;
}
public void showDate()
{
System.out.println(month+"/"+day+"/"+year);
}
//The following three methods are called getters
public int getYear()
{
return year;
}
public int getMonth()
{
return month;
}
public int getDay()
{
return day;
}
//The following three methods are called setters
public void setYear(int xyear)
{
year=xyear;
}
private void setMonth(int xmonth)
{
month=xmonth;
}
public void setDay(int xday)
{
day=xday;
}
//a helper method! Retruns the corresponding String for a given month
// Teacher is lazy and only filled first 6 months. Students don't have this luxury!!
public String getMonthName(int mnth)
{
String monthString;
switch(mnth)
{
case 1: monthString="January";
break;
case 2: monthString="February";
break;
case 3: monthString="March";
break;
case 4: monthString="April";
break;
case 5: monthString="May";
break;
case 6: monthString="June";
break;
default: monthString="Invalid Entry";
break;
}
return monthString;
}
// study this. equals method returns true if the 2 dates are equal
public boolean equals(Date otherDate)
{
return(day==otherDate.day && month==otherDate.month
&& year==otherDate.year);
}
// toString method lets use a variable of type Date in a System.out.println statement
// NOTE: a properly written toString method DOES NOT contain System.out.println !!
public String toString()
{
return (getMonthName(month)+" "+day+", "+year);
}
// compareTo : lets us compare two dates
public int compareTo(Date otherDate)
{
if (year!= otherDate.year)
return(year - otherDate.year);
else if (month!=otherDate.month)
return( month - otherDate.month);
else
return ( day - otherDate.day);
}
}
-----------------------------
Driver program using Date object
import java.util.Scanner;
public class FirstProgram
{
public static void main(String [] args)
{
/* The following code is now outdated. Updated code follows
Date d1=new Date();
Date d2=new Date();
Date d3=new Date();
d1.createDate(2,23,1989);
d2.createDate(2,23,1989);
d3.createDate(3,21,2003);
*/
/* Here's the new code that accomplsihes the same thing */
Date d1=new Date(2,23,1989);
Date d2=new Date(2,23,1989);
Date d3=new Date(3,21,2003);
Date d4=new Date(d3); // what does this do ?
Date d5= new Date(); // what does this do?
System.out.println("d1 is "+d1); // uses toString !!
System.out.println("d2 is "+d2); //uses toString !!
System.out.println("d3 is "+d3); //uses toString!!
if (d1.equals(d2))
System.out.println("d1 and d2 are equal");
else
System.out.println("d1 and d2 are not equal");
if (d1.equals(d3))
System.out.println("d1 and d3 are equal");
else
System.out.println("d1 and d3 are not equal");
// some code using compareTo
if (d1.compareTo(d3)>0)
System.out.println(d1 +" is greater than "+d3);
else
System.out.println(d3 + " is greater than ior equal to "+d1);
}
}