The problem we are going to solve

Programming exercise 5.29 asks you to write a program that can print a calendar for all twelve months of any year that the user enters.

Running this program should produce something like the following:

What year do you want to display?  2018

    January 2018
 Su Mo Tu We Th Fr Sa
     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 2018
 Su Mo Tu We Th Fr Sa
              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 2018
 Su Mo Tu We Th Fr Sa
              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 2018
 Su Mo Tu We Th Fr Sa
  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 2018
 Su Mo Tu We Th Fr Sa
        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 2018
 Su Mo Tu We Th Fr Sa
                 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 2018
 Su Mo Tu We Th Fr Sa
  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 2018
 Su Mo Tu We Th Fr Sa
           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 2018
 Su Mo Tu We Th Fr Sa
                    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 2018
 Su Mo Tu We Th Fr Sa
     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 2018
 Su Mo Tu We Th Fr Sa
              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 2018
 Su Mo Tu We Th Fr Sa
                    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

This is a very challenging problem! Very few programmers who try to solve this problem are capable of conceptualizing and implementing a solution to this problem that is correct on the first try. Instead, most programmers will approach this problem in stages, starting from a simple solution that is clearly incorrect, and working through a series of versions that get closer and closer to the desired final product. This lab exercise will take you through that process with considerable guidance. Below we are going to work through several versions of our program, until we arrive at a version that does everything that we want. Along the way you will gain some experience in working with loops and if-else statements.

Version One

For our first version we are going to start on the goal of printing a calendar for a single month.

Create a new project and place the following code in the main method of your program.

int cell;

for(cell = 1;cell <= 42;cell++) {
    System.out.print(cell);
}

Run this program and make note of the output it produces. You will notice right away that a couple of obvious things are wrong here.

The first issue is easy to address. Modify your code so that the program prints the numbers from 1 to 42 with spaces between the numbers.

Version Two

Next, we want to address the problem of the numbers all appearing on the same line. The obvious fix for this problem is to print line breaks at the appropriate spots. To print a line break in Java, we can use the statement

System.out.println();

This prints nothing, followed by a line break.

The trick now is to get the line breaks to appear in the right places. One way to do this is to print a line break after each number that is divisible by 7.

Also, add a print statement before the start of the loop that prints a heading for the calendar showing the days of the week.

Running your program now should produce something that looks like this:

 Su Mo Tu We Th Fr Sa
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 32 33 34 35
36 37 38 39 40 41 42

Version Three

The next issue to address is the fact that most months do not start on Sunday.

For our next version, add some code that prompts the user to enter a number for which day of the week the month should start on, where Sunday = 1 and Saturday = 7.

Once you know what day of the week the 1st of the month should fall on, you can combine that information with the loop counter variable cell to generate a correct day number for each cell in the calendar grid.

Running your program now should produce something that looks like this:

What day of the week does the month start on? (1 = Sunday, 7 = Saturday)
6

 Su Mo Tu We Th Fr Sa
-4 -3 -2 -1 0 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 32 33 34 35 36 37

Important Note

Your loop will now be working with two different variables. The first variable, cell, is the loop counter variable. The second variable, date, is the number you get by combining cell with the day of the week the 1st lands on. In your print statement you will now be printing date in place of cell.

Version Four

The next issue to address is making sure that we don't print the negative numbers and also that we print the correct number of spaces before each number to get the numbers to line up nicely with the column headings.

In other words, we want to a program that produces something that looks a little more like this:

What day of the week does the month start on? (1 = Sunday, 7 = Saturday)
6

Su Mo Tu We Th Fr Sa
                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 32 33 34 35 36 37

To achieve this, find the statement that prints the numbers and replace it with a set of if-else statements that do the following:

Version Five

The next version is going to include code that can automatically determine what day of the week the month starts on and how many days are in the month. We are going to accomplish this by taking advantage of a Java class that includes a lot of useful methods for working with dates. This Java class is part of the Java class library, which is a large collection of classes that can perform useful tasks.

We have already seen one example of a useful class, the Scanner class. To use this class, we typically import the class with an import statement, create an object belonging to that class, and then call various methods on that object to get useful work done:

// This imports the Scanner class
import java.util.Scanner;

// This creates a Scanner object, input.
Scanner input = new Scanner(System.in);

// This calls the nextInt method on the input object
// to get useful work done.
int x = input.nextInt();

To work with time and date information we are going to use the Calendar class. Here are some things you will need to know to use this class.

Using the Calendar class as described above, modify your program to prompt the user to enter a month and year number and then print a calendar for that month.

When your program runs its should look something like this:

What month and year do you want to display?  9 2012

    September 2012
 Su Mo Tu We Th Fr Sa
                    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

Final Version

Now that we have a program that can print a calendar for any month the user enters, we can finish our program. Replace the code that prompts the user to enter a month and a year with code that instead just prompts them to enter a year. Place a loop around your calendar-making logic that cycles through all of the months in that year, from month 1 to month 12, printing a calendar for each month in the year that the user specified.

Turn in your work

When you finish this program, place your project folder in an archive and email it to me for grading. If you don't finish this exercise in lab, you can turn it in by 5 PM on the day after your lab day without a late penalty.