SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Conditional Execution
Exercise:
Programming Projects
Question:4 | ISBN:9780136091813 | Edition: 2

Question

Write a program that prompts for two people’s birthdays (month and day), along with today’s month and day. The program should figure out how many days remain until each user’s birthday and which birthday is sooner. Hint: It is much easier to solve this problem if you convert each date into an “absolute day” of year, from 1 through 365.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

import java.util.Scanner;

public class BirthdayComparisonDemo
{
    public static void main(String[] args) 
    {
        // Create a Scanner object to read user input
        Scanner scanner = new Scanner(System.in);

        // Prompt for and read the birthdays of person 1 (month and day)
        System.out.println("Person 1 Birthday");
        System.out.print("Enter the month (1-12): ");
        int month1 = scanner.nextInt();
        System.out.print("Enter the day (1-31): ");
        int day1 = scanner.nextInt();

        // Prompt for and read the birthdays of person 2 (month and day)
        System.out.println("\nPerson 2 Birthday");
        System.out.print("Enter the month (1-12): ");
        int month2 = scanner.nextInt();
        System.out.print("Enter the day (1-31): ");
        int day2 = scanner.nextInt();

        // Prompt for and read today's date (month and day)
        System.out.println("\nToday's Date");
        System.out.print("Enter the month (1-12): ");
        int todayMonth = scanner.nextInt();
        System.out.print("Enter the day (1-31): ");
        int todayDay = scanner.nextInt();

        // Calculate the absolute day of the year for each date using the formula
        int absoluteDay1 = calculateAbsoluteDay(month1, day1);
        int absoluteDay2 = calculateAbsoluteDay(month2, day2);
        int todayAbsoluteDay = calculateAbsoluteDay(todayMonth, todayDay);

        // Calculate the number of days remaining until each person's birthday
        int daysRemaining1 = absoluteDay1 - todayAbsoluteDay;
        int daysRemaining2 = absoluteDay2 - todayAbsoluteDay;

        // Determine which birthday is sooner
        String soonerBirthday;
        if (daysRemaining1 < 0) 
        {
            soonerBirthday = "Person 1";
        } else if (daysRemaining2 < 0) 
        {
            soonerBirthday = "Person 2";
        } else if (daysRemaining1 == daysRemaining2)
        {
            soonerBirthday = "Both persons have birthdays on the same day.";
        } else 
        {
            soonerBirthday = (daysRemaining1 < daysRemaining2) ? "Person 1" : "Person 2";
        }

        // Output the results
        System.out.println("\nDays Remaining Until Birthdays:");
        System.out.println("Person 1: " + daysRemaining1);
        System.out.println("Person 2: " + daysRemaining2);
        System.out.println("Sooner Birthday: " + soonerBirthday);
    }

    // Method to calculate the absolute day of the year for a given date (month and day)
    private static int calculateAbsoluteDay(int month, int day) {
        int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int absoluteDay = 0;

        // Sum the days of the months preceding the given month
        for (int i = 1; i < month; i++) {
            absoluteDay += daysInMonth[i];
        }

        // Add the days of the given month
        absoluteDay += day;

        return absoluteDay;
    }
}

OUTPUT:

Person 1 Birthday
Enter the month (1-12): 4
Enter the day (1-31): 15

Person 2 Birthday
Enter the month (1-12): 3
Enter the day (1-31): 20

Today's Date
Enter the month (1-12): 3
Enter the day (1-31): 10

Days Remaining Until Birthdays:
Person 1: 36
Person 2: 10
Sooner Birthday: Person 2

 

0 0

Discussions

Post the discussion to improve the above solution.