JAVA

Tuesday, December 05, 2006

Lab Java Constructor

package sss3;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Date
{
private String month;
private int day;
private int year;
public Date() {
month = "Jan";
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 < 1000) || (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("Jan")) {return 1;
} else if (month.equals("Feb")) {return 2;
} else if (month.equals("Mar")) {return 3;
} else if (month.equals("Apr")) {return 4;
} else if (month.equals("May")) {return 5;
} else if (month.equals("Jun")) {return 6;
} else if (month.equals("Jul")) {return 7;
} else if (month.equals("Aug")) {return 8;
} else if (month.equals("Sep")) {return 9;
} else if (month.equals("Oct")) {return 10;
} else if (month.equals("Nov")) {return 11;
} else if (month.equals("Dec")) {return 12;
} else {
System.out.println("Fatal Error");
System.exit(0);
return 0; //Needed to keep the compiler happy
}}
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))
&& (day == otherDate.day) && (year == otherDate.year));
}
public boolean precedes(Date otherDate) {
return ((year < otherDate.year) ||
(year == otherDate.year && getMonth() < otherDate.getMonth()) ||
(year == otherDate.year && month.equals(otherDate.month)
&& day < otherDate.day));
}
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 on three lines.");
System.out.println(
"Enter month, day, and year as three integers.");
int monthInput = Integer.parseInt(keyboard.readLine());
int dayInput = Integer.parseInt(keyboard.readLine());
int yearInput = Integer.parseInt(keyboard.readLine());
if (dateOK(monthInput, dayInput, yearInput)) {
setDate(monthInput, dayInput, yearInput);
tryAgain = false;
} else {
System.out.println("Illegal date. Reenter input.");
}}}
private boolean dateOK(int monthInt, int dayInt, int yearInt) {
return ((monthInt >= 1) && (monthInt <= 12) &&
(dayInt >= 1) && (dayInt <= 31) &&
(yearInt >= 1000) && (yearInt <= 9999));
}
private boolean dateOK(String monthString, int dayInt, int yearInt) {
return (monthOK(monthString) &&
(dayInt >= 1) && (dayInt <= 31) &&
(yearInt >= 1000) && (yearInt <= 9999));
}
private boolean monthOK(String month) {
return (month.equals("Jan") || month.equals("Feb") ||
month.equals("Mar") || month.equals("Apr") ||
month.equals("May") || month.equals("Jun") ||
month.equals("Jul") || month.equals("Aug") ||
month.equals("Sep") || month.equals("Oct") ||
month.equals("Nov") || month.equals("Dec"));
}
private String monthString(int monthNumber) {
switch (monthNumber) {
case 1:return "Jan";
case 2:return "Feb";
case 3:return "Mar";
case 4:return "Apr";
case 5:return "May";
case 6:return "Jun";
case 7:return "Jul";
case 8:return "Aug";
case 9:return "Sep";
case 10:return "Oct";
case 11:return "Nov";
case 12:return "Dec";
default:
System.out.println("Fatal Error");
System.exit(0);
return "Error";
}}}
-----------
package sss3;
class abc {
public static void main(String[] args) {
Date date1 = new Date("Jan", 1, 2000), date2 = new Date("Feb", 1, 2000),
date3 = new Date("Feb" , 1 , 2000), date4 = new Date("Mar" , 1 , 2000);
System.out.println( date1 );
System.out.println( date2 );
System.out.println( date3);
System.out.println( date4);
}}
----------------
Jan 1, 2000

Feb 1, 2000

Feb 1, 2000

Mar 1, 2000

0 Comments:

Post a Comment

<< Home