Calculate the date of Easter Sunday
You aren't far from getting your program working. You really have two things left you need to do.
- Prompt the user for a year
- Output the date found
The trick to using a Scanner
to prompt the user for input is to create a while-loop which tests each line the user enters, and keeps repeating until it sees a legal value.
Instead of hard-coding y = 2014;
(or whatever), you want to do something like this:
Scanner input = new Scanner(System.in);
int y = -1; // No easter back in B.C.
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, each time the user doesn't enter a number, y
remains -1
and the loop continues.
You are already doing all the calculations correctly, so to end your program, you just need to output the month/day.
I wouldn't bother trying to extract the calculation into a helper 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);
Try this:
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;
}
}
An input of 2001
results in April 15
as the output.