Sunday, 8 March 2015

Java Program to compute the date of Easter Sunday with GUI




This is a Java Application which computes the date of Easter Sunday.

The date of the Easter changes from year to year. Generally its is the first sunday after the first full moon of Spring.New Testment says that the crucification of Jesus Christ took place during Passover(which is after first full moon of Spring).

So the date changes every year. An algorithm was invented by a mathematician Carl Friedrich Gauss in 1800 to compute the date of Easter Sunday.



Here is the algorithm by Carl Friedrich Gauss :

Step 1  :  Let Y be the year
Step 2  :  Divide Y by 19 to get a remainder as A
Step 3  :  Divide Y by 100 to get a quotient  B and a remainder C
Step 4  :  Divide B by 4 to get a quotient D and a remainder E
Step 5  :  Divide (8*B+13) by 25 to get a quotient G
Step 6  :  Divide (19*A+B-D-G+15) by 30 to get a remainder H
Step 7  :  Divide C by 4 to get a quotient J and a remainder K
Step 8  :  Divide (A+11*H) by 319 to get a quotient M
Step 9  :  Divide (2*E+2*J-K-H+M+32) by 7 to get a remainder R
Step 10:  Divide (H-M+R+90) by 25 to get a quotient N
Step 11: Divide (H-M+R+N+19) by 32 to get a remainder P


So for a given year Y Easter Sunday falls on day P and month N


Get the application here (.jar file)    Application on Github

Here we will use a JLabel and a JTextField for entering the year another JLabel for displaying the result and a JButton for computing
JLabel year = new JLabel("Enter Year");
JTextField inputYear = new JTextField(20);
JButton submit = new JButton("Submit");
JLabel Lresult = new JLabel();

Now in the constructor class  for titile of the frame , type of layout , size , position , and some panels to our application.

setTitle("Easter Sunday");
setSize(400, 400);
setLayout(new GridLayout(5, 2));

mainPanel = new JPanel();
inputPanel = new JPanel();
buttonPanel = new JPanel();
resultPanel = new JPanel();

inputPanel.add(year);
inputPanel.add(inputYear);
buttonPanel.add(submit);
resultPanel.add(Lresult);

add(mainPanel);
add(inputPanel);
add(buttonPanel);
add(resultPanel);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
submit.addActionListener(this);
setLocationRelativeTo(null);

Now when the Compute button is clicked the calculation isperformed and the result is set in the JLabel Lresult.
public void actionPerformed(ActionEvent ae) {
        try {
            if (ae.getSource() == submit) {

                int y = Integer.parseInt(inputYear.getText());
                int a = y % 19;
                int b = y / 100;
                int c = y % 100;
                int d = b / 4;
                int e = b % 4;
                int g = (8 * b + 13) / 25;
                int h = (19 * a + b - d - g + 15) % 30;
                int j = c / 4;
                int k = c % 4;
                int m = (a + 11 * h) / 319;
                int r = (2 * e + 2 * j - k - h + m + 32) % 7;
                int n = (h - m + r + 90) / 25;
                int p = (h - m + r + n + 19) % 32;
                String result;
                switch (n) {
                    case 1:
                        result = "January ";
                        break;
                    case 2:
                        result = "February ";
                        break;
                    case 3:
                        result = "March ";
                        break;
                    case 4:
                        result = "April ";
                        break;
                    case 5:
                        result = "May ";
                        break;
                    case 6:
                        result = "June ";
                        break;
                    case 7:
                        result = "July ";
                        break;
                    case 8:
                        result = "August ";
                        break;
                    case 9:
                        result = "September ";
                        break;
                    case 10:
                        result = "October ";
                        break;
                    case 11:
                        result = "November ";
                        break;
                    case 12:
                        result = "December ";
                        break;
                    default:
                        result = "error";
                }

                Lresult.setText("The date of Easter for the year " + y + " is " + result + p);

            }

        } catch (Exception e) {
        }
    }

Get the application here (.jar file)    Application on Github

 The Complete Code 

package eastercalendar;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

/**
 *
 * @author Avinash
 */
public class EasterCalendar extends JFrame implements ActionListener {

    JLabel year = new JLabel("Enter Year");
    JTextField inputYear = new JTextField(20);
    JButton submit = new JButton("Submit");
    JLabel Lresult = new JLabel();

    JPanel mainPanel, inputPanel, buttonPanel, resultPanel;

    public EasterCalendar() throws Exception {
        setTitle("Easter Sunday");
        setSize(400, 400);
        setLayout(new GridLayout(5, 2));

        mainPanel = new JPanel();
        inputPanel = new JPanel();
        buttonPanel = new JPanel();
        resultPanel = new JPanel();

        inputPanel.add(year);
        inputPanel.add(inputYear);
        buttonPanel.add(submit);
        resultPanel.add(Lresult);

        add(mainPanel);
        add(inputPanel);
        add(buttonPanel);
        add(resultPanel);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        submit.addActionListener(this);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {

        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new EasterCalendar().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(EasterCalendar.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        try {
            if (ae.getSource() == submit) {

                int y = Integer.parseInt(inputYear.getText());
                int a = y % 19;
                int b = y / 100;
                int c = y % 100;
                int d = b / 4;
                int e = b % 4;
                int g = (8 * b + 13) / 25;
                int h = (19 * a + b - d - g + 15) % 30;
                int j = c / 4;
                int k = c % 4;
                int m = (a + 11 * h) / 319;
                int r = (2 * e + 2 * j - k - h + m + 32) % 7;
                int n = (h - m + r + 90) / 25;
                int p = (h - m + r + n + 19) % 32;
                String result;
                switch (n) {
                    case 1:
                        result = "January ";
                        break;
                    case 2:
                        result = "February ";
                        break;
                    case 3:
                        result = "March ";
                        break;
                    case 4:
                        result = "April ";
                        break;
                    case 5:
                        result = "May ";
                        break;
                    case 6:
                        result = "June ";
                        break;
                    case 7:
                        result = "July ";
                        break;
                    case 8:
                        result = "August ";
                        break;
                    case 9:
                        result = "September ";
                        break;
                    case 10:
                        result = "October ";
                        break;
                    case 11:
                        result = "November ";
                        break;
                    case 12:
                        result = "December ";
                        break;
                    default:
                        result = "error";
                }

                Lresult.setText("The date of Easter for the year " + y + " is " + result + p);

            }

        } catch (Exception e) {
        }
    }

}

OUTPUT : 
Java Application for computing the date of Easter Sunday

Java Application for computing the date of Easter Sunday

Java Application for computing the date of Easter Sunday




Get the application here (.jar file)    Application on Github

Related Programs :

Cpp Program to calculate the date of Easter Sunday 
C Program to Calculate Easter Sunday