Laboratory 8 Solutions
Lesson 8-2
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Exercise 6
Lesson 8-3
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Lesson 8-4
Exercise 1
Exercise 2
Lesson 8-5
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Lesson 8-6
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Lesson 8-7
Exercise 1
Lesson 8-2
Exercise 1:
The following is an example of the Date class.
public class Date
{
// Data fields
private int day;
private int year;
private int month;
// Constructors
public Date(int newDay, int newMonth, int newYear)
{
day = newDay;
month = newMonth;
year = newYear;
}
public Date()
{
day = 0;
month = 0;
year = 0;
}
// Methods
public int getDay()
{
return day;
}
public int getMonth()
{
return month;
}
public int getYear()
{
return year;
}
public String toString()
{
return month + "/" + day + "/" + year;
}
}
Exercise 2:
See the default Date constructor in Exercise 1.
Exercise 3:
The following is a sample test driver for the Date class.
public class TestDate // A driver class using Date
{
public static void main(String[] args) {
// create a date
Date date1 = new Date( 6, 12, 1944 );
// test getter methods
System.out.print( "Date 1 - " );
System.out.print( "Day: " + date1.getDay() + " " );
System.out.print( "Month: " + date1.getMonth() + " " );
System.out.println( "Year: " + date1.getYear() );
//test toString method
System.out.println( "Date 1 - " + date1.toString());
}
}
Exercise 4:
The following is the three setter methods for the Date class.
public void setDay( int newDay )
{
day = newDay;
}
public void setMonth( int newMonth )
{
month = newMonth;
}
public void setYear( int newYear )
{
year = newYear;
}
Exercise 5:
The following is the updated test driver to test the additional methods.
public class TestDate // A driver class using Date
{
public static void main(String[] args) {
// Added for Exercise 3
// test getter methods
Date date1 = new Date( 6, 12, 1944 );
System.out.print( "Date 1 - " );
System.out.print( "Day: " + date1.getDay() + " " );
System.out.print( "Month: " + date1.getMonth() + " " );
System.out.println( "Year: " + date1.getYear() );
//test toString method
System.out.println( "Date 1 - " + date1.toString());
System.out.println();
// Added for Exercise 5
Date date2 = new Date();
System.out.println( "Default Date 2: " + date2.toString() );
date2.setDay( 27 );
date2.setMonth( 4 );
date2.setYear( 2008 );
System.out.println( "Updated Default Date 2: " + date2.toString());
}
}
Exercise 6:
The following is the compareTo method.
// Added in Exercise 6
public int compareTo( Date date )
{
String thisDate = this.toString();
String otherDate = date.toString();
return thisDate.compareTo( otherDate );
}
Lesson 8-3
Exercise 1:
It needs access to the Date and Distance classes in order to run.
Exercise 2:
1. In your current directory, create a subdirectory and name it “tripObjects”.
2. Move the Date, Distance, and Trip classes to this subdirectory.
3. Put “package tripObjects;” at the top of all three classes: Date, Distance, and Trip. This package statement must be the first line in the source file.
Exercise 3:
The following is a sample application test driver.
import tripObjects.*;
public class TestTripObjects
{
public static void main(String[] args) {
Trip trip1 = new Trip( "Munich", "Tokyo",
new Distance( 2, 170, 5837 ),
new Date( 27, 4, 2008 ),
new Date( 31, 5, 2008 ) );
System.out.println( trip1.toString() );
System.out.println();
Trip trip2 = new Trip( "Toronto", "Montreal",
new Distance( 0, 0, 500 ),
new Date( 5, 6, 2008 ),
new Date( 4, 6, 2009 ) );
System.out.println( trip2.toString() );
}
}
The output is
Destination: Munich
Beginning: Tokyo
Distance: 5837
Going: 4/27/2008
Returning: 5/31/2008
Destination: Toronto
Beginning: Montreal
Distance: 500
Going: 6/5/2008
Returning: 6/4/2009
Exercise 4:
The following is the methods that change the dates of a trip.
public void setDepartureDate( Date newDate )
{
whenGoing = newDate;
}
public void setReturnDate( Date newDate )
{
whenReturning = newDate;
}
Exercise 5:
The following is the updated test driver.
import tripObjects.*;
public class TestTripObjects
{
public static void main(String[] args) {
// Added for Exercise 3
Trip trip1 = new Trip( "Munich", "Tokyo",
new Distance( 2, 170, 5837 ),
new Date( 27, 4, 2008 ),
new Date( 31, 5, 2008 ) );
System.out.println( trip1.toString() );
System.out.println();
Trip trip2 = new Trip( "Toronto", "Montreal",
new Distance( 0, 0, 500 ),
new Date( 5, 6, 2008 ),
new Date( 4, 6, 2009 ) );
System.out.println( trip2.toString() );
// Added for Exercise 5
System.out.println();
System.out.println( "Change dates for both trips:" );
trip1.setDepartureDate( new Date( 1, 5, 2008 ) );
trip1.setReturnDate( new Date( 25, 5, 2008 ) );
System.out.println( trip1.toString() );
System.out.println();
trip2.setDepartureDate( new Date( 1, 7, 2008 ) );
trip2.setReturnDate( new Date( 4, 7, 2008 ) );
System.out.println( trip2.toString() );
}
}
The output is
Destination: Munich
Beginning: Tokyo
Distance: 5837
Going: 4/27/2008
Returning: 5/31/2008
Destination: Toronto
Beginning: Montreal
Distance: 500
Going: 6/5/2008
Returning: 6/4/2009
Change dates for both trips:
Destination: Munich
Beginning: Tokyo
Distance: 5837
Going: 5/1/2008
Returning: 5/25/2008
Destination: Toronto
Beginning: Montreal
Distance: 500
Going: 7/1/2008
Returning: 7/4/2008
Lesson 8-4
Exercise 1:
Modify the Date, Distance, and Trip classes.
- Add “import java.io.*; “ to each class between the package statement and the class declaration.
- Add “implements Serializable “ to the class declaration of each class.
After the above changes, the top of the Date class will resemble the following.
package tripObjects;
import java.io.*; //added
public class Date implements Serializable //added
{
The following is the test driver from Lesson 8-3: Exercise 5 that has been modified to send the output to a file:
import tripObjects.*;
import java.io.*;
public class WriteTripObjects
{
public static void main(String[] args) throws IOException {
ObjectOutputStream outObject = new ObjectOutputStream(
new FileOutputStream( "TripObjects.dat" ));
// Create two Trip objects
Trip trip1 = new Trip( "Munich", "Tokyo",
new Distance( 2, 170, 5837 ),
new Date( 27, 4, 2008 ),
new Date( 31, 5, 2008 ) );
Trip trip2 = new Trip( "Toronto", "Montreal",
new Distance( 0, 0, 500 ),
new Date( 5, 6, 2008 ),
new Date( 4, 6, 2009 ) );
// Output Trip objects to a file
outObject.writeObject( trip1 );
outObject.writeObject( trip2 );
// Change departure and return dates
trip1.setDepartureDate( new Date( 1, 5, 2008 ) );
trip1.setReturnDate( new Date( 25, 5, 2008 ) );
trip2.setDepartureDate( new Date( 1, 7, 2008 ) );
trip2.setReturnDate( new Date( 4, 7, 2008 ) );
// Reset the object stream to disregard the state of any objects
// already written to the stream. If we do not do this, the
// stream will not accept changes to departure/return dates.
// so that the same objects trip1 and trip2 will be saved twice
outObject.reset();
// Output updated Trip objects to the same file
outObject.writeObject( trip1 );
outObject.writeObject( trip2 );
outObject.close();
}
}
Exercise 2:
The following is a sample application that reads in the file created in Exercise 1, and prints objects to the screen.
import tripObjects.*;
import java.io.*;
public class ReadTripObjects
{
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream inObject = new ObjectInputStream(
new FileInputStream( "TripObjects.dat" ));
// Read in four Trip objects
Trip trip1 = (Trip)inObject.readObject();
Trip trip2 = (Trip)inObject.readObject();
Trip updatedTrip1 = (Trip)inObject.readObject();
Trip updatedTrip2 = (Trip)inObject.readObject();
// Print Trip objects
System.out.println( trip1.toString() );
System.out.println();
System.out.println( trip2.toString() );
System.out.println();
System.out.println
("Departure and Return dates changed for both trips:" );
System.out.println( updatedTrip1.toString() );
System.out.println();
System.out.println( updatedTrip2.toString() );
}
}
The output will be the same as in Exercise 5: Lesson 8-3.
Destination: Munich
Beginning: Tokyo
Distance: 5837
Going: 4/27/2008
Returning: 5/31/2008
Destination: Toronto
Beginning: Montreal
Distance: 500
Going: 6/5/2008
Returning: 6/4/2009
Departure and Return dates changed for both trips:
Destination: Munich
Beginning: Tokyo
Distance: 5837
Going: 5/1/2008
Returning: 5/25/2008
Destination: Toronto
Beginning: Montreal
Distance: 500
Going: 7/1/2008
Returning: 7/4/2008
Lesson 8-5
Exercise 1:
switch ( grade )
Exercise 2:
case A: ACount++; break;
case B: BCount++; break;
case C: CCount++; break;
case D: DCount++; break;
case F: FCount++; break;
Exercise 3:
for ( LetterGrade counter : LetterGrade.values() )
Exercise 4:
case A: System.out.println( ACount ); break;
case B: System.out.println( DCount ); break;
case C: System.out.println( CCount ); break;
case D: System.out.println( DCount ); break;
case F: System.out.println( FCount ); break;
Exercise 5:
The complete application is
// Class TestEnum reads in a letter as a string, and converts
// it to the appropriate LetterGrade. The number of times
// each grade appears is summed, using a switch statement.
// The tallies are printed, using a switch statement.
import java.io.*;
import java.util.*;
public class TestEnum
{
enum LetterGrade {A,B,C,D,F}
public static void main(String[] args) throws IOException
{
Scanner inFile = new Scanner(new FileReader("Enum.dat"));
LetterGrade grade;
String strGrade;
int ACount = 0;
int BCount = 0;
int CCount = 0;
int DCount = 0;
int FCount = 0;
while (inFile.hasNext())
{
grade = LetterGrade.valueOf(inFile.nextLine());
// Add one to tally associated with grade
switch ( grade ) /* TO BE FILLED IN: Exercise 1 */
{
/* TO BE FILLED IN: Exercise 2 */
case A: ACount++; break;
case B: BCount++; break;
case C: CCount++; break;
case D: DCount++; break;
case F: FCount++; break;
}
}
for ( LetterGrade counter : LetterGrade.values() )
/* TO BE FILLED IN: Exercise 3 */
// Iterate through each value of LetterGrade
{
System.out.print(counter + ": ");
// Print tally associated with each LetterGrade
switch (counter)
{
/* TO BE FILLED IN: Exercise 4 */
case A: System.out.println( ACount ); break;
case B: System.out.println( DCount ); break;
case C: System.out.println( CCount ); break;
case D: System.out.println( DCount ); break;
case F: System.out.println( FCount ); break;
}
}
}
}
The following is printed.
A: 5
B: 4
C: 4
D: 4
F: 3
Lesson 8-6
Exercise 1:
The following is a sample list of potential objects.
Photo Collection
Photo Set
Photo
Date
Location
Output File
Input File
User Interface
Dimension
Digital Camera
Exercise 2:
The following is a sample list of filtered classes.
Photo Collection
Photo Set
Photo
Date
Output File
User Interface
A Photo Collection consists of one or more Photo Sets. A Photo Set refers to a set of Photos taken during an event (a birthday party, a trip, etc.).
There will be multiple file objects. The Photo Collection will maintain a file of Photo Sets. Each Photo Set will maintain a file of Photos (their data) belonging to this Photo Set.
Exercise 3:
The following are sample CRC cards for the classes identified in Exercise 2.
Class Name:Photo / Superclass: / Subclasses:
Responsibilities / Collaborations
Create itself(title, resolution,
description, filePath) / None
getResolution
return String / None
getTitle
return String / None
getDescription
return String / None
getPhotoFilePath()
return String / None
toString
return String / None
Class Name: Date / Superclass: / Subclasses:
Responsibilities / Collaborations
Create itself(day, month, year) / None
getDay
return int / None
getMonth
return String / None
getYear
return int / None
toString
return String / None
Class Name: PhotoSet / Superclass: / Subclasses:
Responsibilities / Collaborations
Create itself(title, description) / None
getPhoto(photoTitle)
return Photo / Photo
getNextPhoto()
return Photo / Photo
getDate
return Date / Date
getDescription
return String / None
getPhotoSetTitle
return String / None
deletePhoto( photoTitle) / Photo
addPhoto( Photo) / Photo
toString
return String / None
Name: PhotoCollection / Superclass: / Subclasses:
Responsibilities / Collaborations
Create itself (file name) / None
getPhotoSet(photoSetTitle)
return PhotoSet / PhotoSet
removePhotoSet(photoSetTitle) / PhotoSet
addPhotoSet( PhotoSet) / PhotoSet
updatePhotoSet(photoSetTitle) / PhotoSet
Class Name: UserInterface / Superclass: / Subclasses:
Responsibilities / Collaborations
Create itself
browsePhotoSet(photoSetTitle)
getPhotoSetByName(photoSetTitle)
return PhotoSet / PhotoSet
inputPhotoSetData
return PhotoSet / Scanner, PhotoSet
inputPhotoData
return Photo / Scanner, Photo
Exercise 4:
The attributes of the classes are
Class Name / AttributesPhoto / Title
Description
Resolution
Path to physical file of the image
Date / Day
Month
Year
Photo Set / Title
Description
Photo Collection / Input File Stream (to read input files)
Output File Stream (to write to output files)
File name
User Interface / photoCollection
Exercise 5:
A sample of simple test driver may resemble:
Start of method main(String[] args)
Create an instance of User Interface
Call the browsePhotoSet method
End of main
The test driver calls the constructor and browsePhotoSet methods of the User Interface class. For the purpose of this test driver, these methods may resemble
Start of method browsePhotoSet()
Prompt the user to enter a title of Photo Set
Retrieve the Photo Set from the Photo Collection (call getPhotoSetByName( photoSetTitle))
If Photo Set not NULL
While not done
Retrieve a Photo from the Photo Set (call getNextPhoto())
Print Photo data to the screen
Wait for the user response (e.g., Enter - continue, Quit - exit)
Else
Inform the user “No Photo Set with such a title!”
End of browsePhotoSet
Start of constructor UserInterface()
Create an instance of PhotoCollection
End of constructor.
Lesson 8-7
Exercise 1: