Calculate Easter Sunday

Write a program to calculate the date of Easter Sunday. Easter Sunday is the first Sunday after the first full moon of spring. Use the algorithm invented by mathematician Karl Friedrich Gaus in 1800:

  • Let y be the year (e.g. 1800 or 2001).
  • Divide y by 19 and call the remainder a . Ignore the factor.
  • Divide y by 100 to get the coefficient b and the remainder c .
  • Divide b by 4 to get the coefficient d and the remainder e .
  • Divide 8 * b + 13 by 25 to get the coefficient g . Ignore the rest.
  • Divide 19 * a + b - d - g + 15 by 30 to get the remainder h . ignore the factor.
  • Divide c by 4 to get the coefficient j and the remainder k .
  • Divide a + 11 * h by 319 to get the coefficient m . Ignore the rest.
  • Divide 2 * e + 2 * j - k - h + m + 32 by 7 to get the remainder r . Ignore the factor.
  • Divide h - m + r + 90 by 25 to get the coefficient n . Ignore the rest.
  • Divide h - m + r + n + 19 by 32 to get the remainder of p . Ignore the factor.

Easter then falls on day p month n .

For example, if y is 2001:

 a = 6 b = 20 c = 1 d = 5 e = 0 g = 6 h = 18 j = 0 k = 1 m = 0 r = 6 n = 4 p = 15 

Therefore, in 2001, Easter Sunday fell on April 15th.

Make sure you request the user for a year and enter the user in a year. Also, make sure that you display the p and n values ​​with the appropriate messages describing the values ​​displayed.


I am having a slight problem in putting this in Java code. Here is what I tried:

 import java.util.Scanner; public class Easter { public static void main(String[] args) { Scanner input = new Scanner(System.in); int y = 2014; 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; getEasterSundayMonth = n; System.out.println("Month: " + Easter.getEasterSundayMonth()); } } 

This is what I have. I do not know how to assign the material, for example, I tried to get getEasterSundayMonth so that it getEasterSundayMonth value n , I am sure that it is not right. Where am I going from here?

+6
source share
2 answers

Try the following:

 import java.util.Scanner; class Easter { public static void main(String[] args) { System.out.print("Please enter a year to calculate Easter Sunday\n>"); Scanner s = new Scanner(System.in); int inputted = getResult(s); while(inputted <= 0) { System.out.print("Expected a positive year. Please try again:\n>"); inputted = getResult(s); } System.out.println(getEasterSundayDate(inputted)); } private static int getResult(Scanner s) { while(!s.hasNextInt()) { System.out.print("Expected a valid year. Please try again:\n>"); s.nextLine(); } return s.nextInt(); } public static String getEasterSundayDate(int year) { int a = year % 19, b = year / 100, c = year % 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, 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"; } return result + p; } } 

Entering 2001 leads to April 15 as an exit.

+7
source

You are not far from getting your program to work. You really have two things you need to do.

  • User request for a year
  • Display the found date

The trick using Scanner to invite the user to enter is to create a while loop that checks every line the user enters and continues to repeat until it sees the legal value.

Instead of hard coding y = 2014; (or something else) you want to do something like this:

 Scanner input = new Scanner(System.in); int y = -1; // No easter back in BC while (y < 0) { System.out.println("Please enter a year (integer greater than zero)"); if (input.hasNextInt()) { // check to see if the user entered a number y = input.nextInt(); // if so, read it } input.nextLine(); // advance the scanner to the next line of input } 

in this case, every time the user does not enter a number, y remains -1 , and the loop continues.

You are already doing all the calculations correctly, so to complete your program you just need to print the month / day.

I would not try to extract the calculation into an auxiliary method. Just use the calculated values ​​directly in main() :

 int a = y % 19; int b = y / 100; ... int n = (h - m + r + 90) / 25; int p = (h - m + r + n + 19) % 32; System.out.println("In the year " + y + " Easter with fall on day " + p + " of month " + n); 
+4
source

Source: https://habr.com/ru/post/975792/


All Articles