/** * 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.

* * @author Jason Eisner * @author YOUR NAME HERE * @version 0.5 (you should update this to 1.0 when you're done) */ public class RussianDoll { // We'll organize the code for the class in the order suggested // in section 1.9.2 of the textbook, page 47. // ************** CONSTANTS *************** /** The special doll representing ZERO. */ public static final RussianDoll ZERO = new RussianDoll(); // ************** INSTANCE VARIABLES *************** /** Contains the next doll in, or in the special case of ZERO, * contains this doll itself. */ private RussianDoll inner; // ************** CONSTRUCTORS *************** /** The usual constructor. Wraps a new doll around an existing one. */ private RussianDoll(RussianDoll rd) { inner = rd; } /** A special constructor returning a cyclic doll. Used only by ZERO. */ private RussianDoll() { inner = this; } // ************** BASIC METHODS *************** /** Subtracts one. Special case: When applied to ZERO, returns ZERO. * @return The inner Russian doll, or ZERO. */ public RussianDoll smaller() { return inner; } /** Adds one. * @return A newly created, bigger Russian doll that contains the old one. */ public RussianDoll bigger() { return new RussianDoll(this); } /** 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. */ public String toString() { if (this == ZERO) return "0"; else return "S" + inner.toString(); } // ************** TYPE CONVERSIONS *************** // this would be a good place to put longValue and valueOf // ************** INSTRUMENTATION METHODS *************** // this would be a good place to put newWork and newSpace // ************** COMPARISON METHODS *************** // this would be a good place to put equals and compareTo // ************** ARITHMETIC METHODS *************** /** Implements {@link Numeric#add(Numeric)}. */ public RussianDoll add(RussianDoll val) { if (this==ZERO) return val; // 0+val == val else return smaller().add(val.bigger()); /* If you have trouble understanding the above line, * it is equivalent to the following: * * RussianDoll rdThisMinusOne = smaller(); * RussianDoll rdValPlusOne = val.bigger(); * return rdThisMinusOne.add(rdValPlusOne); * * So we are taking advantage of the fact * that a+b == (a-1)+(b+1) for any a > 0. * We keep recursing using this formula, * counting downward with a * while we count upward with b. */ } // ************** TEST METHODS *************** /** A test method of your choice; do whatever you want * here. See also {@link NumericTest}. */ public static void main(String args[]) { System.out.println("Zero : " + ZERO); System.out.println("Zero ++ ++ ++ -- ++ : " + ZERO.bigger().bigger().bigger().smaller().bigger()); System.out.println("Zero ++ -- -- ++ : " + ZERO.bigger().smaller().smaller().bigger()); RussianDoll rdThree = ZERO.bigger().bigger().bigger(); RussianDoll rdFour = ZERO.bigger().bigger().bigger().bigger(); System.out.println("Three : " + rdThree); System.out.println("Four : " + rdFour); System.out.println("Three + Four (instance method) : " + rdThree.add(rdFour)); } }