rPart
and
iPart
for the real
and imaginary parts.
Show the appropriate methods so that you can provide a
class-specific initialization message, so that you can write
Complex r: 3.4 i: 5.0
to create a new complex number. Describe
how the expression
Complex r: 3.4 i: 5.0
Dictionary
parameterized by K
, the types of the
keys, and by V
, the types of the values.
Dictionary
has two operations: lookup
, which
takes a single argument of type K
and returns an object of
type V
; and store
, which takes two arguments of
types K
and V
respectively. Thus
lookup
looks up a key in the dictionary and returns the
corresponding value, while store
enters a new key-value pair
in the dictionary, or replaces the value in an existing key-value pair.
We also have an abstract type Array
, parameterized by a single
type T
. Array also has two operations: lookup
,
which takes a single argument of type integer
and returns an
object of type T
; and store
, which takes two
arguments of types integer
and T
respectively.
Thus, an array is a kind of dictionary where the keys are restricted to being integers.
Using the contravariant rule, what is the subtype relation between the
following pairs of types? The answer to each question would be either
X
is a subtype of Y
, Y
is a subtype
of X
, they are equivalent (i.e. each is a subtype of the
other), or neither is a subtype of the other.
Dictionary[Object,Object]
and Array[Object]
Dictionary[Integer,Object]
and Array[Object]
Array[Integer]
and Array[Object]
Hint: Java supports overloading, so that a method
void mytest(Object x)
void mytest(String x)
void mytest(Object x)
void mytest(String x)
void mytest(String x)
max
for
Comparable
that finds the maximum of the receiver and the
argument. Add specifications for a method add_all
for
Set
that adds all of the elements in the argument (also a set)
to the receiver, and another method maximum
that finds the
maximum element in a set.
Comparable
interface? What would be wrong with just writing
Consider the following Smalltalk code. There is a class named
Squid
, with three methods named method1
,
method2
, and method3
.
s method1
for an instance s
of
Squid, what is printed to the transcript? Explain your reasoning.
not
:
x
in the following
equality constraint:
y
here:
y
to
satisfy the constraint. Briefly discuss the implications for the language
semantics and implementation. (One paragraph is enough.)
m display
. If we encounter the expression
super display
in the method for display
, we'll
look for a display
method starting in C's immediate superclass.
X := X+5
is represented
by temporarily asserting the constraint Temp = X+5
, then
retracting the constraint using backtracking, and finally asserting
X = Temp
.