Write a program to generate the entire calendar for one year. The program must get two values
from the user: (1) the year and (2) the day of the week for January 1st of that year. The year,
which should be positive, is needed to check for and handle leap years1. The day of the week for
January 1st is needed so that you know where to start the calendar. The user should enter 0 for
Sunday, 1 for Monday, ... or 6 for Saturday. As always, you need to validate the user\'s input. To
actually print the calendar, you must use a single method that prints out the calendar for one
month and then call this function 12 times from main(), once for each month in the year. To
check for a leap year you will need to write another method that takes the year as a parameter
and returns true if it’s a leap year, or false otherwise. Stubs (i.e. method signatures without any
code) for both of these methods have been provided for you.
The calendar should be printed in the form below. In this example, January starts on a Saturday
(day 6). Note that February starts on a Tuesday in this example because January ended on a
Monday.
January
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 2829
30 31
February
1 2 3 4 5
6 7 8 9 10 11 12 ...
The format he gave us is as follows:
public class PA6a {
/**
* Error to output if year is not positive
*/
static final String E_YEAR = \"The year must be positive!\";
/**
* Error to output if the day is not between 0 and 6
*/
static final String E_DAY = \"The day of January 1st must be between 0 and 6!\";
/**
* Determines if an input is a leap year
*
* @param year year in question
* @ereturn true if a leap year
*/
public static boolean isLeapYear(int year) {
return false; // TODO: replace with your code
}
/**
* Outputs a month to the console
*
* @param month title
* @param startDay 0=Sunday ... 6=Saturday
* @param numDays number of days in the month
* @return day of the week of the last day of the month
*/
public static int printMonth(String month, int startDay, int numDays) {
return 0; // TODO: replace with your code
}
/**
* Program execution point:
* input year, day of the week (0-6) of january 1
* output calendar for that year
*
* @param args command-line arguments (ignored)
*/
public static void main(String[] args) {
// TODO: write your code here
}
I am confused especially with the print month method, help is greatly appreciated.
Solution
package org.students;
import java.util.Scanner;
import com.sun.org.apache.bcel.internal.generic.IUSHR;
public class Calander {
//declaring static variables
static final String E_YEAR = \"The year must be positive!\";
static final String E_DAY = \"The day of January 1st must be between 0 and 6!\";
public static void main(String[] args) {
//Declaring variables
int getFirstDay;
int year;
Scanner scanner = new Scanner(System.in);
//This while loop continue to execute until user enters valid year
while (true) {
//Getting the year entered by the user
System.out.print(\"Enter the year:.
Write a program to generate the entire calendar for one year. The pr.pdf
1. Write a program to generate the entire calendar for one year. The program must get two values
from the user: (1) the year and (2) the day of the week for January 1st of that year. The year,
which should be positive, is needed to check for and handle leap years1. The day of the week for
January 1st is needed so that you know where to start the calendar. The user should enter 0 for
Sunday, 1 for Monday, ... or 6 for Saturday. As always, you need to validate the user's input. To
actually print the calendar, you must use a single method that prints out the calendar for one
month and then call this function 12 times from main(), once for each month in the year. To
check for a leap year you will need to write another method that takes the year as a parameter
and returns true if it’s a leap year, or false otherwise. Stubs (i.e. method signatures without any
code) for both of these methods have been provided for you.
The calendar should be printed in the form below. In this example, January starts on a Saturday
(day 6). Note that February starts on a Tuesday in this example because January ended on a
Monday.
January
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 2829
30 31
February
1 2 3 4 5
6 7 8 9 10 11 12 ...
The format he gave us is as follows:
public class PA6a {
/**
* Error to output if year is not positive
*/
static final String E_YEAR = "The year must be positive!";
/**
* Error to output if the day is not between 0 and 6
*/
static final String E_DAY = "The day of January 1st must be between 0 and 6!";
2. /**
* Determines if an input is a leap year
*
* @param year year in question
* @ereturn true if a leap year
*/
public static boolean isLeapYear(int year) {
return false; // TODO: replace with your code
}
/**
* Outputs a month to the console
*
* @param month title
* @param startDay 0=Sunday ... 6=Saturday
* @param numDays number of days in the month
* @return day of the week of the last day of the month
*/
public static int printMonth(String month, int startDay, int numDays) {
return 0; // TODO: replace with your code
}
/**
* Program execution point:
* input year, day of the week (0-6) of january 1
* output calendar for that year
*
* @param args command-line arguments (ignored)
*/
public static void main(String[] args) {
3. // TODO: write your code here
}
I am confused especially with the print month method, help is greatly appreciated.
Solution
package org.students;
import java.util.Scanner;
import com.sun.org.apache.bcel.internal.generic.IUSHR;
public class Calander {
//declaring static variables
static final String E_YEAR = "The year must be positive!";
static final String E_DAY = "The day of January 1st must be between 0 and 6!";
public static void main(String[] args) {
//Declaring variables
int getFirstDay;
int year;
Scanner scanner = new Scanner(System.in);
//This while loop continue to execute until user enters valid year
while (true) {
//Getting the year entered by the user
System.out.print("Enter the year: ");
year = scanner.nextInt();
if (year < 0) {
System.out.println(E_YEAR);
} else
break;
}
//This loop continue to execute until user enters valid first day of the month
while (true) {
//Getting the first day entered by the user
System.out.print("Enter 1st day of year ( 0 = Sunday, 6 = Satuday ): ");
getFirstDay = scanner.nextInt();
4. //Checking whether the first day is within valid range or not
if (getFirstDay < 0 || getFirstDay > 6) {
System.out.println(E_DAY);
continue;
} else
break;
}
for (int month = 1; month <= 12; month++) {
int days = 0;
String monthName = " ";
switch (month) {
case 1:
monthName = "January";
days = 31;
break;
case 2:
monthName = "February";
if (isLeapYear(year)) {
days = 29;
} else {
days = 28;
}
break;
case 3:
monthName = "March";
days = 31;
break;
case 4:
monthName = "April";
days = 30;
break;
case 5:
monthName = "May";
days = 31;
break;
case 6:
5. monthName = "June";
days = 30;
break;
case 7:
monthName = "July";
days = 31;
break;
case 8:
monthName = "August";
days = 31;
break;
case 9:
monthName = "September";
days = 30;
break;
case 10:
monthName = "October";
days = 31;
break;
case 11:
monthName = "November";
days = 30;
break;
case 12:
monthName = "December";
days = 31;
break;
default:
System.out.print("Invalid Month.");
System.exit(0);
break;
}
System.out.println(" " + monthName + " " + year);
int i = 0;
int firstDay = 0;
6. switch (month) {
case 1:
firstDay = getFirstDay;
break;
case 2:
firstDay = getFirstDay + 3;
break;
case 3:
firstDay = getFirstDay + 3;
break;
case 4:
firstDay = getFirstDay + 6;
break;
case 5:
firstDay = getFirstDay + 8;
break;
case 6:
firstDay = getFirstDay + 11;
break;
case 7:
firstDay = getFirstDay + 13;
break;
case 8:
firstDay = getFirstDay + 16;
break;
case 9:
firstDay = getFirstDay + 19;
break;
case 10:
firstDay = getFirstDay + 21;
break;
case 11:
firstDay = getFirstDay + 24;
break;
case 12:
firstDay = getFirstDay + 26;
7. break;
}
if (isLeapYear(year)) {
switch (month) {
case 1:
firstDay = getFirstDay;
break;
case 2:
firstDay = getFirstDay + 3;
break;
case 3:
firstDay = getFirstDay + 4;
break;
case 4:
firstDay = getFirstDay + 7;
break;
case 5:
firstDay = getFirstDay + 9;
break;
case 6:
firstDay = getFirstDay + 12;
break;
case 7:
firstDay = getFirstDay + 14;
break;
case 8:
firstDay = getFirstDay + 17;
break;
case 9:
firstDay = getFirstDay + 20;
break;
case 10:
firstDay = getFirstDay + 22;
break;
case 11:
firstDay = getFirstDay + 25;
8. break;
case 12:
firstDay = getFirstDay + 27;
break;
}
}
printMonth(month, firstDay, days);
System.out.println();
}
}
//This method checks whether the year is leap year or not
private static boolean isLeapYear(int year) {
boolean bool = false;
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
bool = true;
else
bool = false;
return bool;
}
//This method will display the month
public static void printMonth(int month, int startDay, int numDays) {
int dayOfWeek = 0;
System.out.println("_______________________________");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
if ((startDay % 7) >= 0) {
if ((startDay % 7) == 0) {
dayOfWeek = 0;
} else if ((startDay % 7) == 1) {
dayOfWeek = 1;
System.out.print(" ");
} else if ((startDay % 7) == 2) {
dayOfWeek = 2;
System.out.print("t ");
} else if ((startDay % 7) == 3) {
dayOfWeek = 3;
System.out.print("tt ");
9. } else if ((startDay % 7) == 4) {
dayOfWeek = 4;
System.out.print("ttt");
} else if ((startDay % 7) == 5) {
dayOfWeek = 5;
System.out.print("ttt ");
} else if ((startDay % 7) == 6) {
dayOfWeek = 6;
System.out.print("tttt ");
}
}
for (int i = 1; i <= numDays; i++) {
if (i < 10)
System.out.print(" " + i);
else
System.out.print(" " + i);
if ((i + startDay) % 7 == 0)
System.out.println();
}
}
}
____________________________________
Output:
Enter the year: 2016
Enter 1st day of year ( 0 = Sunday, 6 = Satuday ): 5
January 2016
_______________________________
Sun Mon Tue Wed Thu Fri Sat
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
February 2016