Write a Java program for Zeller’s Algorithm, which can be used to determine the day of the week for any date in the past, present, or future. The program should request the user to enter the date values (month, day, and year). Next, the program should apply the algorithm (see below), and display the appropriate day of the week. The program should loop until the user enters 0 for a month. Use the pre-test while loop with sentinel-value structure while (month!=0). In addition, validate that the month is between 0 and 12; and that day is between 1 and 31. Use the while loop structure for error checking.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write a Java program for Zeller’s Algorithm, which can be used to determine the day of the week for any date in the past, present, or future.

The program should request the user to enter the date values (month, day, and year).

Next, the program should apply the algorithm (see below), and display the appropriate day of the week.

The program should loop until the user enters 0 for a month. Use the pre-test while loop with sentinel-value structure while (month!=0).

In addition, validate that the month is between 0 and 12; and that day is between 1 and 31. Use the while loop structure for error checking.

**Java Program for Zeller’s Algorithm**

**Purpose**: This program determines the day of the week for any given date in the past, present, or future.

**Instructions**:
1. **User Input**: The program requests the user to enter the month, day, and year.
2. **Execution**: The algorithm is applied to compute the day of the week, and the result is displayed.
3. **Looping**: The program loops until the user inputs `0` for the month. Use a pre-test `while` loop with a sentinel-value structure.

**Validation**: Ensure the month is between 0 and 12, and the day is between 1 and 31. Use the `while` loop structure for error checking.

---

**Algorithm**

**Formula**:
\[ G = \left( \left[ 2.6M - 0.2 \right] + K + D + \left[ \frac{D}{4} \right] + \left[ \frac{C}{4} \right] - 2C \right) \mod 7 \]

**Where**:
- **M**: Month number
- **K**: Day of the month
- **C**: Century number (first two digits of the year)
- **D**: Year number (last two digits of the year)

**Explanations**:
- **M**: March is considered month 1, and February is month 12. January and February are part of the previous year.
- **G**: If G is less than 0, add 7 to G.
- **Casting**: Cast \([2.6M - 0.2]\) as an integer.

**Examples**:

| Month | Day | Year | M | K | C | D  |
|-------|-----|------|---|---|---|----|
| 7     | 4   | 1776 | 5 | 4 | 17| 76 |
| 1     | 17  | 2008 | 11| 17| 20| 7  (not 8) |

---
Transcribed Image Text:**Java Program for Zeller’s Algorithm** **Purpose**: This program determines the day of the week for any given date in the past, present, or future. **Instructions**: 1. **User Input**: The program requests the user to enter the month, day, and year. 2. **Execution**: The algorithm is applied to compute the day of the week, and the result is displayed. 3. **Looping**: The program loops until the user inputs `0` for the month. Use a pre-test `while` loop with a sentinel-value structure. **Validation**: Ensure the month is between 0 and 12, and the day is between 1 and 31. Use the `while` loop structure for error checking. --- **Algorithm** **Formula**: \[ G = \left( \left[ 2.6M - 0.2 \right] + K + D + \left[ \frac{D}{4} \right] + \left[ \frac{C}{4} \right] - 2C \right) \mod 7 \] **Where**: - **M**: Month number - **K**: Day of the month - **C**: Century number (first two digits of the year) - **D**: Year number (last two digits of the year) **Explanations**: - **M**: March is considered month 1, and February is month 12. January and February are part of the previous year. - **G**: If G is less than 0, add 7 to G. - **Casting**: Cast \([2.6M - 0.2]\) as an integer. **Examples**: | Month | Day | Year | M | K | C | D | |-------|-----|------|---|---|---|----| | 7 | 4 | 1776 | 5 | 4 | 17| 76 | | 1 | 17 | 2008 | 11| 17| 20| 7 (not 8) | ---
**Suggested Logic:**

- **Display a Title**
- **Get the month from the user**
  - Validate the month
- **Get the day from the user**
  - Validate the day
- **Get the year from the user**
- **Calculate the day of the week**
  - Convert M (month) to the correct value and change year if necessary
  - Convert the year to C and D
  - After setting up the values for M, K, C, and D, use Java operators to apply the formula
  - Get the value of G
- **Display the day of the week using a switch statement:**
  - G is the day of the week where:
    - 0 = Sunday
    - 1 = Monday
    - 2 = Tuesday
    - 3 = Wednesday
    - 4 = Thursday
    - 5 = Friday
    - 6 = Saturday
    - Any other value: Display an error message.
- **Display the number of entries made by the user**

**You should do this project in small steps. Please do not try to do everything at once…**

**Here is a possible plan:**

- Code and test the display of the Title
- Then, ask the user to enter the month.
- Set up the while loop with sentinel value (month != 0)
- Ask the user to enter the day and year
- Add the counter
- Display the counter

**After the above code works (make sure that the user can enter several sets of data, and exit with month = 0):**

- Add the validations for month and day
- Add the if and calculations statements for G
- Display the value of G (once you get the value right, then delete this statement)
- Display the corresponding day of the week.
Transcribed Image Text:**Suggested Logic:** - **Display a Title** - **Get the month from the user** - Validate the month - **Get the day from the user** - Validate the day - **Get the year from the user** - **Calculate the day of the week** - Convert M (month) to the correct value and change year if necessary - Convert the year to C and D - After setting up the values for M, K, C, and D, use Java operators to apply the formula - Get the value of G - **Display the day of the week using a switch statement:** - G is the day of the week where: - 0 = Sunday - 1 = Monday - 2 = Tuesday - 3 = Wednesday - 4 = Thursday - 5 = Friday - 6 = Saturday - Any other value: Display an error message. - **Display the number of entries made by the user** **You should do this project in small steps. Please do not try to do everything at once…** **Here is a possible plan:** - Code and test the display of the Title - Then, ask the user to enter the month. - Set up the while loop with sentinel value (month != 0) - Ask the user to enter the day and year - Add the counter - Display the counter **After the above code works (make sure that the user can enter several sets of data, and exit with month = 0):** - Add the validations for month and day - Add the if and calculations statements for G - Display the value of G (once you get the value right, then delete this statement) - Display the corresponding day of the week.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education