Hello, can someone please help me refactor this without going past 100 lines.     Here is the code.   import java.util.Scanner; /** * This class contains the entire program to print out a yearly calendar. * * @author Jim Williams * @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; } } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

Hello, can someone please help me refactor this without going past 100 lines.  

 

Here is the code.

 

import java.util.Scanner;

/**
* This class contains the entire program to print out a yearly calendar.
*
* @author Jim Williams
* @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;
}
}
}

6.15 *zyLab: Calendar (Required & Human Graded)
This code works but there are some problems that need to be corrected. Your task is to complete it to course style and documentation standards as described in the CS 200 Style Guide. This project will be human graded.
This program takes as input the year and the day of the week of January 1st and generates a calendar for the whole year.
Getting Started
Set up a Calendar project in your IDE (Eclipse or IntelliJ)
• Download Calendar.java, put it into your project and make sure it compiles and runs.
Note: The provided code works. The goal is to refactor, improving the code while keeping it working.
Add a file header comment.
Turn in the code to zyBooks to make sure the tests pass.
Style and Format each of the methods
Format the method code to follow course standards. The IDES can be helpful, but review to make sure the standards are followed.
In Eclipse, select code, go to the Source menu, select Format, and then select Correct Indentation (Alternatively: Ctrl + Shift + F)
• In IntelliJ, select code (in editor, Ctrl + A), go to Code menu, select Reformat Code (alternatively: Ctrl + Alt + L)
• Verify your program is still working.
• Turn into zyBooks to verify your program is still working. Turning in also saves snapshots of your code to return to at a later time if necessary. If at any time your program is not working, then back up to a
point it was working and try again.
Refactor the identifiers (names)
Refactor the method names, parameters and local variables to be meaningful and follow course standards
• Meaningful names easily guide someone that is not familiar with the code to understand what the method does or what the variable contains. Variables are typically nouns or noun phrases while method
names are typically verbs or verb phrases. Avoid abbreviations unless very commonly used.
• In Eclipse or IntelliJ, right click on a variable name, select Refactor, select Rename. Type the new name and the name will be changed everywhere. (Alternatively in Eclipse Alt + Shift +R, IntelliJ Shift+F6)
Trace the code using any example inputs to figure out what is happening
• Trace by hand, use print statements or use a debugger. If you add print statements to help you debug, remember to remove them later.
Verify your program is still working.
• Turn into zyBooks periodically to verify your program is still working and also provide snapshots of your code to return to at a later time if necessary.
Add Class and Method Header Comments
Write a JavaDoc comment for the class and every method following the course standards. Method comments should include a summary description, an algorithm for non-trivial methods possibly including a
citation, and @param and @return tags (if there is a return value) with a description. We expect you to use your own words.
• Verify your program is still working.
• Turn into zyBooks periodically to verify your program is still working and also provide snapshots of your code to return to at a later time if necessary.
Notes
• Some methods are given to you (getDaysinMonth, getMonthName, main). The only changes that would need to be made to these methods are if they call other methods whose names have been changed.
Final Submission
• Make sure your code follows all the standards in the Grading Rubric and expectations described in the CS 200 Style Guide. Make sure all TODO items are complete and TODO is removed. Turn in your final version to
zyBooks. The latest version that compiles and runs prior to the deadline Thursday, October 15th, at 11.59 pm (no 10% penalty after 6 pm) will be the version that is human graded.
Transcribed Image Text:6.15 *zyLab: Calendar (Required & Human Graded) This code works but there are some problems that need to be corrected. Your task is to complete it to course style and documentation standards as described in the CS 200 Style Guide. This project will be human graded. This program takes as input the year and the day of the week of January 1st and generates a calendar for the whole year. Getting Started Set up a Calendar project in your IDE (Eclipse or IntelliJ) • Download Calendar.java, put it into your project and make sure it compiles and runs. Note: The provided code works. The goal is to refactor, improving the code while keeping it working. Add a file header comment. Turn in the code to zyBooks to make sure the tests pass. Style and Format each of the methods Format the method code to follow course standards. The IDES can be helpful, but review to make sure the standards are followed. In Eclipse, select code, go to the Source menu, select Format, and then select Correct Indentation (Alternatively: Ctrl + Shift + F) • In IntelliJ, select code (in editor, Ctrl + A), go to Code menu, select Reformat Code (alternatively: Ctrl + Alt + L) • Verify your program is still working. • Turn into zyBooks to verify your program is still working. Turning in also saves snapshots of your code to return to at a later time if necessary. If at any time your program is not working, then back up to a point it was working and try again. Refactor the identifiers (names) Refactor the method names, parameters and local variables to be meaningful and follow course standards • Meaningful names easily guide someone that is not familiar with the code to understand what the method does or what the variable contains. Variables are typically nouns or noun phrases while method names are typically verbs or verb phrases. Avoid abbreviations unless very commonly used. • In Eclipse or IntelliJ, right click on a variable name, select Refactor, select Rename. Type the new name and the name will be changed everywhere. (Alternatively in Eclipse Alt + Shift +R, IntelliJ Shift+F6) Trace the code using any example inputs to figure out what is happening • Trace by hand, use print statements or use a debugger. If you add print statements to help you debug, remember to remove them later. Verify your program is still working. • Turn into zyBooks periodically to verify your program is still working and also provide snapshots of your code to return to at a later time if necessary. Add Class and Method Header Comments Write a JavaDoc comment for the class and every method following the course standards. Method comments should include a summary description, an algorithm for non-trivial methods possibly including a citation, and @param and @return tags (if there is a return value) with a description. We expect you to use your own words. • Verify your program is still working. • Turn into zyBooks periodically to verify your program is still working and also provide snapshots of your code to return to at a later time if necessary. Notes • Some methods are given to you (getDaysinMonth, getMonthName, main). The only changes that would need to be made to these methods are if they call other methods whose names have been changed. Final Submission • Make sure your code follows all the standards in the Grading Rubric and expectations described in the CS 200 Style Guide. Make sure all TODO items are complete and TODO is removed. Turn in your final version to zyBooks. The latest version that compiles and runs prior to the deadline Thursday, October 15th, at 11.59 pm (no 10% penalty after 6 pm) will be the version that is human graded.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY