Class RussianDoll

java.lang.Object
  |
  +--RussianDoll

public class RussianDoll
extends java.lang.Object

We designed this class in lecture. A Russian doll is an object that contains another Russian doll. There is a special Russian doll called ZERO that contains itself. Our public methods do not allow the user to construct any other dolls that are nested inside themselves.

Russian dolls can be used to represent integers without actually using Java's integer class. ZERO represents 0, a doll that contains ZERO represents 1, a doll that contains 1 represents 2, etc.

Notice that there may be many different dolls that represent a number such as 5. In other words, even if rd1 and rd2 are both dolls that represent 5, there is no guarantee that rd1 == rd2. They might be different objects in memory.


Field Summary
private  RussianDoll inner
          Contains the next doll in, or in the special case of ZERO, contains this doll itself.
static RussianDoll ZERO
          The special doll representing ZERO.
 
Constructor Summary
private RussianDoll()
          A special constructor returning a cyclic doll.
private RussianDoll(RussianDoll rd)
          The usual constructor.
 
Method Summary
 RussianDoll add(RussianDoll val)
          Implements Numeric.add(Numeric).
 RussianDoll bigger()
          Adds one.
static void main(java.lang.String[] args)
          A test method of your choice; do whatever you want here.
 RussianDoll smaller()
          Subtracts one.
 java.lang.String toString()
          Converts to a string representing a Peano integer: "0", "S0", "SS0", etc.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final RussianDoll ZERO
The special doll representing ZERO.


inner

private RussianDoll inner
Contains the next doll in, or in the special case of ZERO, contains this doll itself.

Constructor Detail

RussianDoll

private RussianDoll(RussianDoll rd)
The usual constructor. Wraps a new doll around an existing one.


RussianDoll

private RussianDoll()
A special constructor returning a cyclic doll. Used only by ZERO.

Method Detail

smaller

public RussianDoll smaller()
Subtracts one. Special case: When applied to ZERO, returns ZERO.

Returns:
The inner Russian doll, or ZERO.

bigger

public RussianDoll bigger()
Adds one.

Returns:
A newly created, bigger Russian doll that contains the old one.

toString

public java.lang.String toString()
Converts to a string representing a Peano integer: "0", "S0", "SS0", etc. The letter "S" stands for "successor," but in the case of Russian dolls, it could stand for "surrounds": S0 is a doll that surrounds 0. SS0 is a doll that surrounds S0, etc.

Overrides:
toString in class java.lang.Object

add

public RussianDoll add(RussianDoll val)
Implements Numeric.add(Numeric).


main

public static void main(java.lang.String[] args)
A test method of your choice; do whatever you want here. See also NumericTest.