I have a problem to determine Easter Sunday based on the algorithm invented by Carl Friedrick Gauss in 1800. I have written my code and double checked the Math several times but not matter what year I type in it tells me that Easter is on April 9th so I must have something wrong in the code. Any help would be great.

import java.util.Scanner;

public class EasterSunday
{
	public EasterSunday(int y)
	{
		
	}
	public int recordYear(int y)
	{
		return y;
	}
	
	public double getEasterSundayMonth()
	{
		a = y % 19;
		b = y / 100;
		c = y % 100;
		d = b / 4;
		e = b % 4;
		g = (8 * b + 13) / 25;
		h = (19 * a + b - d - g + 15) % 30;
		j = c / 4;
		k = c % 4;
		m = (a + 11 * h) / 319;	
		r = (2 * e + 2 * j - k - h + m + 32) % 7;
		n = (h - m + r + 90) / 25;
		return n;
	}
	
	public double getEasterSundayDay()
	{
		p = (h - m + r + n + 19) % 32;
		return p;
	}
	
	private static int y;
	private int a;
	private int b;
	private int c;
	private int d;
	private int e;
	private int g;
	private int h;
	private int j;
	private int k;
	private int m;
	private int r;
	private double n;
	private double p;
	
	public static void main(String[] args) 
	{
		Scanner in = new Scanner(System.in);
		
		EasterSunday easter = new EasterSunday(y);
		
		System.out.print("Enter the year: ");
		int y = in.nextInt();
		easter.recordYear(y);
		
		System.out.println(easter.getEasterSundayMonth());
		System.out.println(easter.getEasterSundayDay());

	}

}

You seem to use variable "y" all over the place. Even though it is not crashed with each other, it is very unclear when you read the program.

// what is "y" here?
EasterSunday easter = new EasterSunday(y);
...
...
// why are you declaring another "y" here?
int y = in.nextInt();

To ensure that the "y" value is correct, you should print out the "y" value before you call recordYear() method.

Edit:
By the way, are you sure that you implement the method recordYear() correctly?

public int recordYear(int y) {
  return y;
}

Aren't you supposed to save the value of incoming "y" instead of returning it?

Edited 5 Years Ago by Taywin: n/a

The y is for the year and all of the math steps are the algorithm for Easter Sunday so the y is repeated because it's in the algorithm that many times. I didn't think about that I'm so use to returning items but yes y needs to remain the same for the entire program so how would I save the value instead of returning it?

Can you show me the algorithm?

Anyway, when I mean that you used "y" variable all over the place, it doesn't mean its appearance, but it means how you use it. In programming, you should try to give a variable name that reflect its purpose. Also, you need to try to give a distinguish name when they may have similar purpose but different type. In this case, you use exactly the same name but different types.

Oh and most variables you used in the getEasterSundayMonth() method can be local instead of static global variable for the class. Your variables shouldn't be static at all actually...

Edited 5 Years Ago by Taywin: n/a

This article has been dead for over six months. Start a new discussion instead.