In: Computer Science
Basically, the code already functions properly. Could you just write in method header comments explaining what the method does and what the parameters are? Some of them are already done. Additionally could you change the variables and method names to make them more descriptive of what they're actually holding? Thank you!
import java.util.Scanner;
/**
* This class contains the entire program to print out a yearly
calendar.
*
* @author
* @author TODO add your name here when you contribute
*/
public class Calendar {
public static void tc(char c3, int c4) {
for (int pie = 0; pie < c4; pie++) {
System.out.print(c3);
}
}
public static boolean yr(int yr2) {
/* TODO this really should be in a method header JavaDoc comment
rather than hidden in the method.
Every year that is exactly divisible by four is a leap year, except
for years that are exactly divisible
by 100, but these centurial years are leap years if they are
exactly divisible by 400. For example,
the years 1700, 1800, and 1900 are not leap years, but the years
1600 and 2000 are.
https://en.wikipedia.org/wiki/Leap_year
*/
boolean yri = false;
if (yr2 % 4 == 0) {
if (yr2 % 100 == 0) {
if (yr2 % 400 == 0) {
yri = true;
} else {
yri = false;
}
} else {
yri = true;
}
} else {
yri = false;
}
return yri;
}
/**
* This returns the number of days in the specified month of
year.
*
* @param month The month to return the number of days.
* @param year The year is used for determining whether it is a leap
year.
* @return The number of days in the specified month of the
year.
*/
public static int getDaysInMonth(int month, int year) {
int daysInMonth = 0;
switch (month) {
//31 days
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
daysInMonth = 31;
break;
//30 days
case 4:
case 6:
case 9:
case 11:
daysInMonth = 30;
break;
case 2: //28 or 29 days
if ( yr(year)) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
break;
}
return daysInMonth;
}
/**
* Returns the name of the month, given the number of the
month.
*
* @param month The month where 1 is January and 12 is
December.
* @return The name of the month.
*/
public static String getMonthName(int month) {
String monthStr;
switch (month) {
case 1:
monthStr = "January";
break;
case 2:
monthStr = "February";
break;
case 3:
monthStr = "March";
break;
case 4:
monthStr = "April";
break;
case 5:
monthStr = "May";
break;
case 6:
monthStr = "June";
break;
case 7:
monthStr = "July";
break;
case 8:
monthStr = "August";
break;
case 9:
monthStr = "September";
break;
case 10:
monthStr = "October";
break;
case 11:
monthStr = "November";
break;
case 12:
monthStr = "December";
break;
default:
monthStr = "UNKNOWN";
break;
}
return monthStr;
}
public static void p(String n, int h) {
final int TOTAL_WIDTH = 28;
final char MONTH_HEADER_LINE_CHAR = '-';
System.out.println();
String it = n + " " + h;
int spacesBefore = (TOTAL_WIDTH - it.length()) / 2;
tc(' ', spacesBefore);
System.out.println(it);
tc(MONTH_HEADER_LINE_CHAR, TOTAL_WIDTH);
System.out.println();
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
}
public static void d2(int da, int md) {
final char CHAR_BETWEEN_DAYS = ' ';
final int DAYS_IN_A_WEEK = 7;
final int LOWEST_SINGLE_DIGIT_DAY = 1;
final int HIGHEST_SINGLE_DIGIT_DAY = 9;
tc( CHAR_BETWEEN_DAYS, da * 4);
for ( int zzzz = 1; zzzz <= md; zzzz++) {
if ( zzzz >= LOWEST_SINGLE_DIGIT_DAY && zzzz <=
HIGHEST_SINGLE_DIGIT_DAY) {
tc(CHAR_BETWEEN_DAYS, 2);
} else {
tc( CHAR_BETWEEN_DAYS, 1);
}
System.out.print( zzzz);
tc( CHAR_BETWEEN_DAYS, 1);
da++;
if ( da % DAYS_IN_A_WEEK == 0) {
System.out.println();
}
}
System.out.println();
}
/**
* This prompts for the year and the day of the week of January 1st
and then
* prints out a calendar for the entire year.
*
* @param args unused
*/
public static void main(String[] args) {
final char FIRST_MONTH = 1;
final char LAST_MONTH = 12;
final int DAYS_IN_A_WEEK = 7;
Scanner input = new Scanner(System.in);
System.out.print("Enter year:");
int year = input.nextInt();
System.out.print("Enter day of week of Jan 1 (0-Sunday, 1-Monday,
etc):");
int startDay = input.nextInt();
for ( int month = FIRST_MONTH; month <= LAST_MONTH; ++month)
{
String monthName = getMonthName( month);
p( monthName, year);
int daysInMonth = getDaysInMonth(month, year);
d2(startDay, daysInMonth);
startDay = (startDay + daysInMonth) % DAYS_IN_A_WEEK;
}
}
}
I have renamed most of the variable and method names of the given program and provided comments for the program wherever required to understand . The updated code is below:
import java.util.Scanner;
/**
* This class contains the entire program to print out a yearly calendar.
*
* @author
* @author TODO add your name here when you contribute
*/
public class Calendar {
//printCharacters() is used is print required character(space or -) for given number of times.
/**
* printCharacters() is used is print required character(space or -) for given number of times.
*
* @param character is the character to be printed
* @param numberOftimes determines number of times the given character is printed
*/
public static void printCharacters(char character, int numberOftimes) {
for (int i = 0; i < numberOftimes; i++) {
System.out.print(character);
}
}
/**
* checks whether leap year or not
*
* @param year The year is used for determining whether it is a leap year.
* @return true if it is leap year otherwise false.
*/
public static boolean isLeapYear(int year) {
/* TODO this really should be in a method header JavaDoc comment rather than hidden in the method.
Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible
by 100, but these centurial years are leap years if they are exactly divisible by 400. For example,
the years 1700, 1800, and 1900 are not leap years, but the years 1600 and 2000 are.
*/
boolean isLeap = false;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
isLeap = true;
} else {
isLeap = false;
}
} else {
isLeap = true;
}
} else {
isLeap = false;
}
return isLeap;
}
/**
* This returns the number of days in the specified month of year.
*
* @param month The month to return the number of days.
* @param year The year is used for determining whether it is a leap year.
* @return The number of days in the specified month of the year.
*/
public static int getNoOfDaysInMonth(int month, int year) {
int daysInMonth = 0;
switch (month) {
//31 days - Months with 31 days
case 1: //Jan
case 3: //Mar
case 5: //May
case 7: //July
case 8: //Aug
case 10: //Oct
case 12: //Dec
daysInMonth = 31;
break;
//30 days - Months with 30 days
case 4: //April
case 6: //June
case 9: //September
case 11: //November
daysInMonth = 30;
break;
case 2: //28 or 29 days for February month
if ( isLeapYear(year)) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
break;
}
return daysInMonth;
}
/**
* Returns the name of the month, given the number of the month.
*
* @param month The month where 1 is January and 12 is December.
* @return The name of the month.
*/
public static String getMonthName(int month) {
String monthStr;
switch (month) {
case 1:
monthStr = "January";
break;
case 2:
monthStr = "February";
break;
case 3:
monthStr = "March";
break;
case 4:
monthStr = "April";
break;
case 5:
monthStr = "May";
break;
case 6:
monthStr = "June";
break;
case 7:
monthStr = "July";
break;
case 8:
monthStr = "August";
break;
case 9:
monthStr = "September";
break;
case 10:
monthStr = "October";
break;
case 11:
monthStr = "November";
break;
case 12:
monthStr = "December";
break;
default:
monthStr = "UNKNOWN";
break;
}
return monthStr;
}
/**
* This method is used to print the below pattern for calendar
* January 2021
----------------------------
Sun Mon Tue Wed Thu Fri Sat
* @param monthName -- input the month name to be printed
* @param year -- input the year of the calendar to be printed
*/
public static void printMonthHeading(String monthName, int year) {
final int TOTAL_WIDTH = 28; // total width is 28 i.e 3 chars for week name(like Sun, Mon etc) and one space i.e total 4 chars-->4*7=28
final char MONTH_HEADER_LINE_CHAR = '-'; //character used to print line under month-year
System.out.println();
String monthHeading = monthName + " " + year;
int spacesBefore = (TOTAL_WIDTH - monthHeading.length()) / 2; //num of spaces to be printed before month name
printCharacters(' ', spacesBefore); // calling printCharacters() to print spaces
System.out.println(monthHeading); //printing month name i.e January 2021
printCharacters(MONTH_HEADER_LINE_CHAR, TOTAL_WIDTH); // calling printCharacters() to print line(-----) under month heading
System.out.println();
System.out.println("Sun Mon Tue Wed Thu Fri Sat"); //print week names for every month calendar
}
public static void printDaysInMonth(int day, int noOfDaysInMonth) {
final char CHAR_BETWEEN_DAYS = ' '; //space is placed between days
final int DAYS_IN_A_WEEK = 7;
final int LOWEST_SINGLE_DIGIT_DAY = 1;
final int HIGHEST_SINGLE_DIGIT_DAY = 9;
printCharacters( CHAR_BETWEEN_DAYS, day * 4); //4 chars for each day( i.e "Sun " ==" 3")
for ( int i = 1; i <= noOfDaysInMonth; i++) {
if ( i >= LOWEST_SINGLE_DIGIT_DAY && i <= HIGHEST_SINGLE_DIGIT_DAY) {
printCharacters(CHAR_BETWEEN_DAYS, 2); //2 spaces has to be left for single digit
} else {
printCharacters( CHAR_BETWEEN_DAYS, 1); //1 space has to be left for double digit
}
System.out.print( i); // print day
printCharacters( CHAR_BETWEEN_DAYS, 1); //1 space has to be left between days
day++;
if ( day % DAYS_IN_A_WEEK == 0) {
System.out.println();
}
}
System.out.println();
}
/**
* This prompts for the year and the day of the week of January 1st and then
* prints out a calendar for the entire year.
*
* @param args unused
*/
public static void main(String[] args) {
final char FIRST_MONTH = 1;
final char LAST_MONTH = 12;
final int DAYS_IN_A_WEEK = 7;
Scanner input = new Scanner(System.in);
System.out.print("Enter year:");
int year = input.nextInt();
System.out.print("Enter day of week of Jan 1 (0-Sunday, 1-Monday, etc):");
int startDay = input.nextInt();
for ( int month = FIRST_MONTH; month <= LAST_MONTH; ++month) {
String monthName = getMonthName( month); //fetching month name
printMonthHeading( monthName, year); //printing method heading
int daysInMonth = getNoOfDaysInMonth(month, year); //fetching num of days in a month
printDaysInMonth(startDay, daysInMonth); //printing days in a month
startDay = (startDay + daysInMonth) % DAYS_IN_A_WEEK; //Resetting day to 0 for every 7 days
}
}
}
The screenshot of the output after updating the code is as below:
If you still have any queries, please reach out through the comment section.