0% found this document useful (0 votes)
292 views

Logbook ATD

This document contains the code for a Logbook class that implements a logbook abstract data type (ADT). The Logbook class contains methods to store log entries for a given day, retrieve entries, get the month and year, determine the number of days in the month, and add two logbooks together if they are for the same month/year. It uses a GregorianCalendar object to work with dates and determine things like leap years and days of the week.

Uploaded by

Edwin Cáceres
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
292 views

Logbook ATD

This document contains the code for a Logbook class that implements a logbook abstract data type (ADT). The Logbook class contains methods to store log entries for a given day, retrieve entries, get the month and year, determine the number of days in the month, and add two logbooks together if they are for the same month/year. It uses a GregorianCalendar object to work with dates and determine things like leap years and days of the week.

Uploaded by

Edwin Cáceres
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

//-------------------------------------------------------------------//

// Laboratory 1
Logbook.java
//
// SOLUTION: Implementation of the Logbook ADT
//
//-------------------------------------------------------------------import java.io.*;
import java.util.*;

// For reading (keyboard) & writing (screen)


// For GregorianCalendar class

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

public void putEntry ( int value )


// Store entry for today's day.
{
// Create instance of today
GregorianCalendar today = new GregorianCalendar( );
// Requires that logMonth and logYear matches today's date.
// Remember that Java numbers months starting with January = 0
if ( logMonth == (today.get(Calendar.MONTH)+ 1) &&
logYear == today.get(Calendar.YEAR) )
// Store entry
entry[today.get(Calendar.DAY_OF_MONTH)- 1] = value;
else
System.out.println("Logbook is not set to today's month/year");
}
public void plus ( Logbook rightLogbook )
// Adds rightLogbook's entries to a logbook's entries.
{
int j; // Loop counter
// Requires that both logbooks are for the same month/year
if ( logMonth == rightLogbook.logMonth &&
logYear == rightLogbook.logYear )
for ( j = 0 ; j < daysInMonth() ; j++ )

entry[j] += rightLogbook.entry[j];
else
System.out.println("Can't add Logbooks for different month/years");
}
} // class Logbook

You might also like