I just need a little help with creating the code for this. Ask the user for a 4-
ID: 667908 • Letter: I
Question
I just need a little help with creating the code for this.
Ask the user for a 4-digit year and a day of the week. The user should enter the full name of the month.
Your program should produce a formatted calendar for the specified year, assuming that January 1 falls on the day of the week specified. There should be a blank line between each month, and the month and year should be centered over each month. Your program should reject any year that is less than 1000 or greater than 9999. Note that your program is taking the user's word for what day of the week the year begins on. (The year 2015 began on a Thursday; if the user enters the year 2015 and Sunday, then produce a calendar in which 2015 starts on a Sunday.) Your program should handle leap year correctly.
Can't use numbers for the day of the week input so I don't really know how to go about using a string and going about it all. If anyone can show me how to go about this whole program, that would be awesome.
Explanation / Answer
//Program
// Calendar.java
package calendar;
import java.util.Scanner;
public class Calendar {
// return true if the given year is a leap year
public static boolean isLeapYear(int year) {
if ((year % 4 == 0) && (year % 100 != 0)) return true;
if (year % 400 == 0) return true;
return false;
}
public static void main(String[] args) {
String Month ;
int Year; // year
String day;
System.out.println("Enter Day of the week");
Scanner sc=new Scanner(System.in);
day=sc.nextLine();
System.out.println("Enter Month");
Month=sc.nextLine();
int f=0;
System.out.println("Enter Year");
Year=sc.nextInt();
while(f==0)
{
if(Year<1000 || Year>9999)
{
System.out.println("Year should not be less than 1000 or greater than 9999");
System.out.println("Reenter Year");
Year=sc.nextInt();
}
else
f=1;
}
// months[i] = name of month i
String[] months = {
"", // leave empty so that months[1] = "January"
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
String[] day_of_week = {
"", // leave empty so that months[1] = "January"
"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday",
"Saturday"
};
// days[i] = number of days in month i
int[] days = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
// check for leap year
if ( (Month.equals("February"))&& isLeapYear(Year))
{
days[2] = 29;
}
// print calendar header
int ind=0;
for(int i=0;i<months.length;i++)
{
if(months[i].equalsIgnoreCase(Month)){
ind=i;
break; }
}
System.out.println(" " + months[ind] + " " + Year);
System.out.println(" S M Tu W Th F S");
int ind1=0;
for(int i=0;i<day_of_week.length;i++)
{
if(day_of_week[i].equalsIgnoreCase(day)){
ind1=i;
break; }
}
// starting day
//int d = day(ind, ind1, Year);
//int d=ind1;
// print the calendar
for (int i = 0; i < ind1-1; i++)
System.out.print(" ");
for (int i = ind1; i <= days[ind]; i++) {
System.out.printf("%2d ", i);
if (((i + ind1-1) % 7 == 0) || (i == days[ind]))
System.out.println();
}
}
}
// Output
Enter Day of the week
Sunday
Enter Month
January
Enter Year
2015
January 2015
S M Tu W Th F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31