Name: ________________
ID:_______________
Required files:
Before you begin,
make copies of DateHelper.java
,
SimpleDate.java
,
SimpleTime.java
. Compile
all three files.
Step 1.
Create a new program, NewDateTester.java
, with a main
method that contains the following lines.
SimpleOutput out = new SimpleOutput(); DateHelper helper = new DateHelper(); helper.printRange(out, new SimpleDate(1964,6,17), new SimpleDate(1964,12,3));
Compile and execute NewDateTester
. Record the results.
Step 2.
Add the following lines to the main
method of NewDateTester
.
helper.printRange(out, new SimpleTime(1964,6,17), new SimpleTime(1964,12,3));
Recompile and execute NewDateTester
. Record the new results.
Step 3. Were the results in steps 1 and 2 the same? If so, why? If not, why not?
Step 4.
Add the following lines to the main
method of NewDateTester
.
helper.printRange(out, new SimpleTime(1964,6,17,5,0), new SimpleTime(1964,12,3,11,30));
What do you expect the new output to be?
Step 5.
Recompile and execute NewDateTester
. Record the new results.
Were they what you expected? Why or why not?
Step 6.
Add the following lines to the main
method of NewDateTester
.
helper.printRange(out, new SimpleDate(1964,6,17), new SimpleTime(1964,12,3,11,30));
What do you expect the new output to be?
Step 7.
Recompile and execute NewDateTester
. Record the new results.
Were they what you expected? Why or why not?
NewPoint
s and PointPrinter
sRequired files:
Step 1.
Make copies of Point.java
, PointFun.java
, and
NewPoint.java
. Change the body of the main
method
of PointFun.java
to read
// We'll be generating some output. SimpleOutput out = new SimpleOutput(); // The point we'll be playing with. Point pt = new Point(2,3); // Something to help us print PointPrinter printer = new PointPrinter(); // Print some basic information printer.print(out,pt); // Move it right a little bit. out.println("Shifting right by 0.7"); pt.shiftRight(0.7); // Print current information printer.print(out,pt); // Move it right a little bit. out.println("Shifting up by 2.5"); pt.shiftUp(2.5); // Print current information printer.print(out,pt); // Move it left a little bit. out.println("Shifting left by 10.2"); pt.shiftLeft(10.2); // Print current information printer.print(out,pt);
Compile all three classes and execute
PointFun
. Record your results.
Step 2.
Update PointFun
to use a NewPoint
wherever it used
a Point
. Summarize your changes here.
Step 3.
Try to compile PointFun
. What happens?
After writing your answer, you may want to look at the notes.
Step 4.
Update PointPrinter
so that it can print both Point
s and
NewPoint
s. Recompile PointPrinter
.
Summarize your changes here.
You may also want to look at a suggested update.
Step 5.
Recompile PointFun
. Are you successful? If so, why would you have
been successful here and not in step 3? If not, why not?
After recording your answer, look at the notes on the subject.
Step 6.
Execute the newly compiled PointFun
and record your results. Are
they what you expected? Why or why not?
Step 7.
Record any general observations you have about the process of creating and using
variant of the Point
class in this experiment.
ExtendedPoint
s Required files:
Point.java
PointFun.java
PointPrinter.java
ExtendedPoint.java
(created and modified in previous laboratory
sessions)
Step 1.
Make copies of Point.java
, PointFun.java
,
PointPrinter.java
, and
ExtendedPoint.java
. The body of the main
method
of PointFun
should be identical to that of step 1 of
Experiment O3.2. Compile all four classes. Execute
PointFun
. Record your results.
Step 2.
Update the main
method of PointFun
so that it uses
ExtendedPoint
s instead of Point
s.
Do not modify PointPrinter
! Summarize your changes.
Step 3.
Compile and execute PointFun
. Were you successful? If
not, summarize and explain the error messages. If you were successful,
compare your answer to step 3 of Experiment
O3.2. When you are done, read the notes on
this step.
Step 4.
Record any general observations you have about the process of creating and
using a subclass of the Point
class in this experiment.
Step 1. Make copies of the two files and compile them.
Step 2.
Create the following Printable
class
public class Printable { /** * Convert the current object to a string. */ public String makePrintable() { ... } // toString() } // class Printable
You will need to fill in something appropriate for the ellipses. Compile this class and correct any errors you encounter. What did you use in place of the ellipses?
Step 3.
Update SimpleOutput.java
to include a
println(Printable)
method. Summarize the
changes you've made to add such a method.
After doing so, you may want to read the notes on this step.
Step 4.
Create a new class, PrintablePoint
that extends
Point
. Add a makePrintable
method
to PrintablePoint
. Compile PrintablePoint
and correct any errors. Summarize the structure of
PrintablePoint
.
Step 5.
Create a new class, PointPrinter
, that creates and prints
a PrintablePoint
. The main
method of
PointPrinter
should be similar to
SimpleOutput out = new SimpleOutput(); PrintablePoint pt = new PrintablePoint(1,4); out.print("The point is "); out.println(pt);
What do you expect to happen if you try to compile or execute
PointPrinter
? Why?
Step 6.
Compile and execute PointPrinter
. What happened? Is
this what you expected? Explain the results.
Step 7.
Update PrintablePoint
so that it extends both
Point
and Printable
. The class header
will read
public class PrintablePoint extends Point extends Printable { ... } // class PrintablePoint
What happens when you try to compile this new class? Why?
Step 8.
Change Printable
from a class to an interface.
Summarize the changes you've made.
Step 9.
Try to compile PrintablePoint
. What happens? How
does this result differ from the result in step 7, and why?
Step 10.
Update PrintablePoint
so that it implements
Printable
rather than extending it. Try to compile
the revised class. How do your results differ from those in
steps 7 and 9? Why do they differ?
Step 11.
Execute PointPrinter
and record the results. Are they
what you expected? Why or why not?
Required files:
Printable.java
(the interface, not the class)
SimpleOutput.java
(as modified in the previous experiment)
Step 1.
If you have not already done so, make a copy of the required files
and add a println(Printable)
method to SimpleOutput
.
Step 2.
Create a new class, Person
, which implements
Printable
. Person
should have one field, a
string representing the name of the person. Make sure you include an
appropriate constructor for setting that name. Do not include a
makePrintable
method! Summarize your design for
the class.
Step 3.
Create a new class, PersonPrinter
with a main
method that reads
SimpleOutput out = new SimpleOutput(); Person author = new Person("Mary P. Boelk"); out.print("The author is "); out.println(author);
Compile and execute PersonPrinter
. Record the results.
Are they what you expected? Why or why not?
Step 4.
Add a makePrintable
method to Person
that that returns the name of the person. Recompile Person
and execute PersonPrinter
. Record the results. Were
they the same as in the previous step? Why or why not?
Step 5.
Change the keyword implements
in
Person
to extends
.
Attempt to recompile Person
. What happens? Why
do you think this is? After writing your answer, restore the
implements
.
Step 6.
Build a Student
class that extends Person
.
Students should include majors in addition to names. Do not
explicitly indicate that Student
implements
Printable
and do not include a makePrintable
.
Compile Student
and correct any errors you encounter. Summarize
the design of your class.
Step 7.
Update the main
method of PersonPrinter
to
print a Student
rather than a Person
. For
example, you might write
Student generic = new Student("Gen R. Ic", "None"); out.print("This is what we know about the student: "); out.println(generic);
Compile and execute the revised PersonPrinter
. Were you
successful? What might success indicate?
Step 8.
Add a makePrintable
method to Student
.
Recompile Student
and execute
PersonPrinter
. Record your results. What do they
suggest? Note that you never explicitly indicated that
Student
s are printable.
[Front Door] [Introduction] [Code]
Copyright (c) 1998 Samuel A. Rebelsky. All rights reserved.
Source text last modified Tue Oct 26 12:28:46 1999.
This page generated on Tue Oct 26 15:36:48 1999 by Siteweaver.
Contact our webmaster at rebelsky@math.grin.edu