|
| 1 | +/** |
| 2 | + * A class that represents Arbitrary Whole Numbers |
| 3 | + * @author David Nguyen |
| 4 | + * @since 11/14/2022 |
| 5 | + */ |
| 6 | +public class ArbitraryWholeNumbers extends ArbitraryFloatingPointNumbers { |
| 7 | + |
| 8 | + // A boolean field that indicates whether the number is less than 0 or not |
| 9 | + private boolean isNegative; |
| 10 | + // An array that stores the digits of the floating point number |
| 11 | + private int[] digits; |
| 12 | + |
| 13 | + /** |
| 14 | + * Creates an Arbitrary Whole Number. |
| 15 | + * Assigns the class's fields the values that the constructor got when a Arbitrary Whole Number object is created. |
| 16 | + * Passes the fields to the superclass (Arbitrary Floating Point Number). |
| 17 | + * @param isNegative A boolean indicating whether the current number is negative or not |
| 18 | + * @param digits An array that represents the digits of an arbitrary floating point number |
| 19 | + */ |
| 20 | + public ArbitraryWholeNumbers(boolean isNegative, int[] digits) { |
| 21 | + super(isNegative, digits); |
| 22 | + this.isNegative = isNegative; |
| 23 | + this.digits = digits; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Determines whether the current arbitrary whole number is negative or not. |
| 28 | + * Returns true if the current number is negative. |
| 29 | + * Returns false if the current number is NOT negative. |
| 30 | + * @return A boolean that indicates whether the current number is negative. |
| 31 | + */ |
| 32 | + public boolean isNegativeNumber() { |
| 33 | + if (isNegative) |
| 34 | + return true; |
| 35 | + else |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * An overridden toString method that creates the string representation of an arbitrary whole number given. |
| 41 | + * @return A string that is the proper string representation of an arbitrary whole number given |
| 42 | + */ |
| 43 | + public String toString() { |
| 44 | + StringBuilder result = new StringBuilder(); |
| 45 | + // Append '-' sign to the beginning of the number if the number is negative |
| 46 | + if (this.isNegative == true) { |
| 47 | + result.append('-'); |
| 48 | + } |
| 49 | + // A new array that is same as digits but has all leading zeros removed. |
| 50 | + int[] array = removeLeadingZero(digits); |
| 51 | + // Append every element of the array that has all leading zeros removed to the resulting string |
| 52 | + for (int i = (array.length - 1); i >= 0; i--) { |
| 53 | + result.append(array[i]); |
| 54 | + } |
| 55 | + return result.toString(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * A helper method to remove leading zeros. |
| 60 | + * @param array Any given int array that needs its leading zeros removed |
| 61 | + * @return An array that has all its leading zeros removed |
| 62 | + */ |
| 63 | + public int[] removeLeadingZero(int[] array) { |
| 64 | + // A variable that stores the numbers of leading zeros |
| 65 | + int numLeadingZero = 0; |
| 66 | + // A variable that stores whether the current digit in the array is a non-zero |
| 67 | + boolean isNonZero = false; |
| 68 | + // A variable storing the index for the following 'while' loop |
| 69 | + int i = array.length - 1; |
| 70 | + /* A loop that determines the number of leading zeros. |
| 71 | + * The nonZero variable will also be set when the first non-zero is met to indicates the end of loop |
| 72 | + * Condition: The index must be more than or equal to 0 and the isNonZero variable must be FALSE |
| 73 | + */ |
| 74 | + while (i >= 0 && !isNonZero) { |
| 75 | + if (array[i] == 0) { |
| 76 | + numLeadingZero++; |
| 77 | + isNonZero = false; |
| 78 | + i--; |
| 79 | + } |
| 80 | + else |
| 81 | + isNonZero = true; |
| 82 | + } |
| 83 | + // A variable that stores the length of the new array |
| 84 | + int newLength = array.length - numLeadingZero; |
| 85 | + // An array of type int that stores the new array |
| 86 | + int[] newArray = new int[newLength]; |
| 87 | + // A variable storing the index of the old (given) array |
| 88 | + int index1 = array.length - numLeadingZero - 1; |
| 89 | + // A variable storing the index of the new array |
| 90 | + int index2 = newArray.length - 1; |
| 91 | + /* A loop that copies every element from old, original array to the new array |
| 92 | + * Condition: index of both arrays must be more than or equal to 0 |
| 93 | + */ |
| 94 | + while (index1 >= 0 && index2 >= 0) { |
| 95 | + newArray[index2] = array[index1]; |
| 96 | + index1--; |
| 97 | + index2--; |
| 98 | + } |
| 99 | + return newArray; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * A helper method that converts all arrays to strings. |
| 104 | + * @param array Any given arrays of type int |
| 105 | + * @return A String that is the string representation of the given array |
| 106 | + */ |
| 107 | + public String arrayToString(int[] array) { |
| 108 | + // A variable of type StringBuilder that stores the output string |
| 109 | + StringBuilder output = new StringBuilder(); |
| 110 | + /* A loop that appends every element of the given array into the output string |
| 111 | + * Condition: index must be less than length of array */ |
| 112 | + for (int i = 0; i < array.length; i++) { |
| 113 | + if (array[i] != 0) |
| 114 | + output.append(array[i]); |
| 115 | + } |
| 116 | + return output.toString(); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * An overridden equals method that determines if two whole numbers are equal to each other |
| 121 | + * This can be done by comparing the contents of the two string representations of the two numbers |
| 122 | + * @param o Any given object |
| 123 | + * @return Returning true or false depending on these numbers' equality |
| 124 | + */ |
| 125 | + public boolean equals(Object o) { |
| 126 | + if (o instanceof ArbitraryWholeNumbers) { |
| 127 | + ArbitraryWholeNumbers number = (ArbitraryWholeNumbers) o; |
| 128 | + // A variable storing this number's digits with leading zeros removed |
| 129 | + int[] newArray1 = removeLeadingZero(this.digits); |
| 130 | + // A variable storing input, given number's digits with leading zeros removed |
| 131 | + int[] newArray2 = removeLeadingZero(number.digits); |
| 132 | + // A variable storing the string representation of this ArbitraryWholeNumbers |
| 133 | + String s1 = arrayToString(newArray1); |
| 134 | + // A variable storing the string representation of the given ArbitraryWholeNumbers |
| 135 | + String s2 = arrayToString(newArray2); |
| 136 | + return (this.isNegativeNumber() == number.isNegativeNumber() && s1.equals(s2)); |
| 137 | + } |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * A method to add two arbitrary whole numbers and return the string representation of the result |
| 143 | + * @param value1 Any given value of type ArbitraryWholeNumbers |
| 144 | + * @param value2 Any given value of type ArbitraryWholeNumbers |
| 145 | + * @exception UnsupportedOperationException Throw this exception when given input numbers have different signs |
| 146 | + * @return A new object of type ArbitraryWholeNumbers that has all the numbers added up |
| 147 | + */ |
| 148 | + public ArbitraryWholeNumbers add (ArbitraryWholeNumbers value1, ArbitraryWholeNumbers value2) { |
| 149 | + // A variable to keep track of the carry |
| 150 | + int carry = 0; |
| 151 | + // A variable to set the length of the array |
| 152 | + int length = 0; |
| 153 | + // Remove Leading Zeros of both input values |
| 154 | + // Array1 is a new array of type int that is the digits of value1 that has been removed all leading zeros |
| 155 | + int[] newArray1 = removeLeadingZero(value1.digits); |
| 156 | + // Array2 is a new array of type int that is the digits of value2 that has been removed all leading zeros |
| 157 | + int[] newArray2 = removeLeadingZero(value2.digits); |
| 158 | + /* Setting length of resulting array to the maximum length between the two newArrays. |
| 159 | + */ |
| 160 | + if (newArray1.length > newArray2.length) |
| 161 | + length = newArray1.length + 1; |
| 162 | + else |
| 163 | + length = newArray2.length + 1; |
| 164 | + // An array of type int storing a new array with the same length as the result array for input value2's digits |
| 165 | + int[] array1 = new int[length]; |
| 166 | + // Copying all digits from value1's original digits array (with leading 0's removed) to this array (array1) |
| 167 | + for (int i = 0; i < newArray1.length; i++) { |
| 168 | + array1[i] = newArray1[i]; |
| 169 | + } |
| 170 | + // An array of type int storing a new array with the same length as the result array for input value2's digits |
| 171 | + int[] array2 = new int[length]; |
| 172 | + // Copying all digits from value1's original digits array (with leading 0's removed) to this array (array1) |
| 173 | + for (int i = 0; i < newArray2.length; i++) { |
| 174 | + array2[i] = newArray2[i]; |
| 175 | + } |
| 176 | + // An array storing the resulting array from adding |
| 177 | + int[] result = new int[length]; |
| 178 | + // A variable storing index of result's digits array |
| 179 | + int i = 0; |
| 180 | + // A temporary variable storing the sum of the addition of each corresponding pair of digits |
| 181 | + int s = 0; |
| 182 | + // If the values for adding have different signs, throw an UnsupportedOperationException |
| 183 | + if (value1.isNegativeNumber() == value2.isNegativeNumber()) { |
| 184 | + /* A loop that adds every corresponding pair of digits from the two arrays and stores them in the result array |
| 185 | + * Condition: index must be less than or equal to the result array's length */ |
| 186 | + while (i < result.length) { |
| 187 | + s = array1[i] + array2[i] + carry; |
| 188 | + if (s >= 10) { |
| 189 | + carry = 1; |
| 190 | + result[i] = s - 10; |
| 191 | + } |
| 192 | + else { |
| 193 | + carry = 0; |
| 194 | + result[i] = s; |
| 195 | + } |
| 196 | + i++; |
| 197 | + } |
| 198 | + } |
| 199 | + else |
| 200 | + throw new UnsupportedOperationException("Invalid inputs"); |
| 201 | + // A whole number object storing the output of the addition, which will also be returned |
| 202 | + ArbitraryWholeNumbers output = new ArbitraryWholeNumbers(false, result); |
| 203 | + // If the inputs are negative, the result must also have isNegative set to true |
| 204 | + if (value1.isNegativeNumber()) |
| 205 | + output.isNegative = true; |
| 206 | + return output; |
| 207 | + } |
| 208 | +} |
0 commit comments