// Illustrate structure of a class, including instance variables
public class DateFirstTry
{
// Note that these instance variables are public
public String month; // always 3 letters long, as in Jan, Feb, etc.
public int day;
public int year; // a four digit number.
public void writeOutput( )
{
System.out.println(month + " " + day + ", " + year);
}
}
// Show how above class would be used, illustrating how data members aren’t protected
public class DateFirstTryDemo
{
public static void main(String[] args)
{
// Declare existance of date1 and date2
DateFirstTry date1, date2;
// Now allocate memory for them
date1 = new DateFirstTry( );
date2 = new DateFirstTry( );
// The above 2 steps could be combined, as in
//DateFirstTry date1 = new DateFirstTry( );
// Set the values for date1, and display the result
date1.month = "Dec";
date1.day = 31;
date1.year = 2006;
System.out.println("date1:");
date1.writeOutput( );
// Set the values for date2, and display the result
date2.month = "Julio";// Oops, it's in Spanish, and too long
date2.day = 32;// Error, too large a value
date2.year = 20987;// I doubt this program will be around then
System.out.println("date2:");
// date2.writeOutput( );// rather than call the method, we can use:
System.out.println(month + " " + day + ", " + year);
}
}
import java.io.BufferedReader;// These lines will not always be shown
import java.io.InputStreamReader;// in subsequent examples, though you
import java.io.IOException;// would always need to have them.
import java.util.Scanner; // For input
// Rather than explicitly setting values of variables, let user enter them.
public class DateSecondTry
{
private String month; //always 3 letters long, as in Jan, Feb, etc.
private int day;
private int year;// a four digit number.
public void writeOutput( )
{
System.out.println(month + " " + day + ", " + year);
}
public void readInput( )
{
Scanner keyboard = new Scanner( System.in);
System.out.println("Enter month, day, and year on three lines:");
month = keyboard.next();
day = keyboard.nextInt();
year = keyboard.nextInt();
}
public int getDay( )
{
return day;
}
public int getYear( )
{
return year;
}
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
}
}
}
// When a class has private data members, they must be accessed using a class method
public class DateSecondTryDemo
{
public static void main(String[] args)
{
DateSecondTry date1, date2;
date1 = new DateSecondTry( );
date2 = new DateSecondTry( );
// date1.month = "Dec";// generates an error since month is private
date1.getInput();
System.out.println("date1:");
date1.writeOutput( );
date2.getInput();// Can still enter invalid values
System.out.println("date2:");
// The following would generate an error, since the data members
// are private
// System.out.println(month + " " + day + ", " + year);
// Instead, we can access them using a method:
System.out.println(date2.getMonth() + " " + date2.getDay() +
", " + date2.getYear() );
}
}
// Illustrate how parameters can be used to initialize values for a date
public class DateThirdTry
{
private String month; //always 3 letters long, as in Jan, Feb, etc.
private int day;
private int year;// a four digit number.
public void setDate( int newMonth, int newDay, int newYear)
{
month = monthString( newMonth);
day = newDay;
year = newYear;
}
public String monthString( int monthNumber)
{
switch ( monthNumber)
{
case 1: return "Jan"; break;
case 2: return "Feb"; break;
// ...
}
}
// display all days up until the given day
public void displayDays( day)
{
int i;
System.out.print("Days passed in the month are: ");
for (i=1; i<= day; i++) {
System.out.print(i + " ");
}
System.out.println();
}
// the rest of the methods are the same as in previous version
}
// Passing values as parameters to initialize the values for a date
public class DateThirdTryDemo
{
public static void main(String[] args)
{
DateThirdTry date = new DateThirdTry( );
int year = 1882;
date.setDate( 6, 17, year);
date.writeOutput();
}
}
// Implementing methods equals() and toString(), which could be used elsewhere in Java
public class DateFourthTry
{
private String month; //always 3 letters long, as in Jan, Feb, etc.
private int day;
private int year; //a four digit number.
public boolean equals(DateFourthTry otherDate)
{
return ( (this.month.equals(otherDate.month)) &
(this.day == otherDate.day) &
(this.year == otherDate.year)
);
}
public String toString( )
{
return (month + " " + day + ", " + year);
}
//... More methods...
} // end class DateFourthTry
// Illustrate how Encapsulation and Information hiding allow enforcing values used
public class DateFifthTry
{
private String month; //always 3 letters long, as in Jan, Feb, etc.
private int day;
private int year; //a four digit number.
// ...
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.");
}
}
public void setDate(int month, int day, int year)
{
if (dateOK(month, day, year))
{
this.month = monthString(month);
this.day = day;
this.year = year;
}
else
{
System.out.println("Fatal Error");
System.exit(0);
}
}
private boolean dateOK(int monthInt, int dayInt, int yearInt)
{
return ( (monthInt >= 1) & (monthInt <= 12) &
(dayInt >= 1) & (dayInt <= 31) &
(yearInt >= 1000) & (yearInt <= 9999) );
}
// ... other methods
} // end class DateFifthTry
public class DateSixthTry// Illustrate method overloading
{
private String month; //always 3 letters long, as in Jan, Feb, etc.
private int day;
private int year; //a four digit number.
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) & (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") );
}
// ...other methods
}//End class DateSixthTry
// Use the above DateSixthTry class to illustrate method overloading
public class OverloadingDemo
{
public static void main(String[] args)
{
DateSixthTry date1 = new DateSixthTry( ),
date2 = new DateSixthTry( ),
date3 = new DateSixthTry( );
// Note the different formats for setting a date
date1.setDate(1, 2, 2007);
date2.setDate("Feb", 2, 2007);
date3.setDate(2007);
System.out.println(date1);
System.out.println(date2);
System.out.println(date3);
}
}
// Illustrate Accessor (get) and Mutator (set) methods
public class Square
{
private int size;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
private String label;
// ...
public String getColor()
{
return color;
}
public void setColor(String newColor)
{
color = newColor;
draw();
}
// ...
}
// Illustrate the use of constructors
public class Date
{
private String month; //always 3 letters long, as in Jan, Feb, etc.
private int day;
private int year; //a four digit number.
// default constructor
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)//Not a real date.
{
System.out.println("Fatal Error.");
System.exit(0);
}
month = aDate.month;
day = aDate.day;
year = aDate.year;
}