Monday, 6 June 2016

Chapter 3 Exercise 5, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*3.5 (Find future dates) Write a 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). Also prompt the user to enter the number of days after today for a future day and display the future day of the week. Here is a sample run:



import java.util.Scanner;
 
public class ProgrammingEx3_5 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  System.out.print("Enter today's day:");
  int today = input.nextInt();
  System.out.print("Enter the number of days elapsed since today:");
  int elapsed = input.nextInt();
   
  String dayString = "";
   
  switch(today%7){
   case 1:dayString= "Monday";break;
   case 2:dayString= "Tuesday";break;
   case 3:dayString= "Wednesday";break;
   case 4:dayString= "Thursday";break;
   case 5:dayString= "Friday";break;
   case 6:dayString= "Saturday";break;
   case 0:dayString= "Sunday";break;
  }
  System.out.print("Today is " + dayString + " and the future day is " );
   
  int day = (today + elapsed);
   
  switch(day%7){
   case 1:dayString= "Monday";break;
   case 2:dayString= "Tuesday";break;
   case 3:dayString= "Wednesday";break;
   case 4:dayString= "Thursday";break;
   case 5:dayString= "Friday";break;
   case 6:dayString= "Saturday";break;
   case 0:dayString= "Sunday";break;
  }
  System.out.print(dayString);
 }
 
}

No comments :

Post a Comment