import java.util.ArrayList; // needed if using an ArrayList to store the // digits of a number /* BigIntUnsigned.java ** Author: R. McCloskey and student named below ** Date: February 2019 ** ** CMPS 144 Spring 2019 ** Student name: ... ** Collaborators: ... ** Known flaws: ... ** ** An instance of this class represents an immutable unsigned (i.e., ** nonnegative) integer. The usual arithmetic operations (sum, ** difference, product, quotient, remainder, less/greater than) are ** supported. What makes it interesting is that there is no fixed upper ** bound upon the value that such an object can represent, making it ** unlike any primitive data type used for representing integers (i.e., ** byte, short, int and long) and unlike the corresponding "wrapper" ** classes (java.lang.Integer, etc.) */ public class BigIntUnsigned implements Comparable, Cloneable { // class constants // --------------- public final static BigIntUnsigned ZERO = new BigIntUnsigned("0"); public final static BigIntUnsigned ONE = new BigIntUnsigned("1"); // instance variables // ------------------ protected ArrayList digits; // stores the digits from least // to most significant // alternative: // protected int[] digits; // constructors // ------------ /* Initializes this object so that it represents the integer described ** by the given string, which is assumed to be an unsigned decimal ** numeral (meaning that each of its characters is in the range '0'..'9'). */ public BigIntUnsigned(String digitStr) { // STUB! } /* Initializes this object so that it represents the given value of ** type int. */ public BigIntUnsigned(int num) { this(num + ""); } /* Initializes a new instance of this class by making its instance variable ** be a clone of the provided value. ** Meant for use only within this class and descendant classes. */ protected BigIntUnsigned(ArrayList digits) { this.digits = (ArrayList)(digits.clone()); trimLeadingZeros(); } // Alternative if instance variable digits is of type int[]: //protected BigIntUnsigned(int[] digits) { // this.digits = digits.clone(); // trimLeadingZeros(); //} // observers // --------- /* Returns the decimal numeral corresponding to this object's value. ** E.g., if this object represents 5689, the string returned should be ** "5689". A leading zero should appear only if the object represents ** zero. */ public String toString() { return ""; // STUB! } /* Returns a negative value, zero, or a positive value, respectively, ** according to whether the integer represented by this object is ** less than, equal to, or greater than, respectively, the integer ** represented by the other object. */ public int compareTo(BigIntUnsigned other) { return 0; // STUB! } /* Reports whether or not this object is equal to the other one. That is ** the case if and only if the other object, too, is an instance of this ** class and the two objects represent the same integer value. */ public boolean equals(Object other) { return (other instanceof BigIntUnsigned) && this.compareTo((BigIntUnsigned)other) == 0; } /* Reports whether or not this object represents an integer that is ** less than that represented by the other object. */ public boolean isLessThan(BigIntUnsigned other) { return this.compareTo(other) < 0; } /* Reports whether or not this object represents an integer that is ** greater than that represented by the other object. */ public boolean isGreaterThan(BigIntUnsigned other) { return this.compareTo(other) > 0; } // generators // ---------- /* Returns a new instance of this class that represents the sum of ** this object and the other one (i.e., this one plus the other one) */ public BigIntUnsigned plus(BigIntUnsigned other) { ArrayList resultDigits = new ArrayList(); // alternative: int[] resultDigits = new int[??]; // Missing code return new BigIntUnsigned(resultDigits); } /* Returns a new instance of this class that represents the difference ** of this object and the other one (i.e., this one minus the other one). ** pre: !this.lessThan(other) */ public BigIntUnsigned minus(BigIntUnsigned other) { ArrayList resultDigits = new ArrayList(); // alternative: int[] resultDigits = new int[??]; // Missing code return new BigIntUnsigned(resultDigits); } /* Returns a new instance of this class that represents the product of ** this object and the other one (i.e., this one times the other one). ** Additional/Optional!! */ public BigIntUnsigned times(BigIntUnsigned other) { return null; // STUB! } /* Returns a new instance of this class that represents the integer ** quotient resulting from dividing this object by the other one. ** pre: !other.equals(ZERO) ** Additional/Optional!! */ public BigIntUnsigned dividedBy(BigIntUnsigned other) { return null; // STUB! } /* Returns a new instance of this class that represents the remainder ** resulting from dividing this object by the other one. ** pre: !other.equals(ZERO) ** Additional/Optional!! */ public BigIntUnsigned mod(BigIntUnsigned other) { return null; // STUB! } // private // ------- /* Removes any elements of digits that correspond to leading zero's. */ private void trimLeadingZeros() { // STUB! } /* Returns the digit in the 10^k column of the decimal numeral ** corresponding to this object. ** pre: k >= 0 */ protected int digitAt(int k) { if (k >= digits.size()) { return 0; } else { return digits.get(k); } } //Alternative if instance variable digits is of type int[]: // if (k >= digits.length) { return 0; } // else { return digits[k]; } }