Logbook ATD
Logbook ATD
// Laboratory 1
Logbook.java
//
// SOLUTION: Implementation of the Logbook ADT
//
//-------------------------------------------------------------------import java.io.*;
import java.util.*;
class Logbook
{
// Data members
private int logMonth,
logYear;
private int[]
entry = new int[31];
private GregorianCalendar logCalendar;
// Logbook month
// Logbook year
// Array of Logbook entries
// Java's built-in Calendar
// Constructors
public Logbook ()
// Default constructor. Creates a logbook for the current month.
// Uses the system clock to initialize the calendar.
// Note: Unlike mankind, Java's built-in Calendar numbers months
//
from January = 0
{
int j;
// Loop counter
logCalendar = new GregorianCalendar( );
logMonth = logCalendar.get(Calendar.MONTH) + 1;
logYear = logCalendar.get(Calendar.YEAR);
// Initialize all entries to 0
for ( j = 0 ; j < 31 ; j++ )
entry[j] = 0;
}
public Logbook ( int month, int year )
// Creates an empty logbook for the specified month and yea
// Note: Unlike mankind, Java's built-in Calendar numbers months from January =
0
{
int j; // Loop counter
if ( month < 1 || month > 12 )
{
Here
}
else
{
// Assumes a default DAY_OF_MONTH as first day of month
Here
}
// Initialize all entries to 0
for ( j = 0 ; j < 31 ; j++ )
entry[j] = 0;
}
// Mtodos
public void putEntry ( int day, int value )
// Graba la entrada del da en especfico
{
Here
else
System.out.println("Invalid Day of Month!");
}
public int getEntry ( int day )
// Retorna la entrada del da en especfico
{
// El da debe de ser mejor o igual
// al nmero de dias en el mes del logbook
Here
else
{
System.out.println("Invalid Day of Month!");
return -1;
}
}
public int month ( )
// Retorna el mes del logbook
{
return logMonth;
}
public int year ( )
// Retorna el ao del logbook
{
return logYear;
}
public int daysInMonth ( )
// Retorna el nmero de das en el logbook del mes
{
int result;
switch ( logMonth )
{
case 4 : case 6 : case 9 : case 11 :
result = 30;
break;
case 2 :
if ( leapYear() )
result = 29;
else
result = 28;
break;
default :
result = 31;
}
return result;
}
// Facilitator (helper) method
private boolean leapYear ( )
// If the logbook month occurs during a leap year, then returns true.
// Otherwise, returns false.
{
return ( logCalendar.isLeapYear(logYear) );
}
//-------------------------------------------------------------------//
//
In-lab operations
//
//-------------------------------------------------------------------private int dayOfWeek ( int day )
// Returns the day of the week corresponding to the specified day.
// Values returned range from 0 (Sunday) to 6 (Saturday).
// NOTE: Java's Calendar numbers DAY_OF_WEEK from Sunday = 1
//
and months from January = 0
{
// First set calendar to the specified day
logCalendar.set(logYear, logMonth -1, day);
return ( logCalendar.get(Calendar.DAY_OF_WEEK) -1 );
}
public void
// Displays a
//
//
//
Sun
//
//
7
// ...
{
Here
}
displayCalendar ()
logbook using the traditional calendar format.
Mon
1
8
Month / Year
Tue
Wed
2
3
9
10
Thu
4
11
Fri
5
12
Sat
6
13
entry[j] += rightLogbook.entry[j];
else
System.out.println("Can't add Logbooks for different month/years");
}
} // class Logbook