Programming in C, 1. The following formula calculates the day of the week. dw =
ID: 673189 • Letter: P
Question
Programming in C,
1. The following formula calculates the day of the week.
dw = (5cen + cen/4 + ycen + ycen/4 + 26(m+1)/10 + dm) %7
where dw is the day of the week ( 0: Saturday, 1: Sunday, and so on, 6: Friday)
ycen is the year of the century (given by -> (the year in question) % 100)
cen is the century ( given by -> (the year in question) /100)
m is the month ( 3: March, 4: April, . . ., 12: December). Now, January and February are not 1 and 2 respectively. They are considered as months 13 and 14 of the previous year. So, for January and February, you will need to convert the user input 1 to 13 and 2 to 14 for the month and change the year to the previous year.
dm is the day of the month Note that the division in the formula performs an integer division. Write a program that prompts the user to enter a year, month, and day of the month from its main( ) function and reads the values in.
The main( ) function then calls another function dayWeek( ) that performs the calculations and displays the name of the day of the week.
Make your program output to look like the following:
Ex 1:
Enter year: (e.g., 2012): 2015
Enter month 1-12: 1
Enter the day of the month: 1-31: 12
Day of the week is Sunday
Ex 2:
Enter year: (e.g., 2012): 2012
Enter month 1-12: 5
Enter the day of the month: 1-31: 12
Day of the week is Saturday
Explanation / Answer
import java.util.Scanner; /** * This program displays the name of the month month, days in the given * month, Julian day of the year, and day of the week for any given * month and day combo for 2012. */ public class Year2012 { /** * Constructor for Year2012 class */ public Year2012() { } /** * Returns the month name as a String. */ public String getMonthName(int mth) { String month = ""; switch(mth) { case 1: month = "January"; break; case 2: month = "February"; break; case 3: month = "March"; break; case 4: month = "April"; break; case 5: month = "May"; break; case 6: month = "June"; break; case 7: month = "July"; break; case 8: month = "August"; break; case 9: month = "September"; break; case 10: month = "October"; break; case 11: month = "November"; break; case 12: month = "December"; break; } return month; } /** * Returns the number of days in the given month. */ public int getDaysInMonth(int mth) { int monthDays; if(mth == 2) monthDays = 29; else if(mth == 4 || mth == 6 || mth == 9 || mth == 11) monthDays = 30; else monthDays = 31; return monthDays; } /** * Returns the Julian day of the year (1-366 as 2012 is a leap year). */ public int getDayOfTheYear(int mth, int dy) { int yearDay = dy; for (int i = 1; i 12 || month == monthInt) { System.out.println("Invalid month! Please enter a month (mm): "); month = keyboard.nextDouble(); if(month == -1 || day == -1) break mainloop; monthInt = month % 1; } while(day < 1 || day > year.getDaysInMonth((int)month) || day == dayInt) { System.out.println("Invalid day! Please enter a day (dd): "); day = keyboard.nextDouble(); if(month == -1 || day == -1) break mainloop; dayInt = day % 1; } System.out.println(" Month of year: " + year.getMonthName((int)month)); System.out.println("Days in month: " + year.getDaysInMonth((int)month)); System.out.println("Day of year: " + year.getDayOfTheYear((int)month, (int)day)); System.out.println("Day of week: " + year.getDayOfWeek(year.getDayOfTheYear((int)month, (int)day))); System.out.println(" Please enter a month (mm): "); month = keyboard.nextDouble(); monthInt = month % 1; System.out.println("Please enter a day (dd): "); day = keyboard.nextDouble(); dayInt = day % 1; } System.out.println(" -1 Detected! Ending program..."); } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.