Write a program that prompts the user for a day (1 – 31 ), month (1 – 12), and y
ID: 3580783 • Letter: W
Question
Write a program that prompts the user for a day (1 – 31 ), month (1 – 12), and year (>= 1583) and prints out the day corresponding to that date—that is, Sunday, Monday, Tuesday, etc. After printing the day of the week, your program should ask if the user wants to compute another date and repeats as necessary. Your program should call the dayOfTheWeek function and passes its output to the PrintDayOfTheWeek function in order to display the day of the week given the user inputs. Your program should notify the user if the input values are invalid and allow the user to enter inputs again (note that the dayOfTheWeek function returns -1 if the day, month, or year is invalid, so you can use the output of this function to determine if any of the input values is invalid).
Display your name as the first line of outputs in your program. Display the inputs/outputs in the following format:
MY NAME - Final Exam – CSIS 113A
Enter month: 1
Enter day: 1
Enter year: 2003
The day of the week is Wednesday
Do you want to continue? [y/n] y
Enter month: 12
Enter day: 31
Enter year: 2040
The day of the week is Monday
Do you want to continue? [y/n] y
Enter month: 3
Enter day: 0
Enter year: 2015
Your input values are invalid. Please try again.
Enter month: 5
Enter day: 21
Enter year: 1600
The day of the week is Sunday
Do you want to continue? [y/n] n
Press any key to continue . . .
Explanation / Answer
DayOfWeek.java
import java.util.Scanner;
public class DayOfWeek {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Suresh Murapaka - Final Exam – CSIS 113A");
while(true){
System.out.print("Enter month (1-12): ");
int m = scan.nextInt();
System.out.print("Enter the day of the month (1-31): ");
int q = scan.nextInt();
System.out.print("Enter year e.g, 1918: ");
int year = scan.nextInt();
int weekDay;
if(m == 1 || m == 2){
m = m + 12;
year = year - 1;
}
int j = Integer.parseInt(String.valueOf(year).substring(0, 2));
int k = Integer.parseInt(String.valueOf(year).substring(2, 4));
weekDay = (q + ((13 * (m + 1)) / 5) + k + (k / 4) + (j / 4) + (5 * j)) % 7;
String s = "";
switch (weekDay) {
case 0:
s = "Saturday";
break;
case 1:
s = "Sunday";
break;
case 2:
s = "Monday";
break;
case 3:
s = "Tuesday";
break;
case 4:
s = "Wednesday";
break;
case 5:
s = "Thrusday";
break;
case 6:
s = "Friday";
break;
}
System.out.println("The day of the week is " + s);
System.out.println("Do you want to continue? [y/n]: ");
char ch = scan.next().charAt(0);
if(ch=='n' || ch == 'N')
break;
}
}
}
Output:
Suresh Murapaka - Final Exam – CSIS 113A
Enter month (1-12): 1
Enter the day of the month (1-31): 1
Enter year e.g, 1918: 2003
The day of the week is Wednesday
Do you want to continue? [y/n]:
y
Enter month (1-12): 12
Enter the day of the month (1-31): 31
Enter year e.g, 1918: 2040
The day of the week is Monday
Do you want to continue? [y/n]:
y
Enter month (1-12): 5
Enter the day of the month (1-31): 21
Enter year e.g, 1918: 1600
The day of the week is Sunday
Do you want to continue? [y/n]:
n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.