Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem java (updated version) Write a program to generate the entire calendar for one year. The program must get two values from the user: (1)

Problem java (updated version)

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 its 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

...

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.

Finally, add correct support for leap years think about how to use your isLeapYear method to correctly output February.

_____

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

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke

12th Edition International Edition

1292023422, 978-1292023427

More Books

Students also viewed these Databases questions

Question

WHAT is transferred and translated?

Answered: 1 week ago

Question

What is management growth? What are its factors

Answered: 1 week ago

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago