Class 1, Part 2 | Intro to Java 30-IT-396 |
Dates, Multiple Constructors Example
// import java.util so that we can use GregorianCalendar and Date
import java.util.*;
/**
* class DueDates
*
* A class used to demonstrate multiple constructors, GregorianCalendar,
and
* Date objects.
*
* @author jonesbr@email.uc.edu
* @version 1.0
*/
public class DueDates {
/**
* The default constructor. Assumes
the current quarter is Winter 02.
*/
public DueDates() {
// Figure out the proper
quarter intellegently, by looking at the current month.
GregorianCalendar gcToday
= new GregorianCalendar();
// Get the month and
year for comparison purposes.
int month = gcToday.get(Calendar.MONTH);
int year = gcToday.get(Calendar.YEAR);
// See where we are.
if (month >= 0 &&
month <= 2 && year == 2002) {
// initialize winter quarter dates.
System.out.println("Initializing Winter 2002");
initializeWinter02();
} else if (month >=
8 && month <= 11) {
// initialize autumn quarter dates.
System.out.println("Initializing Autumn 2002");
initializeAutumn02();
} else {
// Unable to compute. Print an informational message.
System.out.println("DueDates is not currently set up to use the current
date.");
System.out.print("Please enter a quarter and year in the command");
System.out.println("arguments when starting DueDates.");
}
}
/**
* This constructor assigns the quarter
from the command line arguments.
*
*/
public DueDates(String quarter) {
// initialize to the
appropriate quarter based on the command line arguments.
if (quarter.equals("Winter02"))
{
initializeWinter02();
} else if (quarter.equals("Autumn02"))
{
initializeAutumn02();
} else {
System.out.println("Invalid command line arguments. Please try again.");
}
}
/**
* initializeWinter02
*
* A method to initialize due dates for
Winter Quarter 2002
*
* @return void
*/
public void initializeWinter02(){
// Jan 30 Quiz
// Feb 13 Proj 1
// Feb 20 Midterm
// For each date, instantiate
the GregorianCalendar and get the time.
// We can do this all
in one line, or break it up into multiple lines.
Date quiz = (new GregorianCalendar(2002,
0, 30)).getTime();
Date projectOne = (new
GregorianCalendar(2002, 1, 13)).getTime();
Date midterm = (new
GregorianCalendar(2002, 1, 20)).getTime();
sendAlert(quiz, projectOne,
midterm);
}
/**
* initializeAutumn02
*
* A method to initialize due dates for
Autumn Quarter 2002
*
* @return void
*/
public void initializeAutumn02() {
// Oct 9 Quiz
// Oct 24 Proj 1
// Oct 31 Midterm
// For each date, instantiate
the GregorianCalendar and get the time.
// We can do this all
in one line, or break it up into multiple lines.
Date quiz = (new GregorianCalendar(2002,
9, 9)).getTime();
Date projectOne = (new
GregorianCalendar(2002, 9, 24)).getTime();
Date midterm = (new
GregorianCalendar(2002, 9, 31)).getTime();
sendAlert(quiz, projectOne,
midterm);
}
/**
* sendAlert
*
* A method to send alerts based on due
dates
*
* @param quiz the due date of the quiz
* @param projectOne the due date of projectOne
* @param midterm the due date of the midterm
*
* @return void
*/
public void sendAlert(Date quiz, Date projectOne,
Date midterm) {
// Get the current date
so that we can make comparisons
// and print informational
statements.
Date now = new Date();
// Have we already had
the quiz?
if (now.after(quiz))
{
System.out.println("Did you take your quiz? I hope so!");
} else {
System.out.println("Study for the quiz!");
}
// Have we already had
Project One?
if (now.after(projectOne))
{
System.out.println("Did you finish Project One? I hope so!");
} else {
System.out.println("How is Project One coming? Thank goodness you
have more time!");
}
// Have we already had
the midterm?
if (now.after(midterm))
{
System.out.println("Did you take the midterm? I hope so!");
} else {
System.out.println("Study for the midterm!");
}
}
/**
* main
*
* Method called when the program is run.
*/
public static void main(String args[]) {
DueDates dueDates;
// An example of using
two different constructors.
// If an argument was
provided, pass it up to the appropriate constructor.
// If not, just use
the default constructor.
if (args.length == 1)
{
dueDates = new DueDates(args[0]);
} else {
dueDates = new DueDates();
}
}
}
Created by: Brandan
Jones January 4, 2002