import java.awt.*; import java.awt.event.*; import java.applet.*; /*This Applet calculates the day of the week for an given date using Zeller's formula. For simplicity, dates in the range 1901-1999 are assumed. (This avoids the millennium bug issue: years ending in 00 divisible by 4 that are leap years.) Note that the applet will produce erroneous results if you do not press the enter key after each entry. */ /*Written for IT 108 by Anne Marchant 1/02. Developers are advised that the more modern Swing classes should be used for professional applications (using JApplet, JButton, JTextField, etc.). However, since these require Netscape 6 (or greater) and the Java 2 runtime, and these may not be available to all students, the Java 2 AWT has been used here. */ public class dayofweekapplet extends Applet implements ActionListener { TextField dayField, monthField, yearField; //User input fields TextField dayofweek; //Output field Button calcButton; //Control button static int day=0, month=0, year=0; //user input static int century=19; //Assume 20th century static int answer=0; //result public void init() { //We will do our own layout to keep it simple setLayout(null); //Create 3 text fields for input and make them active dayField=new TextField(" "); dayField.setBounds(200,55,50,25); dayField.setEditable(true); dayField.addActionListener(this); add(dayField); monthField=new TextField(" "); monthField.setBounds(200,105,50,25); monthField.setEditable(true); monthField.addActionListener(this); add(monthField); yearField=new TextField(" "); yearField.setBounds(200,155,50,25); yearField.setEditable(true); yearField.addActionListener(this); add(yearField); //make a button and activate it calcButton=new Button("Calculate"); calcButton.setBounds(10,200,75,25); calcButton.addActionListener(this); add(calcButton); //create an output text field dayofweek=new TextField(""); dayofweek.setBounds(200,250,200,25); dayofweek.setEditable(false); add(dayofweek); setSize(610,500); } //handle user input public void actionPerformed(ActionEvent e) { //get values entered into input fields if(e.getSource()==dayField) day=(int)Integer.parseInt(dayField.getText().trim()); if(e.getSource()==monthField) month=(int)Integer.parseInt(monthField.getText().trim()); if(e.getSource()==yearField) year=(int)Integer.parseInt(yearField.getText().trim()); //when user presses button, do calculation if(e.getActionCommand().equals("Calculate")) { if (month <= 2) //adjust values to match formula, since March=1 { //if jan or feb, year is previous year year -= 1; if (year < 0) //handle 1900 dates...not really needed for this { //applet, but useful if we ever expand its year = 99; //functionality century -= 1; } } if (month > 2) // adjust month to match formula Mar=1, Feb=12 month -= 2; // if month is March or higher, subtract 2 else month += 10; // else, month is Jan. or Feb., so add 10 answer = get_day_of_week(); //call function to do calculation answer = answer%7; // find specific day of week //print answer switch(answer) { case 0: dayofweek.setText("Sunday."); break; case 1: dayofweek.setText("Monday."); break; case 2: dayofweek.setText("Tuesday."); break; case 3: dayofweek.setText("Wednesday."); break; case 4: dayofweek.setText("Thursday."); break; case 5: dayofweek.setText("Friday."); break; case 6: dayofweek.setText("Saturday."); break; default: dayofweek.setText("d:"+day+" m:"+month+"y:"+year+ " c:"+century); } repaint(); } } // end get_birthdate() //Zeller's Rule public int get_day_of_week() { int f=0; f = day + (13*month-1)/5 + year + year/4 + century/4 - 2*century; return f; } //Labels for screen public void paint (Graphics g) { g.drawString("This program determines the day of the week for a given date: 1901-1999.",5,25); g.drawString("Press the enter key after each entry:",5,40); g.drawString("Enter Day: ",5,65); g.drawString("Enter Month: ",5,115); g.drawString("Enter last 2 digits of year (01-99):",5,165); g.drawString("The day of the week for this date is: ",5,260); } // end print_day_of_week }