Monday, December 26, 2016

// // Leave a Comment

Exercise 3.5: Find Future Dates

Program that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1, . . ., and Saturday is 6) and the number of days after today for a future day and display the future day of the week.
/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 */

import java.util.Scanner;

public class Ex03_05 {
    public static void main(String[] args) {
        String today;
        String futureday;
        
        Scanner input = new Scanner(System.in);
        
        System.out.print("Enter today's day: ");
 int day = input.nextInt();
        System.out.print("Enter the number of days elapsed since today: ");
 int numDays = input.nextInt();
        
        if (day == 0)
            today = "Sunday";
        else if (day == 1)
            today = "Monday";
        else if (day == 2)
            today = "Tuesday";
        else if (day == 3)
            today = "Wednesday";
        else if (day == 4)
            today = "Thursday";
        else if (day == 5)
            today = "Friday";
        else
            today = "Saturday";
        
        int calcFutureday = (day + numDays) % 7;
        
        if (calcFutureday == 0)
            futureday = "Sunday";
        else if (calcFutureday == 1)
            futureday = "Monday";
        else if (calcFutureday == 2)
            futureday = "Tuesday";
        else if (calcFutureday == 3)
            futureday = "Wednesday";
        else if (calcFutureday == 4)
            futureday = "Thursday";
        else if (calcFutureday == 5)
            futureday = "Friday";
        else
            futureday = "Saturday";
        
        System.out.println("Today is " + today + " and the future day is " + 
                futureday);
    }
}
Liang Introduction to Java Programming exercise 3.5 solution
Sample run
Click here to see other solutions to Introduction to Java Programming.

0 comments:

Post a Comment


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately. To get notified of replies or follow-up comments, click the box next to notify me.

Thanks.