This is a very complex program with many pieces, and there are many different wa
ID: 3757997 • Letter: T
Question
This is a very complex program with many pieces, and there are many different ways to approach it. Before writing any code, take some time to think through the problem. The most difficult piece is to understand how to write a single method that can print out the calendar for any month. Here is a suggestion for how to proceed:
Try to write the isLeapYear method – this is self-contained and there is a unit test to help you determine if you understand the logic.
Now focus on the printMonth method – there is another unit test to help you make sure you understand the logic here. Start with printing the name of the month (supplied as a parameter) and trailing new lines. Then try getting the correct number of days printed (also supplied as a parameter) without worrying about which day the month starts on. Then focus on getting the numbers to line up (hint: how are you going to deal with a single digit vs. double digits), then on starting a new line after each Saturday. Now make sure your return value is correct – it should be one day of the week later than the last printed day (with special handling if the last day is Saturday). Finally, print “blank” entries for each day until the first of the month. For example, if January 1st is a Wednesday, you need to print 3 blank entries.
Next, shift your focus to the main() method. Start by getting input from the user and validating them (there are unit tests focused on these, as well as error messages provided for you).
Now, assuming the inputs are supplied correctly, call your methods to print out each month (i.e. 12 calls to printMonth). Note that you will need to use the return value from this method to decide where to start each subsequent month.
In Java,
Write a program to generate the entire calendar for one year. The program must get two values from the user: (1) the year and (2) the day of the week for January 1st of that year. The year, which should be positive, is needed to check for and handle leap years1. The day of the week for January 1st is needed so that you know where to start the calendar. The user should enter 0 for Sunday, 1 for Monday, ... or 6 for Saturday. As always, you need to validate the user's input. To actually print the calendar, you must use a single method that prints out the calendar for one month and then call this function 12 times from main(), once for each month in the year. To check for a leap year you will need to write another method that takes the year as a parameter and returns true if it’s a leap year, or false otherwise. Stubs (i.e. method signatures without any code) for both of these methods have been provided for you.
The calendar should be printed in the form below. In this example, January starts on a Saturday (day 6). Note that February starts on a Tuesday in this example because January ended on a Monday.
A picture of the desired output has been posted.
Explanation / Answer
Answer:
/****JAVA PROGRAM TO PRINT CALENDAR FOR WHOLE YEAR****/
import java.io.*;
import java.util.*;
import java.lang.*;
/*** class printCalendar****/
public class printCalendar
{
//BOOLEAN METHOD isLeapYear RETURN TRUEIFENTERED YEAR ISLEAP YEAR OTHERWISE RETURN FALSE
public static boolean isLeapYear (int calyear)
{
//CHECKING THE CONDITION A YEAR TO BE LEAPYEAR
return ((calyear%4==0) && (calyear%100 !=0))||(calyear%400==0) ;
}
//PRINT THE CALENDARFOR THE GIVEN MONTH
//INPUTS ARE MONTH,START DAY OF THE MONTH, NUMBER OF DAYS IN THE MONTH
public static int printMonth (String presentMonth,int NUMDAYS, int wDay)
{
//PRINT THE MONTH
System.out.println( presentMonth );
int countDay = 1;
//PRINT THE SPACE
for(int k1 = 0; k1 < wDay; k1++)
{
System.out.print(" ");
}
//PRINT THE DAY
while (countDay <= NUMDAYS)
{
System.out.printf( "%2d", countDay);
if (wDay == 6)
{
System.out.println( " " );
wDay = 0;
}
else wDay = wDay + 1;
countDay = countDay + 1;
}
//RETURN THE END OF THE MONTH
return wDay;
}
//MAIN METHOD
public static void main(String[] args)
{
int calyear, startDAY;
int NUMDAYS;
boolean leapYear;
Scanner calScan=new Scanner(System.in);
//GET THE YEAR
System.out.println("ENTER THE YEAR FOR THE CALENDAR ");
calyear=calScan.nextInt();
//GET THE START OF THE YEAR
System.out.println("ENTER THE START DAY OF JANUARY: ");
startDAY=calScan.nextInt();
//CHECKING THE CONDITION
//CHECK YEAR IS NEGATIVE IF SO THEN EXIT
if (calyear<=0) {
System.out.println("YEAR SHOULDNOT BE NEGATIVE");
System.exit(0);
}
//CHECK THE START OF THE YEAR.
//IF IT IS NOT WITHIN 1 AND 6 THEN EXIT
if (startDAY<0 || startDAY>6) {
System.out.println("JANUARY 1ST MUT BE BETWEEN 1 AND 6 ");
System.exit(0);
}
//CHECK FOR LEAP YEAR
leapYear = isLeapYear(calyear);
//PRINT THE YEAR
System.out.println( calyear );
int febDays=28;
if(leapYear)
febDays=29;
//CALL printMonth 12 TIMES TOPRINT THE CALENDAR FOR THE WHOLE YEAR
startDAY=printMonth("JANUARY",31, startDAY);
startDAY=printMonth("FEBRAUARY", febDays, startDAY);
startDAY=printMonth("MARCH",31, startDAY);
startDAY=printMonth("APRIL",30, startDAY);
startDAY=printMonth("MAY",31, startDAY);
startDAY=printMonth("JUNE",30, startDAY);
startDAY=printMonth("JULY",31, startDAY);
startDAY=printMonth("AUGUST",31, startDAY);
startDAY=printMonth("SEPTEMBER",30, startDAY);
startDAY=printMonth("OCTOBER",31, startDAY);
startDAY=printMonth("NOVEMBER",30, startDAY);
startDAY=printMonth("DECEMBER",31, startDAY);
System.out.println( " ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.