// // ************************************************ // // Philip J. Porvaznik // // Java programming I // // Finds date for Easter for 1982 to 2048 // // problem #3 page 287 // // ************************************************ // import java.awt.*; import java.awt.event.*; import java.io.*; public class Easter extends Frame { private static Frame outputDisplay; private static TextField tYear; private static Label outputLabel; /////////////////////////////////////////////////////////// // // Add Action Handler and Listener // /////////////////////////////////////////////////////////// private static class ActionHandler implements ActionListener { public void actionPerformed (ActionEvent event) { // get the command button id (Exit or Compute) String id = event.getActionCommand (); if (id == "Exit") { outputDisplay.dispose(); System.exit(0); } // not Exit so must be Compute int a, b, c, d, e; int day, year; String month; // get the year from the text box tYear year = Integer.valueOf (tYear.getText()).intValue(); // perform calculations a = year % 19; b = year % 4; c = year % 7; d = (19 * a + 24) % 30; e = (2 * b + 4 * c + 6 * d + 5) % 7; // get the final calculation for day day = 22 + d + e; month = "March"; // set to March, if greater than 31 we have April if (day > 31) { month = "April"; day = day - 31; } // output the computed date outputLabel.setText("The date is " + month + " " + day); } } ///////////////////////////////////////////////////////////// // // MAIN HERE // //////////////////////////////////////////////////////////// public static void main(String args[]) { String month = ""; int day = 0; ActionHandler action; // Create a Frame and display it on the screen outputDisplay = new Frame (); outputDisplay.setLayout (new GridLayout (3,3) ); // Add intro message outputDisplay.add (new Label("Finds date for Easter Sunday 1982 to 2048")); // Add a text field for input outputDisplay.add (new Label("Enter the year: ")); tYear = new TextField ("", 4); outputDisplay.add (tYear); // Add two buttons for compute and exit Button bExit = new Button ("Exit"); bExit.setActionCommand ("Exit"); action = new ActionHandler(); bExit.addActionListener(action); outputDisplay.add (bExit); Button bCompute = new Button ("Compute Date"); bCompute.setActionCommand ("Compute"); action = new ActionHandler(); bCompute.addActionListener(action); outputDisplay.add (bCompute); // Add computed result and text outputLabel = new Label("The computed date shows here"); outputDisplay.add (outputLabel); outputDisplay.add (new Label("To quit close window or click exit")); // Show the frame we created outputDisplay.pack (); outputDisplay.show (); // Event handler for window closing outputDisplay.addWindowListener(new WindowAdapter() // Create a WindowAdapter { public void windowClosing(WindowEvent event) { outputDisplay.dispose(); System.exit(0); } }); } }