...

/

Debugging a Program in Development

Debugging a Program in Development

In this lesson, we will develop a program to display a calendar and add statements to trace the program's execution to help us find a coding error.

The calendar problem

Imagine a Java program that displays a complete calendar for any given year. We already saw how to find the day of the week for any given date and how to detect when a year is a leap year. In the previous lesson, we learned how to use a switch statement to get the number of days in a given month. Using these basic ideas, we could begin the definition of a class of calendars.

A first-draft solution

The program given below shows such a class still in development. The plan is to focus on computing the number of days in a month by writing and testing the private method getDaysInMonth. The class has temporary implementations, or stubs, for other methods in the class. A short main method in the class Driver tests the class for two different years, one of which is a leap year. Click the RUN button to see what happens.

Press + to interact
Calendar.java
Driver.java
/** Calendar.java by F. M. Carrano
A class to display a calendar for a given year.
*/
public class Calendar
{
private final int year;
public Calendar(int theYear)
{
year = theYear;
} // End constructor
/** Displays the 12 months in a year. */
public void display()
{
System.out.println("For the year " + year + ", ");
displayMonth(1);
displayMonth(2);
displayMonth(3);
displayMonth(4);
displayMonth(5);
displayMonth(6);
displayMonth(7);
displayMonth(8);
displayMonth(9);
displayMonth(10);
displayMonth(11);
displayMonth(12);
} // End display
// Displays the calendar for a given month.
private void displayMonth(int month) // STUB
{
int numberOfDays = getDaysInMonth(month);
System.out.println(" Month " + month + " has " + numberOfDays + " days.");
} // End displayMonth
// Returns the number of days in a given month for a specific year.
private int getDaysInMonth(int month)
{
int daysInMonth;
switch (month)
{
// Sept, Apr, June, Nov
case 9: case 4: case 6: case 11:
daysInMonth = 30;
break;
// Feb.
case 2:
if (isLeapYear())
daysInMonth = 29;
else
daysInMonth = 28;
// All other months
default:
daysInMonth = 31;
} // end switch
return daysInMonth;
} // End getDaysInMonth
// Returns true if the year is a leap year; otherwise, returns false.
private boolean isLeapYear()
{
// A year is a leap year if it is
// a) Divisible by 4 but not divisible by 100, or
// b) Divisible by 400.
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
} // End isLeapYear
// Other methods are here . . .
} // End Calendar

Observe that the output is correct, except for month 2. February does not have 31 days, even in a leap year. Let’s look at what we have. The class begins with one data field, year, and a constructor that initializes it. The method display, which should display a complete calendar, produces only basic output for now. It can have a less tedious definition after we study the repetition statements in the next chapter. This method calls the private method displayMonth, which is a stub that in turn calls the private method getDaysInMonth. The latter method is the one we want to develop. Since getDaysInMonth calls the private method isLeapYear, writing a stub for isLeapYear would normally make sense. However, we wrote and ...