Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: New to Programming revised
|
Posted: Jun 21, 2002 9:14 PM
|
|
import javax.swing.*;
public class EasterSundayCalculator{ public EasterSundayCalculator(){ init(); } public static void main(String[] args){ new EasterSundayCalculator(); } public void init(){ boolean done = false; while (!done){ int year = getYear(); JOptionPane.showMessageDialog(null,calculateEasterSunday(year)); done = (JOptionPane.showConfirmDialog(null,"Would you like to calculate another?") != JOptionPane.YES_OPTION); } System.exit(0); } public int getYear(){ int year = -1; while (year < 0){ String message = "Enter a 4 digit year between 1982 and 2048"; String input = JOptionPane.showInputDialog(null,message); try{ year = Integer.parseInt(input); }catch(NumberFormatException nfe){ System.err.println("NumberFormatException: " + nfe.getMessage()); year = -1; } System.err.println("year: " + year); if ((year < 1982)|| (year > 2048)) { year = -1; JOptionPane.showMessageDialog(null,"That was not a valid year input. Try again!","Invalid year", JOptionPane.ERROR_MESSAGE); } } return year; }
/** a is year %19 * b is year % 4 * c is year % 7 * d is (19 * a + 24) % 30 * e is (2 * b + 4 * c + 6 * d + 5) % 7 * Easter Sunday is March (22 + d + e)3(cubed) */ public String calculateEasterSunday(int year){
int a= year %19; int b = year % 4; int c = year % 7; int d = (19 * a + 24) % 30; int e = (2 * b + 4 * c + 6 * d + 5) % 7; int result = (22 + d + e) * (int)Math.sqrt(3); String month = "March"; if (result > 31){ month = "April"; result = result - 31; } return "Easter Sunday is " + month + " " + String.valueOf (result) + " in the year " + String.valueOf(year); } }
|
|