2006-12-25

Lab Static Method, Part II

Lab Static Method

2006-12-11

12/4作業

<<overloading>>
package who_is_angela;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class DateSixthTry
{
private String month;
private int day;
private int 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);
}

private boolean dateOK(int monthInt, int dayInt, int yearInt) {
return ( (monthInt >= 1) &&amp;amp;amp;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;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")); } public void readInput() throws IOException { boolean tryAgain = true; BufferedReader keyboard = new BufferedReader(new InputStreamReader(System. in)); while (tryAgain) { System.out.println("Enter month, day, and year."); System.out.println("Do not use a comma."); String monthInput = keyboard.readLine(); int dayInput = keyboard.read(); int yearInput = keyboard.read(); if (dateOK(monthInput, dayInput, yearInput)) { setDate(monthInput, dayInput, yearInput); tryAgain = false; } else System.out.println("Illegal date. Reenter input."); } } public void writeOutput() { System.out.println(month + " " + day + ", " + year); } public void setMonth(int month) { if ( (month <= 0) || (month > 12)) {
System.out.println("Fatal Error");
System.exit(0);
}
else
this.month = monthString(month);
}

public void setMonth(String month) {
this.month = month;
}

public void setDay(int day) {
if ( (day <= 0) || (day > 31)) {
System.out.println("Fatal Error");
System.exit(0);
}
else
this.day = day;
}

public void setYear(int year) {
if ( (year <> 9999)) {
System.out.println("Fatal Error");
System.exit(0);
}
else
this.year = year;
}

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

public boolean precedes(DateSixthTry otherDate) {
return ( (year < year ="=" year ="=" date1 =" new" date2 =" new" date3 =" new" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://photos1.blogger.com/x/blogger/5813/3816/1600/925535/%3F%3F%3F%3F%3F%3F%3F%3F%3F7.jpg">









.................................................................................................................................
<>






































12/4 Lab Java Constructor
































































2006-12-04

Lab Class Definitions III