[code = java] import java.util.Scanner; public class PrintCalenderShell { public
ID: 3568271 • Letter: #
Question
[code = java]
import java.util.Scanner;
public class PrintCalenderShell
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
//prompt the user to enter the year
boolean repeat = true;
while (repeat)//repeat as long as the user wants to
{
int year =0;
//loop as long as the user is entering an invalid year less than 1800
while(year <1800)
{
System.out.println("Enter a valid year after 1800");
//loop as long as the user is entering an input other than integer
while (!kb.hasNextInt())
{
System.out.println("Enter a valid year after 1800:");
kb.next();
}
year = kb.nextInt();
}
//printing the calender for each month of the year
for (int i =1; i<=12; i++)
{
//call the method printMonth with the parameters year and i
printMonth(year, i);
}
System.out.println("Do you have another year to print the calender:yes/no");
String answer = kb.next();
if (answer.equalsIgnoreCase("no"))
repeat = false;
}
}
//this method prints the body of the calender for the given month
public static void printMonth(int year, int month)
{
//call the method printTitle with the values year and month
printMonthTitle(year, month);
//call the method printMonthBody with the values year and month
printMonthBody(year, month);
}
//this method prints the title of the days in each week(sunday Mon Tues Wed Thur Fir Sat)
public static void printMonthTitle(int year, int month)
{
System.out.println(" " + getMonthName(month) + " " + year);
System.out.println("----------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
}
//this method outputs the calender for each month of the year
public static void printMonthBody(int year, int month)
{
//call the method getStartDay to get the start day for each month of the year
int startDay = getStartDay(year, month);
print(startDay, year, month);
//call the method print to print the calender for the month and pass the first day of the month as the parameter
}
public static void print(int startDay, int year, int month)
{
int start = getStartDay(year, month);
// call the method getNumberOfdaysInMonth to finde out the number of the days in the month
int numOfDaysInMonth = getNumberOfDaysInMonth(year, month);
for(int i=0; i<start; i++)
{
System.out.print(" ");
}
//use a for loop to print spaces up to the start day for each month
for(int i=1; i<=numOfDaysInMonth; i++)
{
System.out.printf("%4d", i);
//use another for loop to print the rest of the days in the calender
if((i+start)%7 ==0)
{
System.out.println();
}
}
System.out.println();
//if the month is November calculate the thanksgiving day and output }
}
//this method calculates the date for thanksgiving, you need to use
//a sitch statment
public static int thanks(int startDay)
{
//int thanks =(11-startDay)%7+22)
return 0;
}
public static String getMonthName(int month)
{
String monthName = "";
switch(month){
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "Aubuest"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December"; break;
}
return monthName;
}
//this method returns the start day of the month
public static int getStartDay(int year, int month)
{
//call the method getTotalNumberOfDays and store it in a variable called start
int start = getTotalNumberOfDays(year, month);
start= start+3;
//add 3 to the variable start
return start%7;
}
//returns the total number of the days up to the given month in the given year
public static int getTotalNumberOfDays(int year, int month)
{
int sum = 0;
//for loop goes from 1800 up to the year
for(int i=1800; i<=year; i++)
{
if(isLeapYear(i))
{
sum = sum+366;
}else{
sum = sum+365;
}
}
//add number of days in the year to sum, call the method leapyear
for(int i=1; i<month; i++)
{
sum = sum+getNumberOfDaysInMonth(year, i);
}
return sum;
}
//use this method from the last HW
public static int getNumberOfDaysInMonth(int year, int month)
{
int days = 0;
switch(month)
{
case 1: days = 30;
break;
case 2: if(isLeapYear(year)){
days = 29;
}else{
days = 28;
}break;
case 3: days = 31;
break;
case 4: days = 30;
break;
case 5: days = 31;
break;
case 6: days = 30;
break;
case 7: days = 31;
break;
case 8: days = 31;
break;
case 9: days = 30;
break;
case 10: days = 31;
break;
case 11: days = 30;
break;
case 12: days = 31;
break;
}
return days;
}
public static boolean isLeapYear(int year)
{
return(year %400==0 || year%4==0 && year%100!=0);
}
}
[/code]
Explanation / Answer
// PrintCalendar.java: Print a calendar for a given month in a year
import java.util.Scanner;
public class PrintCalendar {
/** Main method */
public static void main(String[] args) {
// Prompt the user to enter year
Scanner reader = new Scanner(System.in);
System.out.println("Enter the Year number");
//get user input for a
int year=reader.nextInt();
for(int i=1; i<13;i++)
{
printMonth(year, i);
}
System.exit(0);
}
/** Print the calendar for a month in a year */
static void printMonth(int year, int month) {
// Get start day of the week for the first date in the month
int startDay = getStartDay(year, month);
// Get number of days in the month
int numOfDaysInMonth = getNumOfDaysInMonth(year, month);
// Print headings
printMonthTitle(year, month);
// Print body
printMonthBody(startDay, numOfDaysInMonth);
}
/** Get the start day of the first day in a month */
static int getStartDay(int year, int month) {
// Get total number of days since 1/1/1800
int startDay1800 = 3;
long totalNumOfDays = getTotalNumOfDays(year, month);
// Return the start day
return (int)((totalNumOfDays + startDay1800) % 7);
}
/** Get the total number of days since Jan 1, 1800 */
static long getTotalNumOfDays(int year, int month) {
long total = 0;
// Get the total days from 1800 to year -1
for (int i = 1800; i < year; i++)
if (isLeapYear(i))
total = total + 366;
else
total = total + 365;
// Add days from Jan to the month prior to the calendar month
for (int i = 1; i < month; i++)
total = total + getNumOfDaysInMonth(year, i);
return total;
}
/** Get the number of days in a month */
static int getNumOfDaysInMonth(int year, int month) {
if (month == 1 || month==3 || month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12)
return 31;
if (month == 4 || month == 6 || month == 9 || month == 11)
return 30;
if (month == 2)
if (isLeapYear(year))
return 29;
else
return 28;
return 0; // If month is incorrect.
}
/** Determine if it is a leap year */
static boolean isLeapYear(int year) {
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
return true;
return false;
}
/** Print month body */
static void printMonthBody(int startDay, int numOfDaysInMonth) {
// Pad space before the first day of the month
int i = 0;
for (i = 0; i < startDay; i++)
System.out.print(" ");
for (i = 1; i <= numOfDaysInMonth; i++) {
if (i < 10)
System.out.print(" " + i);
else
System.out.print(" " + i);
if ((i + startDay) % 7 == 0)
System.out.println();
}
System.out.println();
}
/** Print the month title, i.e. May, 1999 */
static void printMonthTitle(int year, int month) {
System.out.println(" " + getMonthName(month)
+ ", " + year);
System.out.println("-----------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
}
/** Get the English name for the month */
static String getMonthName(int month) {
String monthName = null;
switch (month) {
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "August"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December";
}
return monthName;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.