Sie sind auf Seite 1von 3

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

//
// Laboratory 1 Logbook.java
//
// Definitions for the Logbook ADT
//
//
//
//--------------------------------------------------------------------

import java.io.*; // For reading (keyboard) & writing (screen)


import java.util.*; // For GregorianCalendar class

class Logbook
{
// Data members
private int logMonth, // Logbook month
logYear; // Logbook year
private int[]
entry = new int[31]; // Array of Logbook entries
private GregorianCalendar logCalendar; // Java's built-in Calendar

// Constructor
public Logbook ( int month, int year )
// Creates an empty logbook for the specified month and year.
{
int j;
if(!((month-1)>=0&&(month-1)<=11))
logCalendar=new GregorianCalendar(); // Today's Date
else
{
// Assumes a default DAY_OF_MONTH as first day of month
logCalendar=new GregorianCalendar(year,month-1,1);
logMonth=month;
logYear=year;
}
// Set each entry to 0
for(j=0;j<=30;j++)
entry[j]=0;

// Methods
public void putEntry ( int day, int value )
// Stores entry for the specified day.
{
if(day>0 && day<=daysInMonth())
entry[day-1]=value;

public int getEntry ( int day )


// Returns entry for the specified day.
{
if(day>0 && day<=daysInMonth())
return entry[day-1];
else
return -1;
}

public int month ( )


// Returns the logbook month.
{
return logMonth;

public int year ( )


// Returns the logbook year.
{
return logYear;

public int daysInMonth ( )


// Returns the number of days in the logbook month.
{
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
if((logMonth-1)==1)
if(leapYear())
return 29;
else
return 28;
else
return a[logMonth-1];

// Facilitator (helper) method


private boolean leapYear ( )
// If the logbook month occurs during a leap year, then returns true.
// Otherwise, returns false.
{
boolean b=logCalendar.isLeapYear(logYear);
return b;
}

//--------------------------------------------------------------------
//
// In-lab operations
//
//--------------------------------------------------------------------

private int dayOfWeek ( int day )


// Returns the day of the week corresponding to the specified day.
{
logCalendar.set(logYear,logMonth-1,day);
int t=logCalendar.get(Calendar.DAY_OF_WEEK); // Returns the days numbered
from 1 to 7
return (t-1);

public void displayCalendar ()


// Displays a logbook using the traditional calendar format.
{
int dnb=dayOfWeek(1);
System.out.println(" "+logMonth+" / "+logYear);

System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\
t"+"Sat");
//System.out.print(" ");
for(int j=0;j<dnb;j++) System.out.print(" \t");

for(int i=1;i<=daysInMonth();i++)
{
System.out.print(i+" "+getEntry(i)+"\t");
if(dayOfWeek(i+1)==0) System.out.println();

public void putEntry ( int value )


// Store entry for today.
{
if(logYear==logCalendar.get(Calendar.YEAR) &&
logMonth==logCalendar.get(Calendar.MONTH))
{
int x=logCalendar.get(Calendar.DAY_OF_WEEK)-1;
entry[x]=value;
}

public Logbook ( )
// Default constructor. Creates a logbook for the current month/year.
{
logCalendar=new GregorianCalendar(); //Today's Date
logYear=logCalendar.get(Calendar.YEAR);
logMonth=logCalendar.get(Calendar.MONTH);

/* public void plus ( Logbook rightLogbook )


// Combine logbooks.
{

}*/

} // class Logbook

Das könnte Ihnen auch gefallen