\Java
Easter.java
//********************************************************************
//
// Program #2
// Program Title: Easter.java
//
//********************************************************************
//
// Program Description and Design Overview:
// A program that prints out the date for Easter Sunday of a given year
//
// Input Requirements:
// Response to interactive prompt message for year,
// a 4 digit integer
// Response to interactive prompt message for continuing the program,
// a string reply, "yes" or "no"
// The program must also work correctly if "YES", "Yes", "NO", or "No" is entered.
//
// Output Requirements:
// Welcome message and title:
// "Welcome to the Cal Poly CPE 101 Easter Sunday Calculator"
// (followed by an extra blank line)
// User prompts:
// "Enter the 4 digit year (for example, 1999, not 99)"
// "Do you wish to calculate another Easter date (enter "no" or "yes")?"
// Output of Results:
// If an invalid year was entered (error message and prompt for another year value):
// "Invalid year. Date must be between 1900 and 2099 inclusive.
// Please enter the 4 digit year again."
// If a valid year was entered:
// "Easter is Sunday, MMM DD, in YYYY."
// Where MMM = March or April, DD = date of month, and YYYY = year.
//
// Problem Solution Discussion
// You are required to write a program that prints out the date for Easter
// Sunday of a given year. You must use the EASTER-1900 algorithm (described
// below) that calculates the month and day of Easter Sunday for any year in
// the range from 1900 to 2099 inclusive (the algorithm cannot calculate
// correct Easter dates for years outside that range, however).
//
// Data Structures
//
// Name Data Type Description
// ---- --------- -----------
// month string Month Easter is in
// answer string user input to question : "do you wish to input another year"
// year integer user input
// yearMod19 integer user input % 19
// yearMod4 integer user input % 4
// yearMod7 integer user input % 7
// xMod30 integer (19 * user input + 24) mod 30
// yMod7 integer (2 * yearMod19 + 4 * yearMod4 + 6 * yearMod7 + 5) % 7
// dayOfMonth integer the day of easter before it was adjusted
// adjDayOfMonth integer the day of easter after it was adjusted
//
// Problem Solution Design Steps
// 1. Print a welcome message and title
// 2. Repeat the following steps while the user's input response string value is not equal to "no" (either upper case or lower case letters):
// 1. Prompt the user for a year as a 4 digit integer
// 2. Input the year value
// 3. Check if the year is valid: while the year is not valid repeat these steps:
// 1. Print an error message
// 2. Prompt the user for a year as a 4 digit integer
// 3. Input the year value
// 4. Calculate the month and day using the Easter-1900 algorithm (see below)
// 5. Print out the result
// 6. Prompt the user: "Do you wish to input another year (enter "no" or "yes")?"
// 7. Input the user's response string
//
// Program Preconditions:
// user must enter valid year
//
//********************************************************************
import cs1.Keyboard;
public class Easter
{
//-----------------------------------------------------------------
// A comment that must be replaced by comment(s) describing the // program's main method.
//-----------------------------------------------------------------
public static void main (String[] args)
{
String month ="",
answer="yes";
int year, //user input
yearMod19, //user input % 19
yearMod4, //user input % 4
yearMod7, //user input % 7
xMod30, //(19 * user input + 24) mod 30
yMod7, //(2 * yearMod19 + 4 * yearMod4 + 6 * yearMod7 + 5) % 7
dayOfMonth, //the day of easter before it was adjusted
adjDayOfMonth=0; // the day of easter after it was adjusted
System.out.println ("Welcome to the Cal Poly CPE 101 Easter Sunday Calculator");
System.out.println ();
while (! answer.equalsIgnoreCase("no"))//when value of string answer does not eqaul "no" without regard to case
{
System.out.println ("Enter the 4 digit year (for example, 1999, not 99)");
year = Keyboard.readInt(); //stores user input into integer variable
while (year <1900 || year >2100) //when valud of year is not between 1900 & 2100
{
System.out.println ("Invalid year. Date must be between 1900 and 2099 inclusive.\nPlease enter the 4 digit year again.");
year = Keyboard.readInt(); //stores user input into integer variable
}
//algorithm equations
yearMod19 = year % 19;
yearMod4 = year % 4;
yearMod7 = year % 7;
xMod30 = (19 * yearMod19 + 24) % 30;
yMod7 = (2 * yearMod4 + 4 * yearMod7 + 6 * xMod30 + 5) % 7;
if (year == 1954 || year == 1981 || year == 2049 || year == 2076)//if year is 1954 or 1981 or 2049 or 2076
{
dayOfMonth = (15 + xMod30 + yMod7); //algorithm
}
else
{
dayOfMonth = (22 + xMod30 + yMod7); //algorith
}
if (dayOfMonth <=31) //if integer variable dayOfMonth is greater than or equal to 31
{
month = "March";
adjDayOfMonth = dayOfMonth;
}
else if (dayOfMonth > 31)
{
month = "April";
adjDayOfMonth = (dayOfMonth - 31);
}
System.out.println ("Easter is Sunday, " + month + " " + adjDayOfMonth + ", in " + year + ".");
System.out.println ();
System.out.println ("Do you wish to input another year (enter \"yes\" or \"no\")?");
answer = Keyboard.readString(); //stores user input into string variable answer
}
} //end main
} // end class Easter