Java Reference
In-Depth Information
E
XERCISES
1.
Is the class
Student
that is mentioned in Segment 30.18 mutable or immutable? Describe what you would do to
create a companion class for
Student
.
2.
a.
Using the method
getEntry
of the ADT list, describe a client-level method that modifies the entry at a given
position within a list.
b.
What must you know about the entries in the list for your method to work?
c.
Does the method
replace
of the ADT list have any advantage over your method?
3.
Implement the method
compareTo
for the class
ImmutableName
.
4.
Consider the following class definition:
public class
Friend
{
private
Name friendsName;
private
String friendsAddress;
public
Friend(Name aName, String anAddress)
{
friendsName = aName;
friendsAddress = anAddress;
}
// end constructor
public
Name getName()
{
return
friendsName;
}
// end getName
public
String getAddress()
{
return
friendsAddress;
}
// end getAddress
}
// end Friend
Is this class read only? Why?
5.
Given the class
Name
, define a companion class
ImmutableName
by using composition. That is,
ImmutableName
should contain an instance of
Name
as a data field. What are the advantages and disadvantages of using
composition to define companion classes?
6.
Given the class
ImmutableName
, define a companion class
Name
by using inheritance. What are the advantages and
disadvantages of using inheritance to define companion classes?
7.
Define an interface,
ImmutableNameInterface
, that declares the methods in the class
ImmutableName
. Define
another interface,
MutableNameInterface
, that extends
ImmutableNameInterface
and declares the methods in the
class
Name
. Then modify the classes
ImmutableName
and
Name
so that they implement
ImmutableNameInterface
and
MutableNameInterface
, respectively.
8.
A variable of type
ImmutableNameInterface
can reference objects of what type?
9.
A variable of type
MutableNameInterface
can reference objects of what type?
10.
A variable of type
Name
can reference objects of what type?
11.
A variable of type
ImmutableName
can reference objects of what type?