Monday, December 26, 2016

// // 2 comments

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.

2 comments:

  1. the last statement on all the if statements makes the code inefficient because it allows the user to input any number like 7,8,9 and this gives the wrong results when the code is run.

    ReplyDelete
  2. for example if you enter 7 on the today variable and the 0 at the futureDay variable,the output will be today is Saturday and the futureDay is Sunday of which the future day is not supposed to change since no days have elapsed and if you enter 10 maybe ,it gives you same problems because the modulus becomes 3 and the future date becomes wednesday ...considering you have entered 0 on the futureday

    ReplyDelete


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.