horsemanship世界

星期一, 5月 01, 2006

Lab9

import java.io.*;
public class Date
{
private String month;
private int day;
private int year;
public Date ( )
{
month = "January";
day = 1;
year = 1000;
}
public Date (int monthInt, int day, int year)
{
setDate( monthInt, day, year);
}
public Date (String monthString, int day, int year)
{
setDate(monthString, day, year);
}
public Date (int year)
{
setDate(1, 1, year);
}
public Date (Date aDate){if (aDate == null)
{
System.out.println("Fatal Error.");
System.exit(0);
}
month = aDate.month;
day = aDate.day;
year = aDate.year;
}
public void setDate(int monthInt, int day, int year){if (dateOK(monthInt, day, year))
{
this.month = monthString(monthInt);
this.day = day;this.year = year;
}
else{System.out.println("Fatal Error");
System.exit(0);
}}
public void setDate(String monthString, int day, int year)
{
if (dateOK(monthString, day, year))
{
this.month = monthString;
this.day = day;
this.year = year;
}
else{System.out.println("Fatal Error");
System.exit(0);
}}
public void setDate(int year){setDate(1, 1, year);
}
public void setYear(int year){if ( (year > 9999) )
{
System.out.println("Fatal Error");System.exit(0);
}
else this.year = year;
}
public void setMonth(int monthNumber)
{
if ((monthNumber <= 0) ||(monthNumber > 12))
{
System.out.println("Fatal Error");
System.exit(0);
}
else month = monthString(monthNumber);
}
public void setDay(int day){if ((day <= 0) ||(day > 31))
{
System.out.println("Fatal Error");
System.exit(0);
}
else this.day = day;
}
public int getMonth( )
{
if (month.equals("January"))return 1;
else if (month.equals("February"))return 2;
else if (month.equalsIgnoreCase("March"))return 3;
else if (month.equalsIgnoreCase("April"))return 4;
else if (month.equalsIgnoreCase("May"))return 5;
else if (month.equals("June"))return 6;
else if (month.equalsIgnoreCase("July"))return 7;
else if (month.equalsIgnoreCase("August"))return 8;
else if (month.equalsIgnoreCase("September"))return 9;
else if (month.equalsIgnoreCase("October"))return 10;
else if (month.equals("November"))return 11;
else if (month.equals("December"))return 12;
else
{
System.out.println("Fatal Error");System.exit(0);
return 0;
}}
public int getDay( ){return day;
}
public int getYear( ){return year;
}
public String toString( ){return (month + " " + day + ", " + year);
}
public boolean equals(Date otherDate)
{
return ( (month.equals(otherDate.month))
&&amp;amp;amp; (day == otherDate.day)
&& (year == otherDate.year) );
}
public boolean precedes(Date otherDate)
{
return ( (year < year ="=" tryagain =" true;" keyboard =" new" monthinput =" keyboard.readLine(" dayinput =" Integer.parseInt(keyboard.readLine());" yearinput =" Integer.parseInt(keyboard.readLine());" tryagain =" false;">= 1)
&&amp;amp;amp; (monthInt <= 12) &&(dayInt >= 1)
&& (dayInt <= 31) &&(yearInt >= 1000)
&& (yearInt <= 9999) ); } private boolean dateOK(String monthString, int dayInt, int yearInt) { return ( monthOK(monthString) &&amp;amp;amp;(dayInt >= 1)
&& (dayInt <= 31) &&(yearInt >= 1000)
&& (yearInt <= 9999) );
}
private boolean monthOK(String month)
{
return (month.equals("January")
|| month.equals("February") || month.equals("March")
|| month.equals("April") ||month.equals("May")
||month.equals("June") ||month.equals("July")
|| month.equals("August") ||month.equals("September")
||month.equals("October") ||month.equals("November")
|| month.equals("December") );
}private String monthString(int monthNumber){switch (monthNumber){
case 1:return "January";
case 2:return "February";
case 3:return "March";
case 4:return "April";
case 5:return "May";
case 6:return "June";
case 7:return "July";
case 8:return "August";
case 9:return "September";
case 10:return "October";
case 11:return "November";
case 12:return "December";
default:System.out.println("Fatal Error");
System.exit(0);
return "Error";
}}}
===============================
public class ConttructorsDemo {
public static void main(String[]args)
{
Date date1=new Date("December",16,1770),
date2=new Date(1,27,1756),
date3=new Date (1882),
date4=new Date ();

System.out.println(" Whose birthday is "+date1+"?");
System.out.println(" Whose birthday is "+date2+"?");
System.out.println(" Whose birthday is "+date3+"?");
System.out.println(" Whose birthday is "+date4+"?");}}

結果:
Whose birthday is December 16, 1770?
Whose birthday is January 27, 1756?
Whose birthday is January 1, 1882?
Whose birthday is January 1, 1000?
==========================================
public class ConttructorsDemo {
public static void main(String[]args)
{
Date birthday1 = new Date("January",1,2000),
birthday2 = new Date();
Date birthday3 = new Date("February",1,2000);
birthday2.setDate("February",1,2000);
System.out.println(" Whose birthday is "+birthday2+"?");
Date birthday4 = new Date("March",1,2000);
System.out.println(" Whose birthday is "+birthday1+"?");
System.out.println(" Whose birthday is "+birthday3+"?");
System.out.println(" Whose birthday is "+birthday4+"?");}}

結果:
Whose birthday is February 1, 2000?
Whose birthday is January 1, 2000?
Whose birthday is February 1, 2000?
Whose birthday is March 1, 2000?

0 Comments:

張貼留言

<< Home