Basically, the code already functions properly. Could you just write in method header comments explaining what...

80.2K

Verified Solution

Question

Programming

Basically, the code already functions properly. Could you justwrite in method header comments explaining what the method does andwhat the parameters are? Some of them are already done.Additionally could you change the variables and method names tomake them more descriptive of what they're actually holding? Thankyou!


import java.util.Scanner;

/**
* This class contains the entire program to print out a yearlycalendar.
*
* @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 commentrather than hidden in the method.
Every year that is exactly divisible by four is a leap year, exceptfor years that are exactly divisible
by 100, but these centurial years are leap years if they areexactly divisible by 400. For example,
the years 1700, 1800, and 1900 are not leap years, but the years1600 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 ofyear.
*
* @param month The month to return the number of days.
* @param year The year is used for determining whether it is a leapyear.
* @return The number of days in the specified month of theyear.
*/
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 themonth.
*
* @param month The month where 1 is January and 12 isDecember.
* @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 1stand 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;
}
}
}

Answer & Explanation Solved by verified expert
4.2 Ratings (637 Votes)
I have renamed most of the variable and method names of thegiven program and provided comments for the program whereverrequired to understand The updated code is belowimport javautilScanner This class contains the entire program to print out a yearly calendar author author TODO add your name here when you contributepublic class Calendar printCharacters is used is print required characterspace or for given number of times printCharacters is used is print required characterspace 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 printCharacterschar character int numberOftimes for int i 0 i numberOftimes i Systemoutprintcharacter checks whether leap year or not param year The    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students