Class 1, Part 2 | Intro to Java 30-IT-396 |
Comprehensive Example
/*
* Reminder.java
*
* Created on February 5, 2002, 3:00 AM
*/
// import java.util so that we can use GregorianCalendar and Date
import java.util.*;
// import quarters
import quarters.*;
/**
* class Reminder
*
* A class used to demonstrate multiple constructors, GregorianCalendar,
and
* Date objects.
*
* @author jonesbr@email.uc.edu
* @version 1.0
*/
public class Reminder {
private Quarter quarter;
/**
* The default constructor. Assumes
the current quarter is Winter 02.
*/
public Reminder() {
// 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");
quarter = new WinterQuarter();
} else if (month >=
8 && month <= 11) {
// initialize autumn quarter dates.
System.out.println("Initializing Autumn 2002");
quarter = new AutumnQuarter();
} 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.");
System.exit(0);
}
sendAlert();
}
/**
* This constructor assigns the quarter
from the command line arguments.
*
*/
public Reminder(String qtr) {
// initialize to the
appropriate quarter based on the command line arguments.
if (qtr.equals("Winter02"))
{
quarter = new WinterQuarter();
} else if (qtr.equals("Autumn02"))
{
quarter = new AutumnQuarter();
} else {
System.out.println("Invalid command line arguments. Please try again.");
System.exit(0);
}
sendAlert();
}
/**
* 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() {
// 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(quarter.getQuiz()))
{
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(quarter.getProjectOne()))
{
System.out.println("Did you finish Project One? I hope so!");
} else {
System.out.println("How is Project One coming? Thank hevans you have
more time!");
}
// Have we already had
the midterm?
if (now.after(quarter.getMidterm()))
{
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[]) {
Reminder reminder;
// 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)
{
reminder = new Reminder(args[0]);
} else {
reminder = new Reminder();
}
}
}
/*
* Quarter.java
*
* Created on February 6, 2002, 12:22 AM
*/
package quarters;
import java.util.*;
/**
*
* @author jonesbr@email.uc.edu
*/
public abstract class Quarter {
protected Date quiz = new Date();
protected Date projectOne = new Date();
protected Date midterm = new Date();
/**
* method getQuiz
*
* An accessor method
to return the date of a quiz.
*
* @returns Date quiz
*/
public Date getQuiz() {
return
quiz;
}
/**
* method getProjectOne
*
* An accessor method
to return the date of a quiz.
*
* @returns Date projectOne
*/
public Date getProjectOne()
{
return projectOne;
}
/**
* method getMidterm
*
* An accessor method
to return the date of a quiz.
*
* @returns Date midterm
*/
public Date getMidterm() {
return
midterm;
}
}
/*
* AutumnQuarter.java
*
* Created on February 6, 2002, 12:22 AM
*/
package quarters;
/**
*
* @author joensbr@email.uc.edu
* @version 1.0
*/
public class AutumnQuarter extends Quarter {
/** Creates a new instance
of SpringQuarter */
public AutumnQuarter() {
quiz
= (new GregorianCalendar(2002, 9, 9)).getTime();
projectOne
= (new GregorianCalendar(2002, 9, 24)).getTime();
midterm
= (new GregorianCalendar(2002, 9, 31)).getTime();
}
}
/*
* FallQuarter.java
*
* Created on February 6, 2002, 12:23 AM
*/
package quarters;
/**
*
* @author jonesbr@email.uc.edu
* @version 1.0
*/
public class WinterQuarter extends Quarter {
/** Creates a new instance
of FallQuarter */
public WinterQuarter() {
quiz
= (new GregorianCalendar(2002, 0, 30)).getTime();
projectOne
= (new GregorianCalendar(2002, 1, 13)).getTime();
midterm
= (new GregorianCalendar(2002, 1, 20)).getTime();
}
}
The Results (without command line arguments):
Initializing Winter 2002
Study for the quiz!
How is Project One coming? Thank hevans you have more time!
Study for the midterm!
Created by: Brandan
Jones January 4, 2002