o
this()
construct in constructors.
int
type provides the
same operations, and in a much more intuitive fashion. We introduce them for several reasons:
ImmutableInt
) (Approval)Immutable
, modelling integers, with the following state/behavior:
ImmutableInt add(ImmutableInt immutableInt)
ImmutableInt sub(ImmutableInt immutableInt)
ImmutableInt mul(ImmutableInt immutableInt)
ImmutableInt div(ImmutableInt immutableInt)
toString()
int
instance variable maintaining the integer value of the object
this(…)
construct)
div
… ImmutableInt i1 = new ImmutableInt(10); System.out.println("i1: " + i1); … System.out.println("i1.add(i2): " + i1.add(i2); …
stdout
i1: 10 i2: 20 i3: 0 i1.add(i2): 30 i1.sub(i2): -10 i1.mul(i2): 200 i1.div(i2): 0 i1: 10 i2: 20 i3: 0
ImmutableIntApp
) (Approval)
MutableInt
) (Approval)MutableInt
, modelling integers,with the following state/behavior:
MutableInt increaseBy(MutableInt immutableInt)
MutableInt decreaseBy(MutableInt immutableInt)
MutableInt multiplyBy(MutableInt immutableInt)
MutableInt divideBy(MutableInt immutableInt)
MutableInt reset()
toString()
int
instance variable maintaining the integer value of the object
this(…)
construct)
reset
sets the internal state variable to 0
divideBy
… MutableInt i0 = new MutableInt(); System.out.println("i0: " + i0); … System.out.println("i0.increaseBy(i1): " + i0.increaseBy(i1); …
stdout
===== Initial Values ===== i0: 0 i1: 10 i2: 5 i3: 20 i4: 25 i0.increaseBy(i1): 10 i0: 10 i1: 10 i2: 5 i3: 20 i4: 25 i0.decreaseBy(i2): 5 i0: 5 i1: 10 i2: 5 i3: 20 i4: 25 i0.multiplyBy(i3): 100 i0: 100 i1: 10 i2: 5 i3: 20 i4: 25 i0.divideBy(i4): 4 i0: 4 i1: 10 i2: 5 i3: 20 i4: 25 i0.reset(): 0 i0: 0 ===== Chaining ===== i0.increaseBy(i1).decreaseBy(i2).multiplyBy(i3).divideBy(i4): 4 i0: 4 i1: 10 i2: 5 i3: 20 i4: 25
ImmutableIntApp
) (Approval)