星期一, 4月 24, 2006

Lab 0424

package untitled8;
/**
* s9224205
*/

import javax.swing.*;
public class Lab0424
{
private String month ;
private int day ;
private int year ;


/** public void setDate (int newMonth , int newDay , int newYear)
{
day = newDay;
year = newYear ;
month = monthString(newMonth);

}
*/
public void writeOutput()
{
System.out.println(month +"/" + day + ", "+ year);
}

public boolean equals(Lab0424 otherDate)
{
return ((month.equals(otherDate.month) )&&(day == otherDate.day)&&(year == otherDate.year));
}

public boolean precedes(Lab0424 otherDate)
{
return ((year (year == otherDate.year && getMonth() < otherDate.getMonth())||
(year == otherDate.year && month.equals(otherDate.month )&&day < otherDate.day) );
}

public void setDate(int day ,String month,int year)
{
this.day = day ;
this.year = year ;
this.month = month;
}

public int getMonth()
{
if (month.equalsIgnoreCase("Jan"))
return 1;
else if (month.equalsIgnoreCase("Feb"))
return 2;
else if (month.equalsIgnoreCase("Mar"))
return 3;
else if (month.equalsIgnoreCase("Apr"))
return 4;
else if (month.equalsIgnoreCase("May"))
return 5;
else if (month.equalsIgnoreCase("Jun"))
return 6;
else if (month.equalsIgnoreCase("July"))
return 7;
else if (month.equalsIgnoreCase("Aug"))
return 8;
else if (month.equalsIgnoreCase("Sep"))
return 9;
else if (month.equalsIgnoreCase("Oct"))
return 10;
else if (month.equalsIgnoreCase("Nov"))
return 11;
else if (month.equalsIgnoreCase("Dec"))
return 12;
else {
System.out.println("Fatal Error");
System.gc() ;
System.exit(0);
return 0;
}

}
public int getDay()
{
return day;
}

public int getYear()
{
return year;
}

public String toString()
{
return (month+ " "+day+", "+year);
}
}

----------
package untitled8;

public class Lab0424Demo {
public static void main(String [] args)
{
Lab0424 date1 = new Lab0424() ,
date2 = new Lab0424() ;

date1.setDate(17,"Jun",1882);
date2.setDate(17,"Jun",1882);
if (date1.equals(date2) )
System.out.println(date1+" eaquls "+ date2);
else
System.out.println(date1+" does not eaquls "+ date2);
date1.setDate(28,"July",1882);

if (date1.precedes(date2) )
System.out.println(date1+" comes before "+ date2);
else
System.out.println(date2+" comes brfore or is equal to "+ date1);
//System.out.println(date1.toString()+" "+"\n"+date1.getMonth()+"/"+date1.getDay()+", "+date1.getYear() );
System.exit(0) ;
}
}

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 Taiwan License.