Good morning, everyone! I am having an enormous difficulty creating this Java co
ID: 3572637 • Letter: G
Question
Good morning, everyone! I am having an enormous difficulty creating this Java code, would anyone mind helping me please? The assignment is as follows:
Write a Java 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.
January 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
February 1 2 3 4 5 6 7 8 9 10 11 12 …
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: 1. 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. 2. 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. 3. 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). 4. 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. 5. Finally, add correct support for leap years – think about how to use your isLeapYear method to correctly output February.
You have been supplied JUnit tests for the two methods, validating the inputs in main(), as well as the output for the entire calendar for the year 2015.
ALSO, the following format and prepared code is provided below:
package edu.wit.cs.comp1000;
public class PA7a {
/**
* Error to output if year is not positive
*/
static final String E_YEAR = "The year must be positive!";
/**
* Error to output if the day is not between 0 and 6
*/
static final String E_DAY = "The day of January 1st must be between 0 and 6!";
/**
* Determines if an input is a leap year
*
* @param year year in question
* @ereturn true if a leap year
*/
public static boolean isLeapYear(int year) {
return false; // TODO: replace with your code
}
/**
* Outputs a month to the console
*
* @param month title
* @param startDay 0=Sunday ... 6=Saturday
* @param numDays number of days in the month
* @return day of the week of the last day of the month
*/
public static int printMonth(String month, int startDay, int numDays) {
return 0; // TODO: replace with your code
}
/**
* Program execution point:
* input year, day of the week (0-6) of january 1
* output calendar for that year
*
* @param args command-line arguments (ignored)
*/
public static void main(String[] args) {
// TODO: write your code here
}
}
Thank you everyone so much in advance! This will help me out quite a bit!
Explanation / Answer
SOURCE CODE:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Calendar {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the year: ");
int year = Integer.parseInt(br.readLine());
System.out.print("Enter the first day of the year: ");
int day = Integer.parseInt(br.readLine());
for(int month=1; month<=12; month++)
printCalendar(year,day,month);
}
public static boolean isLeapYear(int year)
{
if((year%400==0) || ((year%100!=0)&&(year%4==0)))
return true;
return false;
}
public static void printCalendar(int year, int days,int month)
{
String months[] = {"","January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
int Day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year))
Day[2]=29;
int A[][] = new int[6][7];
int x = 1, z = days;
for(int i=0;i<6;i++)
{
for(int j=days; j<7; j++)
{
if(x<=Day[month])
{
A[i][j] = x;
x++;
}
}
days = 0;
}
System.arraycopy(A[5], 0, A[0], 0, z);
System.out.println(" ----------------------------------------------------");
System.out.println(" "+months[month]+" "+year);
System.out.println(" ----------------------------------------------------");
System.out.println(" SUN MON TUE WED THU FRI SAT");
System.out.println(" ----------------------------------------------------");
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 7; j++)
{
if(A[i][j]!=0)
System.out.print(" "+A[i][j]);
else
System.out.print(" ");
}
System.out.println(" ----------------------------------------------------");
}
System.out.println(" *****************************************************");
}
}
OUTPUT:
Enter the year: 2017
Enter the first day of the year: 0
----------------------------------------------------
January 2017
----------------------------------------------------
SUN MON TUE WED THU FRI SAT
----------------------------------------------------
1 2 3 4 5 6 7
----------------------------------------------------
8 9 10 11 12 13 14
----------------------------------------------------
15 16 17 18 19 20 21
----------------------------------------------------
22 23 24 25 26 27 28
----------------------------------------------------
29 30 31
----------------------------------------------------
*****************************************************
----------------------------------------------------
February 2017
----------------------------------------------------
SUN MON TUE WED THU FRI SAT
----------------------------------------------------
1 2 3 4 5 6 7
----------------------------------------------------
8 9 10 11 12 13 14
----------------------------------------------------
15 16 17 18 19 20 21
----------------------------------------------------
22 23 24 25 26 27 28
----------------------------------------------------
----------------------------------------------------
*****************************************************
----------------------------------------------------
March 2017
----------------------------------------------------
SUN MON TUE WED THU FRI SAT
----------------------------------------------------
1 2 3 4 5 6 7
----------------------------------------------------
8 9 10 11 12 13 14
----------------------------------------------------
15 16 17 18 19 20 21
----------------------------------------------------
22 23 24 25 26 27 28
----------------------------------------------------
29 30 31
----------------------------------------------------
*****************************************************
----------------------------------------------------
April 2017
----------------------------------------------------
SUN MON TUE WED THU FRI SAT
----------------------------------------------------
1 2 3 4 5 6 7
----------------------------------------------------
8 9 10 11 12 13 14
----------------------------------------------------
15 16 17 18 19 20 21
----------------------------------------------------
22 23 24 25 26 27 28
----------------------------------------------------
29 30
----------------------------------------------------
*****************************************************
----------------------------------------------------
May 2017
----------------------------------------------------
SUN MON TUE WED THU FRI SAT
----------------------------------------------------
1 2 3 4 5 6 7
----------------------------------------------------
8 9 10 11 12 13 14
----------------------------------------------------
15 16 17 18 19 20 21
----------------------------------------------------
22 23 24 25 26 27 28
----------------------------------------------------
29 30 31
----------------------------------------------------
*****************************************************
----------------------------------------------------
June 2017
----------------------------------------------------
SUN MON TUE WED THU FRI SAT
----------------------------------------------------
1 2 3 4 5 6 7
----------------------------------------------------
8 9 10 11 12 13 14
----------------------------------------------------
15 16 17 18 19 20 21
----------------------------------------------------
22 23 24 25 26 27 28
----------------------------------------------------
29 30
----------------------------------------------------
*****************************************************
----------------------------------------------------
July 2017
----------------------------------------------------
SUN MON TUE WED THU FRI SAT
----------------------------------------------------
1 2 3 4 5 6 7
----------------------------------------------------
8 9 10 11 12 13 14
----------------------------------------------------
15 16 17 18 19 20 21
----------------------------------------------------
22 23 24 25 26 27 28
----------------------------------------------------
29 30 31
----------------------------------------------------
*****************************************************
----------------------------------------------------
August 2017
----------------------------------------------------
SUN MON TUE WED THU FRI SAT
----------------------------------------------------
1 2 3 4 5 6 7
----------------------------------------------------
8 9 10 11 12 13 14
----------------------------------------------------
15 16 17 18 19 20 21
----------------------------------------------------
22 23 24 25 26 27 28
----------------------------------------------------
29 30 31
----------------------------------------------------
*****************************************************
----------------------------------------------------
September 2017
----------------------------------------------------
SUN MON TUE WED THU FRI SAT
----------------------------------------------------
1 2 3 4 5 6 7
----------------------------------------------------
8 9 10 11 12 13 14
----------------------------------------------------
15 16 17 18 19 20 21
----------------------------------------------------
22 23 24 25 26 27 28
----------------------------------------------------
29 30
----------------------------------------------------
*****************************************************
----------------------------------------------------
October 2017
----------------------------------------------------
SUN MON TUE WED THU FRI SAT
----------------------------------------------------
1 2 3 4 5 6 7
----------------------------------------------------
8 9 10 11 12 13 14
----------------------------------------------------
15 16 17 18 19 20 21
----------------------------------------------------
22 23 24 25 26 27 28
----------------------------------------------------
29 30 31
----------------------------------------------------
*****************************************************
----------------------------------------------------
November 2017
----------------------------------------------------
SUN MON TUE WED THU FRI SAT
----------------------------------------------------
1 2 3 4 5 6 7
----------------------------------------------------
8 9 10 11 12 13 14
----------------------------------------------------
15 16 17 18 19 20 21
----------------------------------------------------
22 23 24 25 26 27 28
----------------------------------------------------
29 30
----------------------------------------------------
*****************************************************
----------------------------------------------------
December 2017
----------------------------------------------------
SUN MON TUE WED THU FRI SAT
----------------------------------------------------
1 2 3 4 5 6 7
----------------------------------------------------
8 9 10 11 12 13 14
----------------------------------------------------
15 16 17 18 19 20 21
----------------------------------------------------
22 23 24 25 26 27 28
----------------------------------------------------
29 30 31
----------------------------------------------------
*****************************************************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.