LAB 15: EXPERIMENTING WITH ABSTRACT
CLASSES AND INTERFACES
Advanced
Programming - CS 239 Department
of Computer Science
Getting Ready: Before going any further
you should:
1. Make a directory on
your N: drive for this lab.
2. Setup your
development environment.
3. Download the file
named TwoPartMeasure.java, Length.java, and Weights.java.
4. Review all of the
classes.
Part I: This part of the lab
will help you review abstract classes.
1. Remove the abstract
modifier
from the declaration of the TwoPartMeasure class and the
declaration of the initializeUnits method. Compile the
class. What error is generated?
|
2. Replace the two abstract
modifiers
and re-compile the TwoPartMeasure class. Why does this
class compile even though it has a method with a missing method body?
|
2b. Does the order of the modifiers matter? Change the order of the visibility modifiers
and abstract and test with the compiler before answering this question.
|
3. Comment out the initializeUnits
method.
Compile the class. What error is generated?
|
4. Restore the commented
out code.
5. Download the file
named Driver0.java
Which of the two
statements in main will cause a compile-time error and why?
|
Part II: This part of the lab
will help you understand specializations of abstract classes.
1. Download the file
named Length.java.
Compile this class.
Is this class
"concrete"? Why or why not?
|
2. Download the file
named Driver1.java. Compile this class.
Compile and execute this
driver. Does the Length class work properly?
|
3. Comment-out the
implementation of the initializeUnits() in the Length
class. Re
-compile the Length class. What error is generated?
|
4. Replace the commented
out code and recompile.
5. Download the Weight
class
(containing pounds and ounces) that extends the TwoPartMeasure class and compile.
Part III: This part of the lab
will help you understand "type safety" issues that sometimes arise
with specializations of abstract classes.
1. Download the file
named Driver2.java.
Compile the driver. Why
doesn't the expression myLength.equals(myWeight) generate a compile-time
error?
|
2. Execute the driver.
What is output?
|
3. Why does the weight
equal the length in the example above? (Note: We will see how this
"problem" can be fixed later in the semester.)
|
Getting Ready: Before going any
further you should:
1.
Download the file, FixedTermSavingsAccount.java (this is the same
program that you are using for PT3.
2.
Download the file named Driver3.java.
3.
Familiarize yourself with the Comparable interface. (See the Java API’s).
Part I: This part of the lab will help you get started with interfaces.
1. Compile and execute Driver3. What
was output?
|
2. Add the following to the end of the
main() method in Driver3:
// Sort the accounts
Arrays.sort(accounts);
// Print the accounts in the sorted order
for (i=0; i < accounts.length; i++)
{
System.out.println(accounts[i].getAccountID()+"\t"+
accounts[i].getUserName());
}
3. Re-compile and re-execute
Driver3. What error is generated?
|
4. Is this a compile-time or a run-time
error?
|
5. The error above is generated because
the Arrays.sort() method (and other methods it calls) expect to be passed
objects that implement the Comparable interface. Since the FixedTermSavingsAccount class contains
a compareTo() method it seems like this problem can be fixed by changing the
declaration of the class to public class FixedTermSavingsAccount implements
Comparable. Make this change and re-compile the FixedTermSavingsAccount class.
What error is generated and why?
|
6. Fix this problem by changing the
type of the parameter of the compareTo() method and casting it (to a
FixedTermSavingsAccount within the method. What specific changes did you make?
|
7. Re-compile and re-execute Driver3.
What was output?
|
Part
II: This part
of the lab will help you understand how to implement an interface. Return to the TwoPartMeasure project from the
first Section of this lab.
|
|
Submission
– Submit this lab by turning in this hard-copy worksheet no later than Monday
Mar 21.