Question

In: Computer Science

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

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;
}
}
}

Solutions

Expert Solution

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.


Related Solutions

Can someone please write clear and concise comments explaining what each line of code is doing...
Can someone please write clear and concise comments explaining what each line of code is doing for this program in C. I just need help tracing the program and understand what its doing. Thanks #include <stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/wait.h> int join(char *com1[], char *com2[]) {    int p[2], status;    switch (fork()) {        case -1:            perror("1st fork call in join");            exit(3);        case 0:            break;        default:...
Can someone please add clear and concise comments thoroughly explaining each line of code below. Just...
Can someone please add clear and concise comments thoroughly explaining each line of code below. Just need help understanding the code, don't need to modify it. The purpose of the code is to count the frequency of words in a text file, and return the most frequent word with its count. It uses two algorithms: Algorithm 1 is based on the data structure LinkedList. It maintains a list for word frequencies. The algorithm runs by scanning every token in the...
This is a python program. Put comments explaining the code, please. Suppose you have been tasked...
This is a python program. Put comments explaining the code, please. Suppose you have been tasked with writing a Python program (using linked lists) to keep track of computer equipment. For each piece of equipment, we track its name, purchase date, purchase amount, and quantity on hand. Write a program that completes the following tasks: allow the user to add a piece of equipment to the front of the list; allow the user to update the quantity of a piece...
1. Write code in mips that will play battleships. Include comments in code on what each...
1. Write code in mips that will play battleships. Include comments in code on what each part is doing.
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments string at the bottom displaying your output. Using sets (described in Deitel chapter 6) will simplify your work. Below is the starter template for the code: def overlap(user1, user2, interests): """ Return the number of interests that user1 and user2 have in common """ return 0    def most_similar(user, interests): """ Determine the name of user who is most similar to the input user...
write this program in java... don't forget to put comments. You are writing code for a...
write this program in java... don't forget to put comments. You are writing code for a calendar app, and need to determine the end time of a meeting. Write a method that takes a String for a time in H:MM or HH:MM format (the program must accept times that look like 9:21, 10:52, and 09:35) and prints the time 25 minutes later. For example, if the user enters 9:21 the method should output 9:46 and if the user enters 10:52...
Just that method --> Java code Write a method “int sumPos(List aList)” to calculate the sum...
Just that method --> Java code Write a method “int sumPos(List aList)” to calculate the sum of positive integers in an ADT List aList of integers using ADT List operations. ADT List operations: isEmpty(), size(), add(index, item), remove(index), get(index), and removeAll(). You should not assume how an ADT List is implemented. Using array indexing, head/tail references, or any operation not defined in ADT List is not allowed.
Programming. Write only code for the following. Do not write any comments. You can submit your...
Programming. Write only code for the following. Do not write any comments. You can submit your answer as .java file(s). #1. Design a Java JProduct class for a product which implements both cloneable and comparable interfaces The class should have the following private member variables: m_id: an integer that holds the product ID m_name: a string that holds the product name m_wholesaleprice: a double that holds the wholesale price m_retailers: a String array that holds all retailers who sell the...
Write a little scenario explaining a way that you could commit payroll/HR fraud. Explain what types...
Write a little scenario explaining a way that you could commit payroll/HR fraud. Explain what types of internal controls could prevent that fraud from occurring.
You are to write an essay explaining in detail what the role of the Federal Reserve...
You are to write an essay explaining in detail what the role of the Federal Reserve Bank is, including what their key policy tools are and how they are intended to impact the overall economy. Your essay should be approximately 1 page
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT