├── ArbitraryFloatingPointNumbers.java ├── ArbitraryWholeNumbers.java ├── Bid.java ├── ComplexNumber.java ├── ComplexNumberInterface.java ├── Contract.java ├── Contractor.java ├── Date.java ├── GaussianInteger.java ├── GaussianIntegerInterface.java ├── HW3Tester.java ├── IntegerNumber.java ├── IntegerNumberInterface.java ├── NaturalNumber.java ├── NaturalNumberInterface.java ├── README.md ├── RationalNumber.java ├── RationalNumberInterface.java ├── RealNumber.java └── RealNumberInterface.java /ArbitraryFloatingPointNumbers.java: -------------------------------------------------------------------------------- 1 | /** 2 | * A class that represents Arbitrary Floating Point Numbers 3 | * @author David Nguyen 4 | * @since 11/14/2022 5 | */ 6 | public class ArbitraryFloatingPointNumbers { 7 | 8 | // An int field that indicates the precision of the current Arbitrary Floating Point Number 9 | private int precision; 10 | // A boolean field that indicates whether the current Arbitrary Floating Point Number is negative or not 11 | private boolean isNegative; 12 | // An array of type int that stores the digits of a Arbitrary Floating Point Number 13 | private int[] digits; 14 | 15 | /** 16 | * Creates an Arbitrary Floating Point Number. 17 | * Assigns the class's fields the values that the constructor got when a Arbitrary Floating Point Number object is created. 18 | * @param precision An integer indicating how many digits to the right of the decimal point that the number has 19 | * @param isNegative A boolean indicating whether the current number is negative or not 20 | * @param digits An array that represents the digits of an arbitrary floating point number 21 | */ 22 | public ArbitraryFloatingPointNumbers(int precision, boolean isNegative, int[] digits) { 23 | this.precision = precision; 24 | this.isNegative = isNegative; 25 | this.digits = digits; 26 | } 27 | 28 | /** 29 | * Creates an Arbitrary Floating Point Number that has precision of 0 (a whole number). 30 | * Assigns the class's fields the values that the constructor got when a Arbitrary Floating Point Number object is created. 31 | * @param isNegative A boolean indicating whether the current number is negative or not 32 | * @param digits An array that represents the digits of an arbitrary floating point number 33 | */ 34 | public ArbitraryFloatingPointNumbers(boolean isNegative, int[] digits) { 35 | this.precision = 0; 36 | this.isNegative = isNegative; 37 | this.digits = digits; 38 | } 39 | 40 | /** 41 | * Determines whether the current arbitrary floating point number is negative or not. 42 | * Returns true if the current arbitrary floating point number is negative. 43 | * Returns false if the current arbitrary floating point number is NOT negative. 44 | * @return A boolean that indicates whether the current number is negative. 45 | */ 46 | public boolean isNegativeNumber() { 47 | if (isNegative) 48 | return true; 49 | else 50 | return false; 51 | } 52 | 53 | /** 54 | * Returns the precision of the current arbitrary floating point number 55 | * @return An integer that shows the precision of the arbitrary floating point number 56 | */ 57 | public int getPrecision() { 58 | return precision; 59 | } 60 | 61 | /** 62 | * Returns an array that has no leading zeros. 63 | * @param array Any given array 64 | * @return An array that has no leading zeros 65 | */ 66 | public int[] removeLeadingZero(int[] array) { 67 | // A variable that stores the numbers of leading zeros 68 | int numLeadingZero = 0; 69 | // A variable that stores whether the current digit in the array is a non-zero 70 | boolean isNonZero = false; 71 | // A variable storing the index for the following 'while' loop 72 | int i = array.length - 1; 73 | /* A loop that determines the number of leading zeros. 74 | * The nonZero variable will also be set when the first non-zero is met to indicates the end of loop 75 | * Condition: The index must be more than or equal to 0 and the isNonZero variable must be FALSE 76 | */ 77 | while (i >= 0 && !isNonZero) { 78 | if (array[i] == 0) { 79 | numLeadingZero++; 80 | isNonZero = false; 81 | i--; 82 | } 83 | else 84 | isNonZero = true; 85 | } 86 | // A variable that stores the length of the new array 87 | int newLength = array.length - numLeadingZero; 88 | // An array of type int that stores the new array 89 | int[] newArray = new int[newLength]; 90 | // A variable storing the index of the old (given) array 91 | int index1 = array.length - numLeadingZero - 1; 92 | // A variable storing the index of the new array 93 | int index2 = newArray.length - 1; 94 | /* A loop that copies every element from old, original array to the new array 95 | * Condition: index of both arrays must be more than or equal to 0 96 | */ 97 | while (index1 >= 0 && index2 >= 0) { 98 | newArray[index2] = array[index1]; 99 | index1--; 100 | index2--; 101 | } 102 | return newArray; 103 | } 104 | 105 | /** 106 | * Converts a Arbitrary Floating Point Number object into its string representation 107 | * @return A String that is the string representation of the given Arbitrary Floating Point Number 108 | */ 109 | public String toString() { 110 | // A variable (object) of type StringBuilder that stores the output, resulting string 111 | StringBuilder result = new StringBuilder(); 112 | // Append '-' sign to the beginning of the number if the number is negative 113 | if (this.isNegative == true) { 114 | result.append('-'); 115 | } 116 | /* A new array of type int that stores the digits of the current arbitrary floating point number after removing 117 | * all leading zeros 118 | */ 119 | int[] array = removeLeadingZero(digits); 120 | /* Append each of the number in the digits array to the string representation of the number based on 3 cases: 121 | * If the length of the array is greater than current number's precision, append each digit normally 122 | * If the length of the array is equal to current number's precision, append 0. in front of number and then append 123 | * each digits normally. 124 | * If the length of the array is less than current number's precision, append 0. in front of number until the first 125 | * number in digits array is reached and then append each digits normally. 126 | */ 127 | if (array.length > this.precision) { 128 | for (int i = (array.length - 1); i >= 0; i--) { 129 | result.append(array[i]); 130 | if (i == (getPrecision())) { 131 | result.append('.'); 132 | } 133 | } 134 | } 135 | else if (array.length == this.getPrecision()) { 136 | result.append('0').append('.'); 137 | for (int i = (array.length - 1); i >= 0; i--) { 138 | result.append(array[i]); 139 | } 140 | } 141 | else { 142 | result.append('0').append('.'); 143 | for (int i = 0; i < (this.getPrecision() - array.length); i++) { 144 | result.append('0'); 145 | } 146 | for (int i = (array.length - 1); i >= (this.getPrecision() - array.length - 1); i--) { 147 | if (array[i] != '0') 148 | result.append(array[i]); 149 | } 150 | } 151 | return result.toString(); 152 | } 153 | 154 | /** 155 | * A helper method that converts all arrays to strings 156 | * @param array Any given arrays of type int 157 | * @return A String that is the string representation of the given array 158 | */ 159 | public String arrayToString(int[] array) { 160 | // A variable of type StringBuilder that stores the output string 161 | StringBuilder output = new StringBuilder(); 162 | /* A loop that appends every element of the given array into the output string 163 | * Condition: index must be less than length of array */ 164 | for (int i = 0; i < array.length; i++) { 165 | if (array[i] != 0) 166 | output.append(array[i]); 167 | } 168 | return output.toString(); 169 | } 170 | 171 | /** 172 | * An overridden equals method that determines if two floating point numbers are equal to each other 173 | * This can be done by comparing the contents of the two string representations of the two numbers 174 | * @param o Any given object 175 | * @return Returning true or false depending on these numbers' equality 176 | */ 177 | public boolean equals(Object o) { 178 | if (o instanceof ArbitraryFloatingPointNumbers) { 179 | // Typecasting given object to ArbitraryFloatingPointNumbers 180 | ArbitraryFloatingPointNumbers number = (ArbitraryFloatingPointNumbers) o; 181 | // A variable storing the string representation of the given ArbitraryFloatingPointNumbers 182 | String s1 = arrayToString(number.digits); 183 | // A variable storing the string representation of this ArbitraryFloatingPointNumbers 184 | String s2 = arrayToString(this.digits); 185 | return (this.isNegativeNumber() == number.isNegativeNumber() && s1.equals(s2)); 186 | } 187 | return false; 188 | } 189 | 190 | /** 191 | * A helper method that helps determine the number of digits to the left of the decimal point of any Arbitrary 192 | * Floating Point Numbers 193 | * @param value Any given ArbitraryFloatingPointNumbers-type object 194 | * @return An integer that represents the number of digits to the left of the decimal point of the given number 195 | */ 196 | public int leftDecimals(ArbitraryFloatingPointNumbers value) { 197 | // A new array of type int that stores the given value's digits array but after being removed all leading zeros 198 | int[] newArray = removeLeadingZero(value.digits); 199 | return newArray.length - value.precision; 200 | } 201 | 202 | /** 203 | * A method to add two arbitrary whole numbers and return the string representation of the result 204 | * @param value1 Any given value of type ArbitraryFloatingPointNumbers 205 | * @param value2 Any given value of type ArbitraryFloatingPointNumbers 206 | * @exception UnsupportedOperationException Throw this exception when given input numbers have different signs 207 | * @return A new object of type ArbitraryFloatingPointNumbers that has all the numbers added up 208 | */ 209 | public ArbitraryFloatingPointNumbers add(ArbitraryFloatingPointNumbers value1, ArbitraryFloatingPointNumbers value2) { 210 | // A variable to keep track of the carry 211 | int carry = 0; 212 | // A variable to set the length of the array 213 | int length = 0; 214 | // Remove Leading Zeros of both input values 215 | // Array1 is a new array of type int that is the digits of value1 that has been removed all leading zeros 216 | int[] newArray1 = removeLeadingZero(value1.digits); 217 | // Array2 is a new array of type int that is the digits of value2 that has been removed all leading zeros 218 | int[] newArray2 = removeLeadingZero(value2.digits); 219 | /* Setting length of resulting array to be one more than the sum of the maximum number of digits to the left of the 220 | * decimal point of value1 and value2 and the maximum digits to the right of the decimal point of value1 and value2. 221 | */ 222 | if (leftDecimals(value1) >= leftDecimals(value2) && value1.getPrecision() >= value2.getPrecision()) 223 | length = leftDecimals(value1) + value1.precision + 1; 224 | else if (leftDecimals(value1) >= leftDecimals(value2) && value1.getPrecision() <= value2.getPrecision()) 225 | length = leftDecimals(value1) + value2.precision + 1; 226 | else if (leftDecimals(value1) <= leftDecimals(value2) && value1.getPrecision() >= value2.getPrecision()) 227 | length = leftDecimals(value2) + value1.precision + 1; 228 | else if (leftDecimals(value1) <= leftDecimals(value2) && value1.getPrecision() <= value2.getPrecision()) 229 | length = leftDecimals(value2) + value2.precision + 1; 230 | /* A new array with the same length as the result array to store input value1's digits, after being removed all 231 | * leading zeros and put in right position (by calling the helper method above) */ 232 | int[] array1 = new int[length]; 233 | /* A new array with the same length as the result array to store input value2's digits, after being removed all 234 | * leading zeros and put in right position (by calling the helper method above) */ 235 | int[] array2 = new int[length]; 236 | // A variable storing the precision of the resulting value 237 | int precisionOutput = 0; 238 | // A temporary variable for storing the placement for adding 239 | int numPlacement = 0; 240 | // Setting the precision of the output number to the equal to the greater precision value of the two inputs 241 | // Re-ordering the digits arrays so that their decimal points line up with each other 242 | /* If precision of value1 is greater than value2's, then add the digits of value2 to the newer array2 array so that 243 | * its decimal point must line up with value1's decimal point */ 244 | if (value1.getPrecision() > value2.getPrecision()) { 245 | precisionOutput = value1.getPrecision(); 246 | numPlacement = value1.getPrecision() - value2.getPrecision(); 247 | // A loop that puts every element of the original digits array of value1 to the newer array1 to prepare number for adding 248 | for (int i = 0; i < newArray1.length; i++) { 249 | array1[i] = newArray1[i]; 250 | } 251 | // Variables as indexes for the following 'while' loop 252 | int i = numPlacement; 253 | int j = 0; 254 | // A loop that puts every element of the original digits array of value2 to the newer array2 to prepare number for adding 255 | while (i < array2.length && j < newArray2.length) { 256 | array2[i] = newArray2[j]; 257 | i++; 258 | j++; 259 | } 260 | } 261 | // Otherwise, do the same thing as above but in reverse order 262 | else { 263 | precisionOutput = value2.getPrecision(); 264 | numPlacement = value2.getPrecision() - value1.getPrecision(); 265 | // A loop that puts every element of value2's digits array to the newer array2 to prepare number for adding 266 | for (int i = 0; i < newArray2.length; i++) { 267 | array2[i] = newArray2[i]; 268 | } 269 | // Variables that serve as indexes for the following 'while' loop 270 | int i = numPlacement; 271 | int j = 0; 272 | // A loop that puts every element of value1's digits array to the newer array1 to prepare number for adding 273 | while (i < array1.length && j < newArray1.length) { 274 | array1[i] = newArray1[j]; 275 | i++; 276 | j++; 277 | } 278 | } 279 | // An array of type int that stores the result of the addition 280 | int[] result = new int[length]; 281 | // A variable storing index for adding 282 | int i = 0; 283 | // A temporary variable storing the sum of the addition of each corresponding pair of digits 284 | int s = 0; 285 | // If the values for adding have different signs, throw an UnsupportedOperationException 286 | if ((value1.isNegativeNumber() == value2.isNegativeNumber())) { 287 | /* A loop that adds every corresponding pair of digits from the two arrays and stores them in the result array 288 | * Condition: index must be less than or equal to the result array's length */ 289 | while (i < result.length) { 290 | s = array1[i] + array2[i] + carry; 291 | if (s >= 10) { 292 | carry = 1; 293 | result[i] = s - 10; 294 | } 295 | else { 296 | carry = 0; 297 | result[i] = s; 298 | } 299 | i++; 300 | } 301 | } 302 | else 303 | throw new UnsupportedOperationException("Invalid inputs"); 304 | // An arbitrary floating point number storing the output of the addition 305 | ArbitraryFloatingPointNumbers output = new ArbitraryFloatingPointNumbers(precisionOutput, false, result); 306 | /* If the inputs are negative, the result must also have isNegative set to true 307 | * Otherwise, it will be kept as false */ 308 | if (value1.isNegativeNumber()) 309 | output.isNegative = true; 310 | return output; 311 | } 312 | } -------------------------------------------------------------------------------- /ArbitraryWholeNumbers.java: -------------------------------------------------------------------------------- 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 | } 209 | -------------------------------------------------------------------------------- /Bid.java: -------------------------------------------------------------------------------- 1 | //David Nguyen - The class Bid represents a bid that a contractor makes for a contract// 2 | public class Bid{ 3 | //The contract field represents the contract name for the bid 4 | Contract contract; 5 | //The contractor field represents the contractor's name 6 | Contractor contractor; 7 | //The value field represents the value of the bid 8 | Double value; 9 | 10 | //A constructor that creates a bid// 11 | public Bid (Contract contract, Contractor contractor, double value) { 12 | this.contract = contract; 13 | this.contractor = contractor; 14 | this.value = value; 15 | } 16 | 17 | //A method that returns the contract for the bid 18 | public Contract contract() { 19 | return contract; 20 | } 21 | 22 | //A method that returns the contractor for the bid 23 | public Contractor contractor() { 24 | return this.contractor; 25 | } 26 | 27 | //A method that returns the value of the bid// 28 | public double value() { 29 | return this.value; 30 | } 31 | } -------------------------------------------------------------------------------- /ComplexNumber.java: -------------------------------------------------------------------------------- 1 | /** 2 | * A class that represents Complex Numbers 3 | * @author David Nguyen 4 | * @since 11/14/2022 5 | */ 6 | public class ComplexNumber implements ComplexNumberInterface{ 7 | 8 | // A field representing the real part 9 | private ArbitraryFloatingPointNumbers real; 10 | // A field representing the imaginary part 11 | private ArbitraryFloatingPointNumbers imaginary; 12 | 13 | /** 14 | * Creates a Complex Number. 15 | * Assigns the class's fields the values that the constructor got when a ComplexNumber object is created. 16 | * @param real An ArbitraryFloatingPointNumbers that represents a real part 17 | * @param imaginary An ArbitraryFloatingPointNumbers that represents an imaginary part 18 | */ 19 | public ComplexNumber (ArbitraryFloatingPointNumbers real, ArbitraryFloatingPointNumbers imaginary) { 20 | this.real = real; 21 | this.imaginary = imaginary; 22 | } 23 | 24 | /** 25 | * A method that retrieves the real part of the complex number. 26 | * @return A floating point number that represents the real part of a complex number 27 | */ 28 | public ArbitraryFloatingPointNumbers getRealPart() { 29 | return real; 30 | } 31 | 32 | /** 33 | * A method that retrieves the imaginary part of the complex number. 34 | * @return ArbitraryFloatingPointNumbers A floating point number that represents the imaginary part of a complex number 35 | */ 36 | public ArbitraryFloatingPointNumbers getImaginaryPart() { 37 | return imaginary; 38 | } 39 | 40 | /** 41 | * A method that retrieves the imaginary part of the complex number. 42 | * @param number Any ComplexNumber object that is given as input 43 | * @return A Complex Number that is the result of the addition between this ComplexNumber and the input number 44 | */ 45 | public ComplexNumber add(ComplexNumberInterface number) { 46 | ArbitraryFloatingPointNumbers newReal = this.getRealPart().add(this.getRealPart(), number.getRealPart()); 47 | ArbitraryFloatingPointNumbers newImaginary = this.getImaginaryPart().add(this.getImaginaryPart(), number.getImaginaryPart()); 48 | ComplexNumber result = new ComplexNumber (newReal, newImaginary); 49 | return result; 50 | } 51 | 52 | /** 53 | * A method that compares any two complex numbers. 54 | * @param value1 Any ComplexNumber object that is given as input 55 | * @param value2 Any ComplexNumber object that is given as input 56 | * @return A boolean (true or false) depending on equality of these two numbers 57 | */ 58 | public boolean equals(ComplexNumber value1, ComplexNumber value2) { 59 | return (value1.getRealPart().equals(value2.getRealPart())) && (value1.getImaginaryPart().equals(getImaginaryPart())); 60 | } 61 | 62 | /** 63 | * A method that returns the string representation of this complex number. 64 | * @return A String that is the proper string representation of the complex number 65 | */ 66 | public String toString() { 67 | String output = new String(); 68 | if (!getImaginaryPart().isNegativeNumber()) { 69 | output = (getRealPart() + " + " + getImaginaryPart() + "i"); 70 | } 71 | else 72 | output = (getRealPart() + " " + getImaginaryPart() + "i"); 73 | return output; 74 | } 75 | } -------------------------------------------------------------------------------- /ComplexNumberInterface.java: -------------------------------------------------------------------------------- 1 | /** 2 | * An interface that represents Complex Numbers 3 | * @author David Nguyen 4 | * @since 11/14/2022 5 | */ 6 | public interface ComplexNumberInterface { 7 | 8 | /** 9 | * An abstract method that retrieves the real part of the complex number. 10 | * @return A floating point number that represents the real part of the complex number 11 | */ 12 | ArbitraryFloatingPointNumbers getRealPart(); 13 | 14 | /** 15 | * An abstract method that retrieves the imaginary part of the complex number 16 | * @return A floating point number that represents the imaginary part of the complex number 17 | */ 18 | ArbitraryFloatingPointNumbers getImaginaryPart(); 19 | } -------------------------------------------------------------------------------- /Contract.java: -------------------------------------------------------------------------------- 1 | //David Nguyen - A class that represents a contract 2 | public class Contract { 3 | //The id field represents the ID of the contract 4 | String contractID; 5 | //The minValue field represents the minimum value of the contract 6 | double minValue; 7 | //The maxValue field represents the maximum value of the contract 8 | double maxValue; 9 | //The bonus field represents the per-day bonus associated with the contract 10 | double bonus; 11 | //The penalty field represents the per-day penalty associated with the contract 12 | double penalty; 13 | //The deadline field represents the deadline for the contract 14 | Date deadline; 15 | //The bidAccept field stores whether the contract is still accepting bids or not 16 | boolean bidAccept; 17 | //The bestBid field stores the best bid so far for the contract 18 | Bid bestBid; 19 | //The complete field stores whether the contract is completed or not 20 | boolean complete; 21 | //The completedDay stores the day on which the contract is completed 22 | Date completedDate; 23 | 24 | //A constructor that creates a Contract// 25 | public Contract (String contractID, double minValue, double maxValue, double bonus, double penalty, Date deadline) { 26 | this.contractID = contractID; 27 | this.minValue = minValue; 28 | this.maxValue = maxValue; 29 | this.bonus = bonus; 30 | this.penalty = penalty; 31 | this.deadline = deadline; 32 | this.bidAccept = true; 33 | this.bestBid = null; 34 | this.complete = false; 35 | this.completedDate = null; 36 | } 37 | 38 | //A method that gets the ID of the contract 39 | public String getID() { 40 | return this.contractID; 41 | } 42 | 43 | //A method that gets the minimum value of the contract 44 | public double getMinValue() { 45 | return this.minValue; 46 | } 47 | 48 | //A method that sets the minimum value for the contract 49 | public void setMinValue (double value) { 50 | this.minValue = value; 51 | } 52 | 53 | //A method that gets the maximum value of the contract 54 | public double getMaxValue() { 55 | return this.maxValue; 56 | } 57 | 58 | //A method that sets the maximum value for the contract 59 | public void setMaxValue(double value) { 60 | this.maxValue = value; 61 | } 62 | 63 | //A method that gest the per-day bonus associated with the contract 64 | public double getBonus() { 65 | return this.bonus; 66 | } 67 | 68 | //A method that sets the per-day bonus 69 | public void setBonus(double bonus) { 70 | this.bonus = bonus; 71 | } 72 | 73 | //A method that gets the per-day penalty 74 | public double getPenalty() { 75 | return this.penalty; 76 | } 77 | 78 | //A method that sets the per-day penalty 79 | public void setPenalty (double penalty) { 80 | this.penalty = penalty; 81 | } 82 | 83 | //A method that gets the deadline for the contract 84 | public Date getDeadline() { 85 | return this.deadline; 86 | } 87 | 88 | //A method that sets the deadline for the contract 89 | public void setDeadline(Date deadline) { 90 | this.deadline = deadline; 91 | } 92 | 93 | //A method that returns true if a contractor may submit a bid for the contract 94 | public boolean isAcceptingBids() { 95 | return this.bidAccept; 96 | } 97 | 98 | //A method that returns the best bid so far for the contract. It returns null if there've been no acceptable bids 99 | public Bid getBestBid() { 100 | if (bidAccept == true) 101 | return this.bestBid; 102 | else 103 | return null; 104 | } 105 | 106 | /*A method that returns true if the bid is accepted and becomes the current best bid. However, it returns false 107 | if the bid is not accepted*/ 108 | public boolean makeBid (Bid bid) { 109 | Bid bestBidSoFar = getBestBid(); 110 | if (bidAccept == false){ 111 | return false; 112 | } 113 | else 114 | if (this.minValue <= bid.value && bid.value <= this.maxValue) { 115 | if (bestBidSoFar == null) { 116 | this.bestBid = bestBidSoFar; 117 | return true; 118 | } 119 | else { 120 | if (bid != bestBidSoFar) { 121 | this.bestBid = bestBidSoFar; 122 | return true; 123 | } 124 | } 125 | } 126 | return false; 127 | } 128 | 129 | //A method that indicates that the contract is no longer accepting bid 130 | public void awardContract () { 131 | this.bidAccept = false; 132 | } 133 | 134 | //A method that returns true if the contract has been completed, or false if not 135 | public boolean isComplete () { 136 | return complete; 137 | } 138 | 139 | //A method that returns the date on which the contract was completed, returns null if the contract is not completed 140 | public Date completeDate () { 141 | if (this.complete = true) { 142 | return this.completedDate; 143 | } 144 | else { 145 | return null; 146 | } 147 | } 148 | 149 | /*A method that sets the contract to completed, sets the completed date for the contract, 150 | and pay the contractor of the best bid the required amount, which is the sum of the best bid & penalty or bonus*/ 151 | public void setComplete (Date completedDate) { 152 | this.completedDate = completedDate; 153 | this.complete = true; 154 | //A variable that stores the amount to be paid to the contractor// 155 | double amountPay = getBestBid().value; 156 | int daysLateOrBeforeDeadline = Date.difference(getDeadline(), completedDate); 157 | //An if-statement that determines the amount to be paid to the contractor based on bonus & bid & penalty 158 | if (daysLateOrBeforeDeadline > 0) { 159 | double penalty = getPenalty() * daysLateOrBeforeDeadline; 160 | amountPay = amountPay + penalty; 161 | } 162 | if (daysLateOrBeforeDeadline < 0) { 163 | double bonus = getBonus() * daysLateOrBeforeDeadline; 164 | amountPay = amountPay - bonus; 165 | } 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /Contractor.java: -------------------------------------------------------------------------------- 1 | //David Nguyen - A class representing a contractor, including their name, address, contact person, and balance// 2 | public class Contractor { 3 | 4 | //A field representing the contractor's name// 5 | private String name; 6 | //A field representing the contractor's address 7 | private String address; 8 | //A field representing the contractors' contact person 9 | private String contact; 10 | //A field representing the balance of the contractor 11 | private double balance; 12 | //A field representing the amount paid to the contractor 13 | private double amount; 14 | 15 | //A constructor that creates a contractor// 16 | public Contractor (String name, String address, String contact) { 17 | this.name = name; 18 | this.address = address; 19 | this.contact = contact; 20 | } 21 | 22 | //A method that returns the name of the contractor// 23 | public String getName () { 24 | return this.name; 25 | } 26 | //A method that sets the name of the contractor// 27 | //The variable name represents the desired name that will be set to the contractor// 28 | public void setName (String name) { 29 | this.name = name; 30 | } 31 | 32 | //A method that returns the address of the contractor 33 | public String getAddress () { 34 | return this.address; 35 | } 36 | 37 | //A method that sets the address of the contractor// 38 | //The variable address represents the desired address that will be set to the contractor// 39 | public void setAddress (String address) { 40 | this.address = address; 41 | } 42 | 43 | //A method that returns the contact person of the contractor// 44 | public String getContact () { 45 | return this.contact; 46 | } 47 | 48 | //A method that sets the name of the contact person of the contractor 49 | //The variable name represents the name of the contact person of the contractor 50 | public void setContact (String contact) { 51 | this.contact = contact; 52 | } 53 | 54 | //A method that returns the amount that has been paid to the contractor 55 | public double getAmountPaid () { 56 | return this.amount; 57 | } 58 | 59 | //A method that pays the contractor the specified amount 60 | //The variable amount represents the specified amount paid to the contractor 61 | public void pay (double amount) { 62 | this.amount = this.amount + amount; 63 | this.balance = this.balance + this.amount; 64 | } 65 | 66 | //A method that override the inherited toString method to represent the contractor name, address, and contact 67 | public String toString() { 68 | return getName() + ", " + getAddress() + ", " + getContact(); 69 | } 70 | 71 | /* A method that override the inherited equals method to determine if the two contractor instances are equal 72 | (if their names are equal)*/ 73 | public boolean equals(Object o) { 74 | if (o instanceof Contractor) { 75 | Contractor c = (Contractor)o; 76 | if (this.getName() == c.getName()) 77 | return true; 78 | else 79 | return false; 80 | } 81 | return false; 82 | } 83 | } -------------------------------------------------------------------------------- /Date.java: -------------------------------------------------------------------------------- 1 | //David Nguyen - Date class: A class that represents a date (With Extra Credit)// 2 | public class Date { 3 | 4 | //A field that represents the day of the date// 5 | private int day; 6 | //A field that represents the month of the date// 7 | private Month month; 8 | //A field that represents the year of the date// 9 | private int year; 10 | //A field that represents the current date// 11 | private String date; 12 | //A variable storing whether the date is being displayed in a US format 13 | boolean isInUSFormat; 14 | 15 | //Extra-Credit Attempt: An enum type that represents the months in a year from January to December 16 | public enum Month { 17 | //Declaring the maximum number of days for each month 18 | JAN(31), 19 | FEB(28), 20 | MAR(31), 21 | APR(30), 22 | MAY(31), 23 | JUN(30), 24 | JUL(31), 25 | AUG(31), 26 | SEP(30), 27 | OCT(31), 28 | NOV(30), 29 | DEC(31); 30 | 31 | //A variable that stores the maximum number of days of each month 32 | private final int numDays; 33 | 34 | //A constructor that sets the maximum number of days of each month 35 | Month(int numDays) { 36 | this.numDays = numDays; 37 | } 38 | 39 | //A method that returns the maximum number of days of each month 40 | public int getNumDayOfMonth(Month month) { 41 | return this.numDays; 42 | } 43 | 44 | //A method returning the number of days between December 31st of previous year and the 1st of the selected month 45 | public static int dayOfYear(Month month) { 46 | //A variable that stores the number of days between December 31st of previous year and the 1st of this month. 47 | int numDaysFromDec31; 48 | //Calculating the number of days from December 31st of previous year and the 1st of this month 49 | if (month == JAN) 50 | numDaysFromDec31 = 1; 51 | else if (month == FEB) 52 | numDaysFromDec31 = 1 + 31; 53 | else if (month == MAR) 54 | numDaysFromDec31 = 1 + 31 + 28; 55 | else if (month == APR) 56 | numDaysFromDec31 = 1 + 31 + 28 + 31; 57 | else if (month == MAY) 58 | numDaysFromDec31 = 1 + 31 + 28 + 31 + 30; 59 | else if (month == JUN) 60 | numDaysFromDec31 = 1 + 31 + 28 + 31 + 30 + 31; 61 | else if (month == JUL) 62 | numDaysFromDec31 = 1 + 31 + 28 + 31 + 30 + 31 + 30; 63 | else if (month == AUG) 64 | numDaysFromDec31 = 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31; 65 | else if (month == SEP) 66 | numDaysFromDec31 = 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31; 67 | else if (month == OCT) 68 | numDaysFromDec31 = 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30; 69 | else if (month == NOV) 70 | numDaysFromDec31 = 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31; 71 | else 72 | numDaysFromDec31 = 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30; 73 | return numDaysFromDec31; 74 | } 75 | } 76 | 77 | //A constructor that creates a Date object with the int inputs being the day, month, and year of the date// 78 | public Date (int day, Month month, int year) { 79 | this.day = day; 80 | this.month = month; 81 | this.year = year; 82 | this.date = this.month + "/" + this.day + "/" + this.year; 83 | this.isInUSFormat = false; 84 | } 85 | 86 | //A method that returns the number of days between this Date and January 1 of the same year// 87 | public int daysFromJan1() { 88 | //A variable that stores the number of days from Jan 1 89 | int numberOfDays; 90 | //A statement that adds the number of days that have passed from Jan 1 to the current date to calculate days from Jan 1// 91 | numberOfDays = Month.dayOfYear(this.month) + this.day; 92 | return numberOfDays; 93 | } 94 | 95 | /*A method that returns the day of the date. If the day falls outside the range 0-31, 96 | the day is invalid and program returns 00*/ 97 | public int getDay () { 98 | if (1 < this.day && this.day < 31) 99 | return this.day; 100 | else 101 | return 0; 102 | } 103 | 104 | /*A method that returns the month of the date*/ 105 | public Month getMonth() { 106 | return this.month; 107 | } 108 | 109 | //A method that returns the year of the date// 110 | public int getYear () { 111 | return this.year; 112 | } 113 | 114 | //A method that sets the date to the US date formatting. If the input is true, change to US Formatting and vice versa 115 | public void setUSFormat(boolean useUSFormat) { 116 | if (useUSFormat == false) { 117 | this.date = this.date; 118 | } 119 | else { 120 | this.date = this.month + "/" + this.day + "/" + this.year; 121 | } 122 | } 123 | 124 | //A method that checks whether the date is being displayed in a US format// 125 | public boolean isUSFormat() { 126 | /*An if-statement that returns true & set the value of the variable isInUSFormat to true if the date is displayed 127 | in the US Format and vice versa*/ 128 | if (this.date == this.month + "/" + this.day + "/" + this.year) { 129 | isInUSFormat = true; 130 | } 131 | else { 132 | isInUSFormat = false; 133 | } 134 | return isInUSFormat; 135 | } 136 | 137 | //A method that takes 2 dates and returns the difference in days between the two// 138 | public static int difference(Date date1, Date date2) { 139 | //A variable that stores the difference in terms of year between the two dates// 140 | int yearDifference = date1.year - date2.year; 141 | //A variable that stores the difference in days between the two given dates// 142 | int totalDays = 0; 143 | //A variable that stores the difference in terms of days between the two dates in the same year// 144 | int dayDifference = date1.daysFromJan1() - date2.daysFromJan1(); 145 | /*An if-statement that calculates the difference in terms of days between the two days*/ 146 | if (yearDifference < 0) 147 | totalDays = (yearDifference * 365) + dayDifference; 148 | else 149 | totalDays = (yearDifference * 365) + dayDifference; 150 | return totalDays; 151 | } 152 | 153 | //A method that override the inherited toString() method to display the date based on the desired format// 154 | public String toString() { 155 | if (isInUSFormat == true) { 156 | return (this.getMonth() + " " + this.getDay() + ", " + this.getYear()); 157 | } 158 | else { 159 | return (this.getDay() + " " + this.getMonth() + ", " + this.getYear()); 160 | } 161 | } 162 | 163 | /*A method that override the inherited equals() method to compare the two date instances. 164 | The two dates are equal if they have the same day, month, year values, and vice versa*/ 165 | public boolean equals(Object o) { 166 | if (o instanceof Date) { 167 | Date d = (Date)o; 168 | if (this.getDay() == d.getDay() && this.getMonth() == d.getMonth() && this.getYear() == d.getYear()) 169 | return true; 170 | else 171 | return false; 172 | } 173 | return false; 174 | } 175 | } -------------------------------------------------------------------------------- /GaussianInteger.java: -------------------------------------------------------------------------------- 1 | /** 2 | * A class that represents Gaussian Integers 3 | * @author David Nguyen 4 | * @since 11/14/2022 5 | */ 6 | public class GaussianInteger implements GaussianIntegerInterface { 7 | 8 | //A field representing the real part 9 | private ArbitraryWholeNumbers real; 10 | //A field representing the imaginary part 11 | private ArbitraryWholeNumbers imaginary; 12 | 13 | /** 14 | * Creates a Gaussian Integer. 15 | * Assigns the class's fields the values that the constructor got when a GaussianInteger object is created. 16 | * @param real An ArbitraryWholeNumbers that represents a real part 17 | * @param imaginary An ArbitraryWholeNumbers that represents an imaginary part 18 | */ 19 | public GaussianInteger(ArbitraryWholeNumbers real, ArbitraryWholeNumbers imaginary) { 20 | this.real = real; 21 | this.imaginary = imaginary; 22 | } 23 | 24 | /** 25 | * A method that retrieves the real part of a gaussian integer. 26 | * @return A whole number that represents the real part of a gaussian integer 27 | */ 28 | public ArbitraryWholeNumbers getRealPart() { 29 | return real; 30 | } 31 | 32 | /** 33 | * A method that retrieves the imaginary part of a gaussian integer. 34 | * @return A whole number that represents the imagainary part of a gaussian integer 35 | */ 36 | public ArbitraryWholeNumbers getImaginaryPart() { 37 | return imaginary; 38 | } 39 | 40 | /** 41 | * A method that retrieves the imaginary part of the complex number. 42 | * @param number Any GaussianInteger object that is given as input 43 | * @return A Gaussian Integer that is the result of the addition between this Gaussian Integer and the input number 44 | */ 45 | public GaussianInteger add(GaussianIntegerInterface number) { 46 | ArbitraryWholeNumbers newReal = this.getRealPart().add(this.getRealPart(), number.getRealPart()); 47 | ArbitraryWholeNumbers newImaginary = this.getImaginaryPart().add(this.getImaginaryPart(), number.getImaginaryPart()); 48 | GaussianInteger result = new GaussianInteger (newReal, newImaginary); 49 | return result; 50 | } 51 | 52 | /** 53 | * A method that compares any two gaussian integers. 54 | * @param value1 Any GaussianInteger object that is given as input 55 | * @param value2 Any GaussianInteger object that is given as input 56 | * @return A boolean (true or false) depending on equality of these two numbers 57 | */ 58 | public boolean equals (GaussianIntegerInterface value1, GaussianIntegerInterface value2) { 59 | return (value1.getRealPart().equals(value2.getRealPart())) && (value1.getImaginaryPart().equals(getImaginaryPart())); 60 | } 61 | 62 | /** 63 | * A method that returns the string representation of this gaussian integer. 64 | * @return A String that is the proper string representation of the gaussian integer 65 | */ 66 | public String toString() { 67 | String output = new String(); 68 | if (!getImaginaryPart().isNegativeNumber()) { 69 | output = (getRealPart() + " + " + getImaginaryPart() + "i"); 70 | } 71 | else 72 | output = (getRealPart() + " " + getImaginaryPart() + "i"); 73 | return output; 74 | } 75 | } -------------------------------------------------------------------------------- /GaussianIntegerInterface.java: -------------------------------------------------------------------------------- 1 | /** 2 | * An interface that represents Gaussian Integers 3 | * @author David Nguyen 4 | * @since 11/14/2022 5 | */ 6 | public interface GaussianIntegerInterface extends ComplexNumberInterface { 7 | 8 | /** 9 | * An abstract method that retrieves the real part of the gaussian integers. 10 | * @return A whole number that represents the real part of the gaussian integers 11 | */ 12 | ArbitraryWholeNumbers getRealPart(); 13 | 14 | /** 15 | * An abstract method that retrieves the imaginary part of the gaussian integers. 16 | * @return A whole number that represents the imaginary part of the gaussian integers 17 | */ 18 | ArbitraryWholeNumbers getImaginaryPart(); 19 | } -------------------------------------------------------------------------------- /HW3Tester.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import org.junit.jupiter.api.Test; 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | import static org.junit.jupiter.api.Assertions.assertTrue; 5 | 6 | /** 7 | * A test class for all methods in HW3. 8 | * All methods have been tested, test cases and other information for the tests are described in the comments below. 9 | * @author David Nguyen 10 | * @since 11/14/2022 11 | */ 12 | public class HW3Tester { 13 | 14 | /** 15 | * Testing the ArbitraryFloatingPointNumbers class: 16 | */ 17 | 18 | /** 19 | * Testing the getPrecision() method with 2 cases: 20 | */ 21 | @Test 22 | public void testGetPrecision() { 23 | int[] a1 = new int[] {9,5,1,4,1,3}; 24 | ArbitraryFloatingPointNumbers value1 = new ArbitraryFloatingPointNumbers (0, true, a1); 25 | int[] a2 = new int[] {9,5,1,4,1,3,1}; 26 | ArbitraryFloatingPointNumbers value2 = new ArbitraryFloatingPointNumbers (1, false, a2); 27 | int[] a3 = new int[] {9,5,1,4,1,3,1,4}; 28 | ArbitraryFloatingPointNumbers value3 = new ArbitraryFloatingPointNumbers (4, false, a3); 29 | // Case 1: Test if this number’s Precision is 0 30 | assertEquals(0, value1.getPrecision()); 31 | // Case 2: Test if this number’s Precision is 1 32 | assertEquals(1, value2.getPrecision()); 33 | // Case 3: Test if this number’s Precision is more than 1 (4) 34 | assertEquals(4, value3.getPrecision()); 35 | } 36 | 37 | /** 38 | * Testing the isNegativeNumber() method with 2 cases: 39 | */ 40 | @Test 41 | public void testIsNegativeFloatingNumber() { 42 | int[] a1 = new int[] {9,5,1,4,1,3}; 43 | ArbitraryFloatingPointNumbers value1 = new ArbitraryFloatingPointNumbers (5, true, a1); 44 | int[] a2 = new int[] {9,5,1,4,1,3,1}; 45 | ArbitraryFloatingPointNumbers value2 = new ArbitraryFloatingPointNumbers (5, false, a2); 46 | // Case 1: Test if this number is negative 47 | assertEquals(true, value1.isNegativeNumber()); 48 | // Case 2: Test if this number is not negative 49 | assertEquals(false, value2.isNegativeNumber()); 50 | } 51 | 52 | /** 53 | * Testing the toString() method with 6 cases: 54 | */ 55 | @Test 56 | public void testToStringFloatingPoint() { 57 | int[] a1 = new int[] {6,7,1}; 58 | ArbitraryFloatingPointNumbers value1 = new ArbitraryFloatingPointNumbers (4, true, a1); 59 | int[] a2 = new int[] {0,0,0,3,4,5,6,0,0,0}; 60 | ArbitraryFloatingPointNumbers value2 = new ArbitraryFloatingPointNumbers (5, true, a2); 61 | int[] a3 = new int[] {6,7,1}; 62 | ArbitraryFloatingPointNumbers value3 = new ArbitraryFloatingPointNumbers (3, true, a3); 63 | int[] a4 = new int[] {6,7,1}; 64 | ArbitraryFloatingPointNumbers value4 = new ArbitraryFloatingPointNumbers (4, false, a4); 65 | int[] a5 = new int[] {0,0,0,3,4,5,6,0,0,0}; 66 | ArbitraryFloatingPointNumbers value5 = new ArbitraryFloatingPointNumbers (5, false, a5); 67 | int[] a6 = new int[] {6,7,1}; 68 | ArbitraryFloatingPointNumbers value6 = new ArbitraryFloatingPointNumbers (3, false, a6); 69 | // Case 1: Test if this number is negative with its precision greater than its digits array’s length 70 | assertEquals("-0.0176", value1.toString()); 71 | // Case 2: Test if this number is negative with its precision smaller than its digits array’s length 72 | assertEquals("-65.43000", value2.toString()); 73 | // Case 3: Test if this number is negative with its precision equal to its digits array’s length 74 | assertEquals("-0.176", value3.toString()); 75 | // Case 4: Test if this number is not negative with its precision greater than its digits array’s length 76 | assertEquals("0.0176", value4.toString()); 77 | // Case 5: Test if this number is not negative with its precision smaller than its digits array’s length 78 | assertEquals("65.43000", value5.toString()); 79 | // Case 6: Test if this number is not negative with its precision equal to its digits array’s length 80 | assertEquals("0.176", value6.toString()); 81 | } 82 | 83 | /** 84 | * Testing the removeLeadingZero() method with 6 cases: 85 | */ 86 | @Test 87 | public void testRemoveLeadingZeroFloatingPoint() { 88 | int[] a1 = new int[] { 0, 0, 0, 3, 4, 5, 6 }; 89 | int[] a2 = new int[] { 0, 0, 0, 3, 4, 5, 6, 0 }; 90 | int[] a3 = new int[] { 0, 0, 0, 3, 4, 5, 6, 0, 0, 0 }; 91 | int[] a4 = new int[] { 0, 3, 4, 5, 6 }; 92 | int[] a5 = new int[] { 3, 4, 5, 6, 0 }; 93 | int[] a6 = new int[] { 3, 4, 5, 0, 6}; 94 | int[] a7 = new int[] { 3, 4, 5, 6 }; 95 | ArbitraryFloatingPointNumbers value1 = new ArbitraryFloatingPointNumbers (4, true, a1); 96 | // Case 1: Test 0 - test if the given digits array contains no leading zero 97 | assertEquals(true, Arrays.equals(value1.removeLeadingZero(a1), a1)); 98 | // Case 2: Test 1 - test if the given digits array contains 1 leading zero 99 | assertEquals(true, Arrays.equals(value1.removeLeadingZero(a2), a1)); 100 | // Case 3: Test many - test if the given digits array contains many leading zeros 101 | assertEquals(true, Arrays.equals(value1.removeLeadingZero(a3), a1)); 102 | // Case 4: Test first - test if the first digit of the given digits array is 0 103 | assertEquals(true, Arrays.equals(value1.removeLeadingZero(a4), a4)); 104 | // Case 5: Test last - test if the last digit of the given digits array is 0 105 | assertEquals(true, Arrays.equals(value1.removeLeadingZero(a5), a7)); 106 | // Case 6: Test middle - test if the middle digits of the given digits array is 0 107 | assertEquals(true, Arrays.equals(value1.removeLeadingZero(a6), a6)); 108 | } 109 | 110 | /** 111 | * Testing the leftDecimals() method with 3 cases: 112 | */ 113 | @Test 114 | public void testLeftDecimals() { 115 | int[] a1 = new int[] {6,7,1}; 116 | ArbitraryFloatingPointNumbers value1 = new ArbitraryFloatingPointNumbers (3, true, a1); 117 | int[] a2 = new int[] {0,0,0,3,4,5,6}; 118 | ArbitraryFloatingPointNumbers value2 = new ArbitraryFloatingPointNumbers (6, true, a2); 119 | int[] a3 = new int[] {0,0,0,3,4,5,6}; 120 | ArbitraryFloatingPointNumbers value3 = new ArbitraryFloatingPointNumbers (4, true, a3); 121 | // Case 1: Test if the number of digits to the left of floating point is 0 122 | assertEquals(0, value1.leftDecimals(value1)); 123 | // Case 2: Test if the number of digits to the left of floating point is 1 124 | assertEquals(1, value1.leftDecimals(value2)); 125 | // Case 3: Test if the number of digits to the left of floating point is 3 126 | assertEquals(3, value1.leftDecimals(value3)); 127 | } 128 | 129 | /** 130 | * Testing the arrayToString() method with 2 cases: 131 | */ 132 | @Test 133 | public void testArrayToStringFloatingPoint() { 134 | int[] a1 = new int[] { 1 }; 135 | int[] a2 = new int[] { 0, 3, 4, 5, 6 }; 136 | ArbitraryFloatingPointNumbers value1 = new ArbitraryFloatingPointNumbers (4, false, a1); 137 | // Case 1: Test if a given number to be converted into a string contains 1 digit 138 | assertEquals("1", value1.arrayToString(a1)); 139 | // Case 2: Test if a given number to be converted into a string contains many digits 140 | assertEquals("3456", value1.arrayToString(a2)); 141 | } 142 | 143 | /** 144 | * Testing the equals() method with 2 cases: 145 | */ 146 | @Test 147 | public void testEqualsFloatingPoint() { 148 | int[] a1 = new int[] {9,5,1,4,1,3,1,4}; 149 | ArbitraryFloatingPointNumbers value1 = new ArbitraryFloatingPointNumbers (5, false, a1); 150 | int[] a2 = new int[] {6,7,1}; 151 | ArbitraryFloatingPointNumbers value2 = new ArbitraryFloatingPointNumbers (3, true, a2); 152 | int[] a3 = new int[] {9,5,1,4,1,3,1,4}; 153 | ArbitraryFloatingPointNumbers value3 = new ArbitraryFloatingPointNumbers (5, false, a3); 154 | // Case 1: Test if this number and the number to be compared is not equal: 155 | assertEquals(false, value1.equals(value2)); 156 | // Case 2: Test if this number and the number to be compared is equal to each other: 157 | assertEquals(true, value1.equals(value3)); 158 | } 159 | 160 | /** 161 | * Testing the add() method with 6 cases: 162 | */ 163 | @Test 164 | public void testAddFloatingPointNumbers() { 165 | // Case 1: Test if the addition of a positive number with another number with a bigger precision: 166 | int[] a1 = new int[] {3,5,1}; 167 | ArbitraryFloatingPointNumbers value1 = new ArbitraryFloatingPointNumbers (2, false, a1); 168 | int[] a2 = new int[] {5,8,7,2,1}; 169 | ArbitraryFloatingPointNumbers value2 = new ArbitraryFloatingPointNumbers (4, false, a2); 170 | int[] a3 = new int[] {5,8,0,8,2}; 171 | ArbitraryFloatingPointNumbers value3 = new ArbitraryFloatingPointNumbers (4, false, a3); 172 | assertEquals(true, value1.add(value1,value2).equals(value3)); 173 | // Case 2: Test if the addition of a positive number with another number with a smaller precision: 174 | int[] a4 = new int[] {5,8,7,2,1}; 175 | ArbitraryFloatingPointNumbers value4 = new ArbitraryFloatingPointNumbers (4, false, a4); 176 | int[] a5 = new int[] {3,5,1}; 177 | ArbitraryFloatingPointNumbers value5 = new ArbitraryFloatingPointNumbers (2, false, a5); 178 | int[] a6 = new int[] {5,8,0,8,2}; 179 | ArbitraryFloatingPointNumbers value6 = new ArbitraryFloatingPointNumbers (4, false, a6); 180 | assertEquals(true, value4.add(value4,value5).equals(value6)); 181 | // Case 3: Test if the addition of two positive numbers with equal precisions 182 | int[] a7 = new int[] {5,3,1}; 183 | ArbitraryFloatingPointNumbers value7 = new ArbitraryFloatingPointNumbers (2, false, a7); 184 | int[] a8 = new int[] {3,5,1}; 185 | ArbitraryFloatingPointNumbers value8 = new ArbitraryFloatingPointNumbers (2, false, a8); 186 | int[] a9 = new int[] {8,8,2}; 187 | ArbitraryFloatingPointNumbers value9 = new ArbitraryFloatingPointNumbers (2, false, a9); 188 | assertEquals(true, value7.add(value7,value8).equals(value9)); 189 | // Case 4: Test if the addition of a negative number with another number with a bigger precision 190 | int[] a10 = new int[] {3,5,1}; 191 | ArbitraryFloatingPointNumbers value10 = new ArbitraryFloatingPointNumbers (2, true, a10); 192 | int[] a11 = new int[] {5,8,7,2,1}; 193 | ArbitraryFloatingPointNumbers value11 = new ArbitraryFloatingPointNumbers (4, true, a11); 194 | int[] a12 = new int[] {5,8,0,8,2}; 195 | ArbitraryFloatingPointNumbers value12 = new ArbitraryFloatingPointNumbers (4, true, a12); 196 | assertEquals(true, value10.add(value10,value11).equals(value12)); 197 | // Case 5: Test if the addition of a negative number with another number with a smaller precision 198 | int[] a13 = new int[] {5,8,7,2,1}; 199 | ArbitraryFloatingPointNumbers value13 = new ArbitraryFloatingPointNumbers (4, true, a13); 200 | int[] a14 = new int[] {3,5,1}; 201 | ArbitraryFloatingPointNumbers value14 = new ArbitraryFloatingPointNumbers (2, true, a14); 202 | int[] a15 = new int[] {5,8,0,8,2}; 203 | ArbitraryFloatingPointNumbers value15 = new ArbitraryFloatingPointNumbers (4, true, a15); 204 | assertEquals(true, value14.add(value13,value14).equals(value15)); 205 | // Case 6: Test if the addition of two negative numbers with equal precisions 206 | int[] a16 = new int[] {5,3,1}; 207 | ArbitraryFloatingPointNumbers value16 = new ArbitraryFloatingPointNumbers (2, true, a16); 208 | int[] a17 = new int[] {3,5,1}; 209 | ArbitraryFloatingPointNumbers value17 = new ArbitraryFloatingPointNumbers (2, true, a17); 210 | int[] a18 = new int[] {8,8,2}; 211 | ArbitraryFloatingPointNumbers value18 = new ArbitraryFloatingPointNumbers (2, true, a18); 212 | assertEquals(true, value17.add(value17,value16).equals(value18)); 213 | } 214 | 215 | /** 216 | * Case 7 of the add() method testing: Test if the inputs with different signs will throw an Unsupported Operation Exception: 217 | */ 218 | @Test 219 | public void testIfAddThrowsExceptionFloatingPoint() { 220 | int[] a1 = new int[] {7,9,0,2,4}; 221 | ArbitraryFloatingPointNumbers value = new ArbitraryFloatingPointNumbers (true, a1); 222 | int[] a2 = new int[] {3,1,4}; 223 | ArbitraryFloatingPointNumbers value1 = new ArbitraryFloatingPointNumbers (false, a2); 224 | boolean thrown = false; 225 | try { 226 | value.add(value, value1); 227 | } 228 | catch (UnsupportedOperationException e) { 229 | thrown = true; 230 | } 231 | assertEquals(true, thrown); 232 | } 233 | 234 | /** 235 | * Testing ArbitraryWholeNumbers methods: 236 | */ 237 | 238 | /** 239 | * Testing isNegativeNumber() method: 240 | */ 241 | @Test 242 | public void testIsNegativeWholeNumber() { 243 | int[] a1 = new int[] {9,5,1,4,1,3}; 244 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (true, a1); 245 | int[] a2 = new int[] {9,5,1,4,1,3,1}; 246 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 247 | // Case 1: Testing when given number is negative 248 | assertEquals(true, value1.isNegativeNumber()); 249 | // Case 2: Testing when given number is positive 250 | assertEquals(false, value2.isNegativeNumber()); 251 | } 252 | 253 | /** 254 | * Testing toString() method: 255 | */ 256 | @Test 257 | public void testToStringWholeNumber() { 258 | int[] a1 = new int[] {6,7,1}; 259 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (true, a1); 260 | int[] a2 = new int[] {0,0,0,3,4,5,6,0}; 261 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (true, a2); 262 | int[] a3 = new int[] {0,0,0,3,4,5,6,0,0,0}; 263 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (true, a3); 264 | int[] a4 = new int[] {6,7,1}; 265 | ArbitraryWholeNumbers value4 = new ArbitraryWholeNumbers (false, a4); 266 | int[] a5 = new int[] {0,0,0,3,4,5,6,0}; 267 | ArbitraryWholeNumbers value5 = new ArbitraryWholeNumbers (false, a5); 268 | int[] a6 = new int[] {0,0,0,3,4,5,6,0,0,0}; 269 | ArbitraryWholeNumbers value6 = new ArbitraryWholeNumbers (false, a6); 270 | // Case 1: Test if this number is negative with 0 leading zero 271 | assertEquals("-176", value1.toString()); 272 | // Case 2: Test if this number is negative with 1 leading zero 273 | assertEquals("-6543000", value2.toString()); 274 | // Case 3: Test if this number is negative with many leading zeros 275 | assertEquals("-6543000", value3.toString()); 276 | // Case 4: Test if this number is not negative with 0 leading zero 277 | assertEquals("176", value4.toString()); 278 | // Case 5: Test if this number is not negative with 1 leading zero 279 | assertEquals("6543000", value5.toString()); 280 | // Case 6: Test if this number is not negative with many leading zeros 281 | assertEquals("6543000", value6.toString()); 282 | } 283 | 284 | /** 285 | * Testing the removeLeadingZero() method with 6 cases: 286 | */ 287 | @Test 288 | public void testRemoveLeadingZeroWholeNumber() { 289 | int[] a1 = new int[] { 0, 0, 0, 3, 4, 5, 6 }; 290 | int[] a2 = new int[] { 0, 0, 0, 3, 4, 5, 6, 0 }; 291 | int[] a3 = new int[] { 0, 0, 0, 3, 4, 5, 6, 0, 0, 0 }; 292 | int[] a4 = new int[] { 0, 3, 4, 5, 6 }; 293 | int[] a5 = new int[] { 3, 4, 5, 6, 0 }; 294 | int[] a6 = new int[] { 3, 4, 5, 0, 6}; 295 | int[] a7 = new int[] { 3, 4, 5, 6 }; 296 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 297 | // Case 1: Test 0 - test if the given digits array contains no leading zero 298 | assertEquals(true, Arrays.equals(value1.removeLeadingZero(a1), a1)); 299 | // Case 2: Test 1 - test if the given digits array contains 1 leading zero 300 | assertEquals(true, Arrays.equals(value1.removeLeadingZero(a2), a1)); 301 | // Case 3: Test many - test if the given digits array contains many leading zeros 302 | assertEquals(true, Arrays.equals(value1.removeLeadingZero(a3), a1)); 303 | // Case 4: Test first - test if the first digit of the given digits array is 0 304 | assertEquals(true, Arrays.equals(value1.removeLeadingZero(a4), a4)); 305 | // Case 5: Test last - test if the last digit of the given digits array is 0 306 | assertEquals(true, Arrays.equals(value1.removeLeadingZero(a5), a7)); 307 | // Case 6: Test middle - test if the middle digits of the given digits array is 0 308 | assertEquals(true, Arrays.equals(value1.removeLeadingZero(a6), a6)); 309 | } 310 | 311 | /** 312 | * Testing the arrayToString() method with 2 cases: 313 | */ 314 | @Test 315 | public void testArrayToStringWholeNumber() { 316 | int[] a1 = new int[] { 1 }; 317 | int[] a2 = new int[] { 0, 3, 4, 5, 6 }; 318 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 319 | // Case 1: Test if a given number to be converted into a string contains 1 digit 320 | assertEquals("1", value1.arrayToString(a1)); 321 | // Case 2: Test if a given number to be converted into a string contains many digits 322 | assertEquals("3456", value1.arrayToString(a2)); 323 | } 324 | 325 | /** 326 | * Testing the equals() method with 2 cases: 327 | */ 328 | @Test 329 | public void testEqualsWholeNumber() { 330 | int[] a1 = new int[] {9,5,1,4,1,3,1,4}; 331 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 332 | int[] a2 = new int[] {6,7,1}; 333 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (true, a2); 334 | int[] a3 = new int[] {9,5,1,4,1,3,1,4}; 335 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (false, a3); 336 | // Case 1: Test if this number and the number to be compared is not equal 337 | assertEquals(false, value1.equals(value2)); 338 | // Case 2: Test if this number and the number to be compared is equal to each other 339 | assertEquals(true, value1.equals(value3)); 340 | } 341 | 342 | /** 343 | * Testing the add() method with 2 cases: 344 | */ 345 | @Test 346 | public void testAddWholeNumbers() { 347 | // Case 1: Test if the addition of a positive number with another positive number can produce a correct result 348 | int[] a1 = new int[] {4,1,5,8}; 349 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 350 | int[] a2 = new int[] {0,0,0,3,4,5,6}; 351 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 352 | int[] a3 = new int[] {4,1,5,1,5,5,6}; 353 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (false, a3); 354 | assertEquals(true, value1.add(value1,value2).equals(value3)); 355 | // Case 2: Test if the addition of a negative number with another negative number can produce a correct result 356 | int[] a4 = new int[] {9,5,9}; 357 | ArbitraryWholeNumbers value4 = new ArbitraryWholeNumbers (true, a4); 358 | int[] a5 = new int[] {9,6,9}; 359 | ArbitraryWholeNumbers value5 = new ArbitraryWholeNumbers (true, a5); 360 | int[] a6 = new int[] {8,2,9,1}; 361 | ArbitraryWholeNumbers value6 = new ArbitraryWholeNumbers (true, a6); 362 | assertEquals(true, value4.add(value4,value5).equals(value6)); 363 | } 364 | 365 | /** 366 | * Case 3 of the add() method testing: Test if the inputs with different signs will throw an Unsupported Operation Exception: 367 | */ 368 | @Test 369 | public void testIfAddWholeNumbersThrowsException() { 370 | int[] a1 = new int[] {7,9,0,2,4}; 371 | ArbitraryWholeNumbers value = new ArbitraryWholeNumbers (true, a1); 372 | int[] a2 = new int[] {3,1,4}; 373 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a2); 374 | boolean thrown = false; 375 | try { 376 | value.add(value, value1); 377 | } 378 | catch (UnsupportedOperationException e) { 379 | thrown = true; 380 | } 381 | assertEquals(true, thrown); 382 | } 383 | 384 | /** 385 | * Testing the ComplexNumber class: 386 | */ 387 | 388 | /** 389 | * Testing the getRealPart() method with 2 cases: 390 | */ 391 | @Test 392 | public void testGetRealPartComplex() { 393 | // Case 1: Test if the method correctly returns the real part (which, in this case, is NEGATIVE) of the complex number 394 | int[] a1 = new int[] {2,3,5,1,3}; 395 | ArbitraryFloatingPointNumbers value1 = new ArbitraryFloatingPointNumbers (4, true, a1); 396 | int[] a2 = new int[] {3,2,1,7,6}; 397 | ArbitraryFloatingPointNumbers value2 = new ArbitraryFloatingPointNumbers (3, false, a2); 398 | ComplexNumber c = new ComplexNumber(value1, value2); 399 | assertEquals(true, c.getRealPart().equals(value1)); 400 | // Case 2: Test if the method correctly returns the real part (which, in this case, is POSITIVE) of the complex number 401 | int[] a3 = new int[] {2,3,5,1,3}; 402 | ArbitraryFloatingPointNumbers value3 = new ArbitraryFloatingPointNumbers (4, false, a3); 403 | int[] a4 = new int[] {3,2,1,7,6}; 404 | ArbitraryFloatingPointNumbers value4 = new ArbitraryFloatingPointNumbers (3, false, a4); 405 | ComplexNumber d = new ComplexNumber(value3, value4); 406 | assertEquals(true, d.getRealPart().equals(value3)); 407 | } 408 | 409 | /** 410 | * Testing the getImaginaryPart() method with 2 cases: 411 | */ 412 | @Test 413 | public void testGetImaginaryPartComplex() { 414 | // Case 1: Test if the method correctly returns the imaginary part (which, in this case, is POSITIVE) of the complex number 415 | int[] a1 = new int[] {2,3,5,1,3}; 416 | ArbitraryFloatingPointNumbers value1 = new ArbitraryFloatingPointNumbers (4, false, a1); 417 | int[] a2 = new int[] {3,2,1,7,6}; 418 | ArbitraryFloatingPointNumbers value2 = new ArbitraryFloatingPointNumbers (3, false, a2); 419 | ComplexNumber c = new ComplexNumber(value1, value2); 420 | assertEquals(true, c.getImaginaryPart().equals(value2)); 421 | // Case 2: Test if the method correctly returns the real part (which, in this case, is NEGATIVE) of the complex number 422 | int[] a3 = new int[] {2,3,5,1,3}; 423 | ArbitraryFloatingPointNumbers value3 = new ArbitraryFloatingPointNumbers (4, false, a3); 424 | int[] a4 = new int[] {3,2,1,7,6}; 425 | ArbitraryFloatingPointNumbers value4 = new ArbitraryFloatingPointNumbers (3, true, a4); 426 | ComplexNumber d = new ComplexNumber(value3, value4); 427 | assertEquals(true, d.getImaginaryPart().equals(value4)); 428 | } 429 | 430 | /** 431 | * Testing the add() method with 4 cases: 432 | */ 433 | @Test 434 | public void testAddComplexNumbers() { 435 | /* Case 1: Test if the addition between a complex number with a positive real part and positive imaginary part and 436 | another complex number with a positive real part and positive imaginary part */ 437 | int[] a1 = new int[] {2,3,5,1,3}; 438 | ArbitraryFloatingPointNumbers value1 = new ArbitraryFloatingPointNumbers (4, false, a1); 439 | int[] a2 = new int[] {3,2,1,7,6}; 440 | ArbitraryFloatingPointNumbers value2 = new ArbitraryFloatingPointNumbers (3, false, a2); 441 | ComplexNumber c = new ComplexNumber(value1, value2); 442 | int[] a3 = new int[] {3,0,0,0,3}; 443 | ArbitraryFloatingPointNumbers value3 = new ArbitraryFloatingPointNumbers (4, false, a3); 444 | int[] a4 = new int[] {4,6,9,5}; 445 | ArbitraryFloatingPointNumbers value4 = new ArbitraryFloatingPointNumbers (3, false, a4); 446 | ComplexNumber d = new ComplexNumber(value3, value4); 447 | int[] a5 = new int[] {5,3,5,1,6}; 448 | ArbitraryFloatingPointNumbers value5 = new ArbitraryFloatingPointNumbers (4, false, a5); 449 | int[] a6 = new int[] {7,8,0,3,7}; 450 | ArbitraryFloatingPointNumbers value6 = new ArbitraryFloatingPointNumbers (3, false, a6); 451 | ComplexNumber e = new ComplexNumber(value5, value6); 452 | assertEquals(true, ((c.add(d).toString()).equals(e.toString()))); 453 | /* Case 2: Test if the addition between a complex number with a positive real part and negative imaginary part and 454 | * another complex number with a positive real part and negative imaginary part */ 455 | int[] a7 = new int[] {2,3,5,1,3}; 456 | ArbitraryFloatingPointNumbers value7 = new ArbitraryFloatingPointNumbers (4, false, a7); 457 | int[] a8 = new int[] {3,2,1,7,6}; 458 | ArbitraryFloatingPointNumbers value8 = new ArbitraryFloatingPointNumbers (3, true, a8); 459 | ComplexNumber f = new ComplexNumber(value7, value8); 460 | int[] a9 = new int[] {2,3,5,1,3}; 461 | ArbitraryFloatingPointNumbers value9 = new ArbitraryFloatingPointNumbers (4, false, a9); 462 | int[] a10 = new int[] {7,2,0,4}; 463 | ArbitraryFloatingPointNumbers value10 = new ArbitraryFloatingPointNumbers (2, true, a10); 464 | ComplexNumber g = new ComplexNumber(value9, value10); 465 | int[] a11 = new int[] {4,6,0,3,6}; 466 | ArbitraryFloatingPointNumbers value11 = new ArbitraryFloatingPointNumbers (4, false, a11); 467 | int[] a12 = new int[] {3,9,3,7,0,1}; 468 | ArbitraryFloatingPointNumbers value12 = new ArbitraryFloatingPointNumbers (3, true, a12); 469 | ComplexNumber h = new ComplexNumber(value11, value12); 470 | assertEquals(true, ((f.add(g).toString()).equals(h.toString()))); 471 | /* Case 3: Test if the addition between a complex number with a negative real part and positive imaginary 472 | * part and another complex number with a negative real part and positive imaginary part */ 473 | int[] a13 = new int[] {7,9,0,2,4}; 474 | ArbitraryFloatingPointNumbers value13 = new ArbitraryFloatingPointNumbers (4, true, a13); 475 | int[] a14 = new int[] {9,9,9,9}; 476 | ArbitraryFloatingPointNumbers value14 = new ArbitraryFloatingPointNumbers (3, false, a14); 477 | ComplexNumber i = new ComplexNumber(value13, value14); 478 | int[] a15 = new int[] {2,2,5}; 479 | ArbitraryFloatingPointNumbers value15 = new ArbitraryFloatingPointNumbers (2, true, a15); 480 | int[] a16 = new int[] {7,7,7,7,8}; 481 | ArbitraryFloatingPointNumbers value16 = new ArbitraryFloatingPointNumbers (4, false, a16); 482 | ComplexNumber j = new ComplexNumber(value15, value16); 483 | int[] a17 = new int[] {7,9,2,4,9}; 484 | ArbitraryFloatingPointNumbers value17 = new ArbitraryFloatingPointNumbers (4, true, a17); 485 | int[] a18 = new int[] {7,6,7,7,8,1}; 486 | ArbitraryFloatingPointNumbers value18 = new ArbitraryFloatingPointNumbers (4, false, a18); 487 | ComplexNumber k = new ComplexNumber(value17, value18); 488 | assertEquals(true, ((i.add(j).toString()).equals(k.toString()))); 489 | /* Case 4: Test if the addition between a complex number with a negative real part and negative imaginary part and 490 | * another complex number with a negative real part and negative imaginary part */ 491 | int[] a19 = new int[] {7,9,0,2,4}; 492 | ArbitraryFloatingPointNumbers value19 = new ArbitraryFloatingPointNumbers (4, true, a19); 493 | int[] a20 = new int[] {9,9,9,9}; 494 | ArbitraryFloatingPointNumbers value20 = new ArbitraryFloatingPointNumbers (3, true, a20); 495 | ComplexNumber l = new ComplexNumber(value7, value8); 496 | int[] a21 = new int[] {2,2,5}; 497 | ArbitraryFloatingPointNumbers value21 = new ArbitraryFloatingPointNumbers (4, true, a21); 498 | int[] a22 = new int[] {7,7,7,7,8}; 499 | ArbitraryFloatingPointNumbers value22 = new ArbitraryFloatingPointNumbers (2, true, a22); 500 | ComplexNumber m = new ComplexNumber(value9, value10); 501 | int[] a23 = new int[] {7,9,2,4,9}; 502 | ArbitraryFloatingPointNumbers value23 = new ArbitraryFloatingPointNumbers (4, true, a23); 503 | int[] a24 = new int[] {7,6,7,7,8,1}; 504 | ArbitraryFloatingPointNumbers value24 = new ArbitraryFloatingPointNumbers (3, true, a24); 505 | ComplexNumber n = new ComplexNumber(value11, value12); 506 | assertEquals(true, ((l.add(m).toString()).equals(n.toString()))); 507 | // Case 5: Test if adding in hierarchy can produce a correct number 508 | // Test if adding a complex number with an integer number can produce a correct result 509 | int[] a25 = new int[] {7,9,7,2,7}; 510 | ArbitraryWholeNumbers value25 = new ArbitraryWholeNumbers (false, a25); 511 | int[] a26 = new int[] {7,9,7,2,7}; 512 | ArbitraryWholeNumbers value26 = new ArbitraryWholeNumbers (false, a26); 513 | ComplexNumber complexNumber = new ComplexNumber (value25, value26); 514 | IntegerNumber integerNumber = new IntegerNumber (value25); 515 | int[] a27 = new int[] {4,9,5,5,4,1}; 516 | ArbitraryFloatingPointNumbers value27 = new ArbitraryFloatingPointNumbers (false, a27); 517 | int[] a28 = new int[] {7,9,7,2,7}; 518 | ArbitraryFloatingPointNumbers value28 = new ArbitraryFloatingPointNumbers (false, a28); 519 | ComplexNumber complexNumber2 = new ComplexNumber (value27, value28); 520 | assertEquals(true, ((complexNumber.add(integerNumber).toString()).equals(complexNumber2.toString()))); 521 | } 522 | 523 | /** 524 | * Case 6 of the add() method testing: Test if the inputs with different signs will throw an Unsupported Operation Exception: 525 | */ 526 | @Test 527 | public void testIfAddComplexThrowsException() { 528 | int[] a1 = new int[] {7,9,0,2,4}; 529 | ArbitraryWholeNumbers value = new ArbitraryWholeNumbers (true, a1); 530 | int[] a2 = new int[] {3,1,4}; 531 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a2); 532 | ComplexNumber c1 = new ComplexNumber (value, value1); 533 | int[] a3 = new int[] {7,2,9,5,6}; 534 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a3); 535 | int[] a4 = new int[] {5,6}; 536 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (true, a4); 537 | ComplexNumber c2 = new ComplexNumber (value2, value3); 538 | boolean thrown = false; 539 | try { 540 | c1.add(c2); 541 | } 542 | catch (UnsupportedOperationException e) { 543 | thrown = true; 544 | } 545 | assertEquals(true, thrown); 546 | } 547 | 548 | /** 549 | * Testing the equals() method with 2 cases: 550 | */ 551 | @Test 552 | public void testEqualsComplexNumbers() { 553 | // Case 1: Test if this complex number and the number to be compared is equal 554 | int[] a1 = new int[] {2,3,5,1,3}; 555 | ArbitraryFloatingPointNumbers value1 = new ArbitraryFloatingPointNumbers (4, true, a1); 556 | int[] a2 = new int[] {3,2,1,7,6}; 557 | ArbitraryFloatingPointNumbers value2 = new ArbitraryFloatingPointNumbers (3, false, a2); 558 | ComplexNumber c = new ComplexNumber(value1, value2); 559 | int[] a3 = new int[] {2,3,5,1,3}; 560 | ArbitraryFloatingPointNumbers value3 = new ArbitraryFloatingPointNumbers (4, true, a3); 561 | int[] a4 = new int[] {3,2,1,7,6}; 562 | ArbitraryFloatingPointNumbers value4 = new ArbitraryFloatingPointNumbers (3, false, a4); 563 | ComplexNumber d = new ComplexNumber(value3, value4); 564 | assertEquals(true, c.equals(c,d)); 565 | // Case 2: Test if this complex number and the number to be compared is not equal 566 | int[] a5 = new int[] {2,3,5,1,3}; 567 | ArbitraryFloatingPointNumbers value5 = new ArbitraryFloatingPointNumbers (4, true, a5); 568 | int[] a6 = new int[] {3,2,1,7}; 569 | ArbitraryFloatingPointNumbers value6 = new ArbitraryFloatingPointNumbers (3, false, a6); 570 | ComplexNumber e = new ComplexNumber(value5, value6); 571 | int[] a7 = new int[] {2,3,5,3}; 572 | ArbitraryFloatingPointNumbers value7 = new ArbitraryFloatingPointNumbers (4, true, a7); 573 | int[] a8 = new int[] {2,1,7}; 574 | ArbitraryFloatingPointNumbers value8 = new ArbitraryFloatingPointNumbers (3, false, a8); 575 | ComplexNumber f = new ComplexNumber(value7, value8); 576 | assertEquals(false, e.equals(e,f)); 577 | } 578 | 579 | /** 580 | * Testing the toString() method with 4 cases: 581 | */ 582 | @Test 583 | public void testToStringComplexNumbers() { 584 | // Case 1: Test if the complex number to be represented contains a positive real part and positive imaginary part 585 | int[] a5 = new int[] {2,3,5,1,3}; 586 | ArbitraryFloatingPointNumbers value5 = new ArbitraryFloatingPointNumbers (4, false, a5); 587 | int[] a6 = new int[] {3,2,1,7,6}; 588 | ArbitraryFloatingPointNumbers value6 = new ArbitraryFloatingPointNumbers (3, false, a6); 589 | ComplexNumber a = new ComplexNumber(value5, value6); 590 | assertEquals("3.1532 + 67.123i", a.toString()); 591 | // Case 2: Test if the complex number to be represented contains a negative real part and positive imaginary part 592 | int[] a1 = new int[] {2,3,5,1,3}; 593 | ArbitraryFloatingPointNumbers value1 = new ArbitraryFloatingPointNumbers (4, true, a1); 594 | int[] a2 = new int[] {3,2,1,7,6}; 595 | ArbitraryFloatingPointNumbers value2 = new ArbitraryFloatingPointNumbers (3, false, a2); 596 | ComplexNumber c = new ComplexNumber(value1, value2); 597 | assertEquals("-3.1532 + 67.123i", c.toString()); 598 | // Case 3: Test if the complex number to be represented contains a positive real part and negative imaginary part 599 | int[] a3 = new int[] {3,0,0,0,9}; 600 | ArbitraryFloatingPointNumbers value3 = new ArbitraryFloatingPointNumbers (4, false, a3); 601 | int[] a4 = new int[] {3,9,1,0}; 602 | ArbitraryFloatingPointNumbers value4 = new ArbitraryFloatingPointNumbers (3, true, a4); 603 | ComplexNumber d = new ComplexNumber(value3, value4); 604 | assertEquals("9.0003 -0.193i", d.toString()); 605 | // Case 4: Test if the complex number to be represented contains a negative real part and negative imaginary part 606 | int[] a7 = new int[] {2,3,5,1,3}; 607 | ArbitraryFloatingPointNumbers value7 = new ArbitraryFloatingPointNumbers (4, true, a7); 608 | int[] a8 = new int[] {3,2,1,7,6}; 609 | ArbitraryFloatingPointNumbers value8 = new ArbitraryFloatingPointNumbers (3, true, a8); 610 | ComplexNumber b = new ComplexNumber(value7, value8); 611 | assertEquals("-3.1532 -67.123i", b.toString()); 612 | } 613 | 614 | /** 615 | * Testing the methods of the Gaussian Integer class 616 | */ 617 | 618 | /** 619 | * Testing the getRealPart() method with 2 cases: 620 | */ 621 | @Test 622 | public void testGetRealPartGaussian() { 623 | // Case 1: Test if the method correctly returns the real part (which, in this case, is negative) of the gaussian integer 624 | int[] a1 = new int[] {2,3,5,1,3}; 625 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (true, a1); 626 | int[] a2 = new int[] {3,2,1,7,6}; 627 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 628 | GaussianInteger g = new GaussianInteger(value1, value2); 629 | assertEquals(true, g.getRealPart().equals(value1)); 630 | // Case 2: Test if the method correctly returns the real part (which, in this case, is positive) of the gaussian integer 631 | int[] a3 = new int[] {2,3,5,1,3}; 632 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (false, a3); 633 | int[] a4 = new int[] {3,2,1,7,6}; 634 | ArbitraryWholeNumbers value4 = new ArbitraryWholeNumbers (false, a4); 635 | GaussianInteger c = new GaussianInteger(value3, value4); 636 | assertEquals(true, c.getRealPart().equals(value3)); 637 | } 638 | 639 | /** 640 | * Testing the getImaginaryPart() method with 2 cases: 641 | */ 642 | @Test 643 | public void testGetImaginaryPartGaussian() { 644 | // Case 1: Test if the method correctly returns the imaginary part (which, in this case, is positive) of the gaussian integer 645 | int[] a1 = new int[] {2,3,5,1,3}; 646 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (true, a1); 647 | int[] a2 = new int[] {3,2,1,7,6}; 648 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 649 | GaussianInteger g = new GaussianInteger(value1, value2); 650 | assertEquals(true, g.getImaginaryPart().equals(value2)); 651 | // Case 2: Test if the method correctly returns the imaginary part (which, in this case, is negative) of the gaussian integer 652 | int[] a3 = new int[] {2,3,5,1,3}; 653 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (true, a3); 654 | int[] a4 = new int[] {3,2,1,7,6}; 655 | ArbitraryWholeNumbers value4 = new ArbitraryWholeNumbers (true, a4); 656 | GaussianInteger c = new GaussianInteger(value3, value4); 657 | assertEquals(true, c.getImaginaryPart().equals(value4)); 658 | } 659 | 660 | 661 | /** 662 | * Testing the add() method with 4 cases: 663 | */ 664 | @Test 665 | public void testAddGaussian() { 666 | /* Case 1: Test if the addition between a gaussian with a positive real part and positive imaginary part and 667 | another complex number with a positive real part and positive imaginary part */ 668 | int[] a1 = new int[] {2,3,5,1,3}; 669 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 670 | int[] a2 = new int[] {3,2,1,7,6}; 671 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 672 | GaussianInteger c = new GaussianInteger(value1, value2); 673 | int[] a3 = new int[] {3,0,0,0,3}; 674 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (false, a3); 675 | int[] a4 = new int[] {4,6,9,5}; 676 | ArbitraryWholeNumbers value4 = new ArbitraryWholeNumbers (false, a4); 677 | GaussianInteger d = new GaussianInteger(value3, value4); 678 | int[] a5 = new int[] {5,3,5,1,6}; 679 | ArbitraryWholeNumbers value5 = new ArbitraryWholeNumbers (false, a5); 680 | int[] a6 = new int[] {7,8,0,3,7}; 681 | ArbitraryWholeNumbers value6 = new ArbitraryWholeNumbers (false, a6); 682 | GaussianInteger e = new GaussianInteger(value5, value6); 683 | assertEquals(true, ((c.add(d).toString()).equals(e.toString()))); 684 | /* Case 2: Test if the addition between a gaussian with a positive real part and negative imaginary part and 685 | * another complex number with a positive real part and negative imaginary part */ 686 | int[] a7 = new int[] {2,3,5,1,3}; 687 | ArbitraryWholeNumbers value7 = new ArbitraryWholeNumbers (false, a7); 688 | int[] a8 = new int[] {3,2,1,7,6}; 689 | ArbitraryWholeNumbers value8 = new ArbitraryWholeNumbers (true, a8); 690 | GaussianInteger f = new GaussianInteger(value7, value8); 691 | int[] a9 = new int[] {2,3,5,1,3}; 692 | ArbitraryWholeNumbers value9 = new ArbitraryWholeNumbers (false, a9); 693 | int[] a10 = new int[] {7,2,0,4}; 694 | ArbitraryWholeNumbers value10 = new ArbitraryWholeNumbers (true, a10); 695 | GaussianInteger g = new GaussianInteger(value9, value10); 696 | int[] a11 = new int[] {4,6,0,3,6}; 697 | ArbitraryWholeNumbers value11 = new ArbitraryWholeNumbers (false, a11); 698 | int[] a12 = new int[] {0,5,1,1,7}; 699 | ArbitraryWholeNumbers value12 = new ArbitraryWholeNumbers (true, a12); 700 | GaussianInteger h = new GaussianInteger(value11, value12); 701 | assertEquals(true, ((f.add(g).toString()).equals(h.toString()))); 702 | /* Case 3: Test if the addition between a gaussian with a negative real part and positive imaginary 703 | * part and another complex number with a negative real part and positive imaginary part */ 704 | int[] a13 = new int[] {7,9,0,2,4}; 705 | ArbitraryWholeNumbers value13 = new ArbitraryWholeNumbers (true, a13); 706 | int[] a14 = new int[] {9,9,9,9}; 707 | ArbitraryWholeNumbers value14 = new ArbitraryWholeNumbers (false, a14); 708 | GaussianInteger i = new GaussianInteger(value13, value14); 709 | int[] a15 = new int[] {2,2,5}; 710 | ArbitraryWholeNumbers value15 = new ArbitraryWholeNumbers (true, a15); 711 | int[] a16 = new int[] {7,7,7,7,8}; 712 | ArbitraryWholeNumbers value16 = new ArbitraryWholeNumbers (false, a16); 713 | GaussianInteger j = new GaussianInteger(value15, value16); 714 | int[] a17 = new int[] {9,1,6,2,4}; 715 | ArbitraryWholeNumbers value17 = new ArbitraryWholeNumbers (true, a17); 716 | int[] a18 = new int[] {6,7,7,7,9}; 717 | ArbitraryWholeNumbers value18 = new ArbitraryWholeNumbers (false, a18); 718 | GaussianInteger k = new GaussianInteger(value17, value18); 719 | assertEquals(true, ((i.add(j).toString()).equals(k.toString()))); 720 | /* Case 4: Test if the addition between a gaussian with a negative real part and negative imaginary part and 721 | * another complex number with a negative real part and negative imaginary part */ 722 | int[] a19 = new int[] {7,9,0,2,4}; 723 | ArbitraryWholeNumbers value19 = new ArbitraryWholeNumbers (true, a19); 724 | int[] a20 = new int[] {9,9,9,9}; 725 | ArbitraryWholeNumbers value20 = new ArbitraryWholeNumbers (true, a20); 726 | GaussianInteger l = new GaussianInteger(value7, value8); 727 | int[] a21 = new int[] {2,2,5}; 728 | ArbitraryWholeNumbers value21 = new ArbitraryWholeNumbers (true, a21); 729 | int[] a22 = new int[] {7,7,7,7,8}; 730 | ArbitraryWholeNumbers value22 = new ArbitraryWholeNumbers (true, a22); 731 | GaussianInteger m = new GaussianInteger(value9, value10); 732 | int[] a23 = new int[] {9,1,6,2,4}; 733 | ArbitraryWholeNumbers value23 = new ArbitraryWholeNumbers (true, a23); 734 | int[] a24 = new int[] {6,7,7,7,9}; 735 | ArbitraryWholeNumbers value24 = new ArbitraryWholeNumbers (true, a24); 736 | GaussianInteger n = new GaussianInteger(value11, value12); 737 | assertEquals(true, ((l.add(m).toString()).equals(n.toString()))); 738 | // Case 5: Test if adding in hierarchy can produce a correct number 739 | // Test if adding a gaussian number with an integer number can produce a correct result 740 | int[] a25 = new int[] {7,9,7,2,7}; 741 | ArbitraryWholeNumbers value25 = new ArbitraryWholeNumbers (false, a25); 742 | int[] a26 = new int[] {7,9,7,2,7}; 743 | ArbitraryWholeNumbers value26 = new ArbitraryWholeNumbers (false, a26); 744 | GaussianInteger number = new GaussianInteger (value25, value26); 745 | IntegerNumber integerNumber = new IntegerNumber (value25); 746 | int[] a27 = new int[] {4,9,5,5,4,1}; 747 | ArbitraryWholeNumbers value27 = new ArbitraryWholeNumbers (false, a27); 748 | int[] a28 = new int[] {7,9,7,2,7}; 749 | ArbitraryWholeNumbers value28 = new ArbitraryWholeNumbers (false, a28); 750 | GaussianInteger number2 = new GaussianInteger (value27, value28); 751 | assertEquals(true, ((number.add(integerNumber).toString()).equals(number2.toString()))); 752 | } 753 | 754 | /** 755 | * Case 6 of the add() method testing: Test if the inputs with different signs will throw an Unsupported Operation Exception: 756 | */ 757 | @Test 758 | public void testIfGaussianAddMethodThrowsException() { 759 | int[] a1 = new int[] {7,9,0,2,4}; 760 | ArbitraryWholeNumbers value = new ArbitraryWholeNumbers (true, a1); 761 | int[] a2 = new int[] {3,1,4}; 762 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a2); 763 | GaussianInteger g1 = new GaussianInteger (value, value1); 764 | int[] a3 = new int[] {7,6,9,0,0}; 765 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a3); 766 | int[] a4 = new int[] {6,5}; 767 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (false, a4); 768 | GaussianInteger g2 = new GaussianInteger (value2, value3); 769 | boolean thrown = false; 770 | try { 771 | g2.add(g1); 772 | } 773 | catch (UnsupportedOperationException e) { 774 | thrown = true; 775 | } 776 | assertTrue(thrown); 777 | } 778 | 779 | /** 780 | * Testing the toString() method with 4 cases: 781 | */ 782 | @Test 783 | public void testToStringGaussian() { 784 | // Case 1: Test if the complex number to be represented contains a positive real part and positive imaginary part 785 | int[] a5 = new int[] {2,3,5,1,3}; 786 | ArbitraryWholeNumbers value5 = new ArbitraryWholeNumbers (false, a5); 787 | int[] a6 = new int[] {3,2,1,7,6}; 788 | ArbitraryWholeNumbers value6 = new ArbitraryWholeNumbers (false, a6); 789 | GaussianInteger a = new GaussianInteger(value5, value6); 790 | assertEquals("31532 + 67123i", a.toString()); 791 | // Case 2: Test if the complex number to be represented contains a negative real part and positive imaginary part 792 | int[] a1 = new int[] {2,3,5,1,3}; 793 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (true, a1); 794 | int[] a2 = new int[] {3,2,1,7,6}; 795 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 796 | GaussianInteger c = new GaussianInteger(value1, value2); 797 | assertEquals("-31532 + 67123i", c.toString()); 798 | // Case 3: Test if the complex number to be represented contains a positive real part and negative imaginary part 799 | int[] a3 = new int[] {3,0,0,0,9}; 800 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (false, a3); 801 | int[] a4 = new int[] {3,9,1,0}; 802 | ArbitraryWholeNumbers value4 = new ArbitraryWholeNumbers (true, a4); 803 | GaussianInteger d = new GaussianInteger(value3, value4); 804 | assertEquals("90003 -193i", d.toString()); 805 | // Case 4: Test if the complex number to be represented contains a negative real part and negative imaginary part 806 | int[] a7 = new int[] {2,3,5,1,3}; 807 | ArbitraryWholeNumbers value7 = new ArbitraryWholeNumbers (true, a7); 808 | int[] a8 = new int[] {3,2,1,7,6}; 809 | ArbitraryWholeNumbers value8 = new ArbitraryWholeNumbers (true, a8); 810 | GaussianInteger b = new GaussianInteger(value7, value8); 811 | assertEquals("-31532 -67123i", b.toString()); 812 | } 813 | 814 | /** 815 | * Testing the equals() method with 2 cases: 816 | */ 817 | @Test 818 | public void testEqualsGaussian() { 819 | // Case 1: Test if this gaussian integers and the number to be compared is equal 820 | int[] a1 = new int[] {2,3,5,1,3}; 821 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (true, a1); 822 | int[] a2 = new int[] {3,2,1,7,6}; 823 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 824 | GaussianInteger c = new GaussianInteger(value1, value2); 825 | int[] a3 = new int[] {2,3,5,1,3}; 826 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (true, a3); 827 | int[] a4 = new int[] {3,2,1,7,6}; 828 | ArbitraryWholeNumbers value4 = new ArbitraryWholeNumbers (false, a4); 829 | GaussianInteger d = new GaussianInteger(value3, value4); 830 | assertEquals(true, c.equals(c,d)); 831 | // Case 2: Test if this gaussian integers and the number to be compared is not equal 832 | int[] a5 = new int[] {2,3,5,1,3}; 833 | ArbitraryWholeNumbers value5 = new ArbitraryWholeNumbers (true, a5); 834 | int[] a6 = new int[] {3,2,1,7}; 835 | ArbitraryWholeNumbers value6 = new ArbitraryWholeNumbers (false, a6); 836 | GaussianInteger e = new GaussianInteger(value5, value6); 837 | int[] a7 = new int[] {2,3,5,3}; 838 | ArbitraryWholeNumbers value7 = new ArbitraryWholeNumbers (true, a7); 839 | int[] a8 = new int[] {2,1,7}; 840 | ArbitraryWholeNumbers value8 = new ArbitraryWholeNumbers (false, a8); 841 | GaussianInteger f = new GaussianInteger(value7, value8); 842 | assertEquals(false, e.equals(e,f)); 843 | } 844 | 845 | /** 846 | * Testing the RealNumber class: 847 | */ 848 | 849 | /** 850 | * Testing the getNumber() method with 2 cases: 851 | */ 852 | @Test 853 | public void testGetNumberRealNumber() { 854 | // Case 1: Test if the method correctly returns the number (which, in this case, is negative) 855 | int[] a1 = new int[] {2,3,5,1,3}; 856 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (true, a1); 857 | RealNumber g = new RealNumber(value1); 858 | assertEquals(true, g.getNumber().equals(value1)); 859 | // Case 2: Test if the method correctly returns the number (which, in this case, is positive) 860 | int[] a3 = new int[] {2,3,5,1,3}; 861 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (false, a3); 862 | RealNumber c = new RealNumber(value3); 863 | assertEquals(true, c.getNumber().equals(value3)); 864 | } 865 | 866 | /** 867 | * Testing the getRealPart() method with 2 cases: 868 | */ 869 | @Test 870 | public void testGetRealPartRealNumber() { 871 | // Case 1: Test if the method correctly returns the number itself (which, in this case, is positive): 872 | int[] a1 = new int[] {2,3,5,1,3}; 873 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (true, a1); 874 | RealNumber g = new RealNumber(value1); 875 | assertEquals(true, g.getRealPart().equals(value1)); 876 | // Case 2: Test if the method correctly returns the number itself (which, in this case, is negative): 877 | int[] a3 = new int[] {2,3,5,1,3}; 878 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (false, a3); 879 | RealNumber c = new RealNumber(value3); 880 | assertEquals(true, c.getRealPart().equals(value3)); 881 | } 882 | 883 | /** 884 | * Testing the getImaginaryPart() method with 1 case: 885 | */ 886 | @Test 887 | public void testGetImaginaryPartRealNumber() { 888 | // Case 1: Test if the method correctly returns the number of 0, as a real number cannot have any imaginary part: 889 | int[] a1 = new int[] {2,3,5,1,3}; 890 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (true, a1); 891 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, new int[] {0}); 892 | RealNumber g = new RealNumber(value1); 893 | assertEquals(true, g.getImaginaryPart().equals(value2)); 894 | } 895 | 896 | /** 897 | * Testing the add() method with 2 cases: 898 | */ 899 | @Test 900 | public void testAddRealNumber() { 901 | /* Case 1: Test if the addition of a positive real number and a positive real number yields the correct result */ 902 | int[] a1 = new int[] {2,3,5,1,3}; 903 | ArbitraryFloatingPointNumbers value1 = new ArbitraryFloatingPointNumbers (false, a1); 904 | RealNumber c = new RealNumber(value1); 905 | int[] a2 = new int[] {3,2,1,7,6}; 906 | ArbitraryFloatingPointNumbers value2 = new ArbitraryFloatingPointNumbers (false, a2); 907 | RealNumber d = new RealNumber(value2); 908 | int[] a3 = new int[] {5,5,6,8,9}; 909 | ArbitraryFloatingPointNumbers value3 = new ArbitraryFloatingPointNumbers (false, a3); 910 | RealNumber e = new RealNumber(value3); 911 | assertEquals(true, ((c.add(d).toString()).equals(e.toString()))); 912 | /* Case 2: Test if the addition of a negative real number and a negative real number yields the correct result */ 913 | int[] a4 = new int[] {2,3,5,1,3}; 914 | ArbitraryFloatingPointNumbers value4 = new ArbitraryFloatingPointNumbers (true, a4); 915 | RealNumber l = new RealNumber(value4); 916 | int[] a5 = new int[] {3,2,1,7,6}; 917 | ArbitraryFloatingPointNumbers value5 = new ArbitraryFloatingPointNumbers (true, a5); 918 | RealNumber m = new RealNumber(value5); 919 | int[] a6 = new int[] {5,5,6,8,9}; 920 | ArbitraryFloatingPointNumbers value6 = new ArbitraryFloatingPointNumbers (true, a6); 921 | RealNumber n = new RealNumber(value6); 922 | assertEquals(true, ((l.add(m).toString()).equals(n.toString()))); 923 | // Case 3: Test if adding in hierarchy can produce a correct number 924 | // Test if adding a real number with an integer number can produce a correct result 925 | int[] a25 = new int[] {7,9,7,2,7}; 926 | ArbitraryWholeNumbers value25 = new ArbitraryWholeNumbers (false, a25); 927 | int[] a26 = new int[] {7,9,7,2,7}; 928 | ArbitraryWholeNumbers value26 = new ArbitraryWholeNumbers (false, a26); 929 | RealNumber number = new RealNumber (value25); 930 | IntegerNumber integerNumber = new IntegerNumber (value26); 931 | int[] a27 = new int[] {4,9,5,5,4,1}; 932 | ArbitraryFloatingPointNumbers value27 = new ArbitraryFloatingPointNumbers (false, a27); 933 | RealNumber number2 = new RealNumber (value27); 934 | assertEquals(true, ((number.add(integerNumber).toString()).equals(number2.toString()))); 935 | } 936 | 937 | /** 938 | * Case 4 of the add() method testing: Test if the inputs with different signs will throw an Unsupported Operation Exception: 939 | */ 940 | @Test 941 | public void testIfAddRealNumberThrowsException() { 942 | int[] a1 = new int[] {7,9,0,2,4}; 943 | ArbitraryWholeNumbers value = new ArbitraryWholeNumbers (true, a1); 944 | RealNumber r = new RealNumber (value); 945 | int[] a2 = new int[] {3,1,4}; 946 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a2); 947 | RealNumber r1 = new RealNumber (value1); 948 | boolean thrown = false; 949 | try { 950 | r1.add(r); 951 | } 952 | catch (UnsupportedOperationException e) { 953 | thrown = true; 954 | } 955 | assertTrue(thrown); 956 | } 957 | 958 | /** 959 | * Testing the equals() method with 2 cases: 960 | */ 961 | @Test 962 | public void testEqualsRealNumber() { 963 | // Case 1: Test if two real numbers to be compared are equal to each other: 964 | int[] a1 = new int[] {2,3,5,1,3}; 965 | ArbitraryFloatingPointNumbers value1 = new ArbitraryFloatingPointNumbers (false, a1); 966 | RealNumber c = new RealNumber(value1); 967 | int[] a2 = new int[] {2,3,5,1,3}; 968 | ArbitraryFloatingPointNumbers value2 = new ArbitraryFloatingPointNumbers (false, a2); 969 | RealNumber d = new RealNumber(value2); 970 | assertEquals(true, (c.equals(c,d))); 971 | // Case 2: Test if two real numbers to be compared are NOT equal to each other: 972 | int[] a4 = new int[] {2,3,5,1,3}; 973 | ArbitraryFloatingPointNumbers value4 = new ArbitraryFloatingPointNumbers (false, a4); 974 | RealNumber l = new RealNumber(value4); 975 | int[] a5 = new int[] {3,2,1,7,6}; 976 | ArbitraryFloatingPointNumbers value5 = new ArbitraryFloatingPointNumbers (true, a5); 977 | RealNumber m = new RealNumber(value5); 978 | assertEquals(false, (l.equals(l,m))); 979 | } 980 | 981 | /** 982 | * Testing the toString() method with 2 cases: 983 | */ 984 | @Test 985 | public void testToStringRealNumber() { 986 | // Case 1: Test if the real number to be represented is negative: 987 | int[] a1 = new int[] {2,3,5,1,3}; 988 | ArbitraryFloatingPointNumbers value1 = new ArbitraryFloatingPointNumbers (true, a1); 989 | RealNumber c = new RealNumber(value1); 990 | assertEquals("-31532.", (c.toString())); 991 | // Case 2: Test if the real number to be represented is positive: 992 | int[] a4 = new int[] {2,3,5,1,3}; 993 | ArbitraryFloatingPointNumbers value4 = new ArbitraryFloatingPointNumbers (false, a4); 994 | RealNumber l = new RealNumber(value4); 995 | assertEquals("31532.", (l.toString())); 996 | } 997 | 998 | /** 999 | * Testing methods of class RationalNumber: 1000 | */ 1001 | 1002 | /** 1003 | * Testing the getNumerator() method with 2 cases: 1004 | */ 1005 | @Test 1006 | public void testGetNumeratorRational() { 1007 | // Case 1: Test if the method correctly returns the real part (which, in this case, is positive) of the gaussian integer 1008 | int[] a1 = new int[] {2,3,5,1,3}; 1009 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1010 | int[] a2 = new int[] {3,2,1,7,6}; 1011 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 1012 | RationalNumber g = new RationalNumber(value1, value2); 1013 | assertEquals(true, g.getNumerator().equals(value1)); 1014 | // Case 2: Test if the method correctly returns the real part (which, in this case, is positive) of the gaussian integer 1015 | int[] a3 = new int[] {2,3,5,1,3}; 1016 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (true, a3); 1017 | int[] a4 = new int[] {3,2,1,7,6}; 1018 | ArbitraryWholeNumbers value4 = new ArbitraryWholeNumbers (false, a4); 1019 | RationalNumber c = new RationalNumber(value3, value4); 1020 | assertEquals(true, c.getNumerator().equals(value3)); 1021 | } 1022 | 1023 | /** 1024 | * Testing the getDenominator() method with 2 cases: 1025 | */ 1026 | @Test 1027 | public void testGetDenominatorRational() { 1028 | // Case 1: Test if the method correctly returns the denominator of the rational number (which, in this case, is positive) 1029 | int[] a1 = new int[] {2,3,5,1,3}; 1030 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1031 | int[] a2 = new int[] {3,2,1,7,6}; 1032 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 1033 | RationalNumber g = new RationalNumber(value1, value2); 1034 | assertEquals(true, g.getDenominator().equals(value2)); 1035 | // Case 2: Test if the method correctly returns the denominator of the rational number (which, in this case, is negative) 1036 | int[] a3 = new int[] {2,3,5,1,3}; 1037 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (true, a3); 1038 | int[] a4 = new int[] {3,2,1,7,6}; 1039 | ArbitraryWholeNumbers value4 = new ArbitraryWholeNumbers (false, a4); 1040 | RationalNumber c = new RationalNumber(value3, value4); 1041 | assertEquals(true, c.getDenominator().equals(value4)); 1042 | } 1043 | 1044 | /** 1045 | * Testing the inherited getNumber() method with 2 cases: 1046 | */ 1047 | @Test 1048 | public void testGetNumberRational() { 1049 | // Case 1: Test if the method correctly returns the number of the rational number (which, in this case, is positive.) 1050 | int[] a1 = new int[] {2,3,5,1,3}; 1051 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1052 | int[] a2 = new int[] {3,2,1,7,6}; 1053 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 1054 | RationalNumber g = new RationalNumber(value1, value2); 1055 | assertEquals(true, g.getNumber().equals(value1)); 1056 | // Case 2: Test if the method correctly returns the number of the rational number (which, in this case, is negative.) 1057 | int[] a3 = new int[] {2,3,5,1,3}; 1058 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (true, a3); 1059 | int[] a4 = new int[] {3,2,1,7,6}; 1060 | ArbitraryWholeNumbers value4 = new ArbitraryWholeNumbers (false, a4); 1061 | RationalNumber c = new RationalNumber(value3, value4); 1062 | assertEquals(true, c.getNumber().equals(value3)); 1063 | } 1064 | 1065 | /** 1066 | * Testing the inherited getNumber() method with 2 cases: 1067 | */ 1068 | @Test 1069 | public void testGetRealPartRational() { 1070 | // Case 1: Test if the method correctly returns the number of the rational number (which, in this case, is positive.) 1071 | int[] a1 = new int[] {2,3,5,1,3}; 1072 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1073 | int[] a2 = new int[] {3,2,1,7,6}; 1074 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 1075 | RationalNumber g = new RationalNumber(value1, value2); 1076 | assertEquals(true, g.getRealPart().equals(value1)); 1077 | // Case 2: Test if the method correctly returns the number of the rational number (which, in this case, is negative.) 1078 | int[] a3 = new int[] {2,3,5,1,3}; 1079 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (true, a3); 1080 | int[] a4 = new int[] {3,2,1,7,6}; 1081 | ArbitraryWholeNumbers value4 = new ArbitraryWholeNumbers (false, a4); 1082 | RationalNumber c = new RationalNumber(value3, value4); 1083 | assertEquals(true, c.getRealPart().equals(value3)); 1084 | } 1085 | 1086 | /** 1087 | * Testing the inherited getNumber() method with 1 case: 1088 | */ 1089 | @Test 1090 | public void testGetImaginaryPartRational() { 1091 | // Case 1: Test if the method correctly returns the number of 0, as a natural number cannot have any imaginary part 1092 | int[] a1 = new int[] {2,3,5,1,3}; 1093 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1094 | int[] a2 = new int[] {3,2,1,7,6}; 1095 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 1096 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (false, new int[] {0}); 1097 | RationalNumber g = new RationalNumber(value1, value2); 1098 | assertEquals(true, g.getImaginaryPart().equals(value3)); 1099 | } 1100 | 1101 | /** 1102 | * Testing the add() method with 2 cases: 1103 | */ 1104 | @Test 1105 | public void testAddRational() { 1106 | /* Case 1: Test if the addition between two rational numbers with positive numerators and same denominators 1107 | * can yield correct answers */ 1108 | int[] a1 = new int[] {2,3,5,1,3}; 1109 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1110 | int[] a2 = new int[] {3,2,1,7,6}; 1111 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 1112 | RationalNumber g = new RationalNumber(value1, value2); 1113 | int[] a3 = new int[] {9,2,1,9,4}; 1114 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (false, a3); 1115 | int[] a4 = new int[] {3,2,1,7,6}; 1116 | ArbitraryWholeNumbers value4 = new ArbitraryWholeNumbers (false, a4); 1117 | RationalNumber e = new RationalNumber(value3, value4); 1118 | int[] a5 = new int[] {1,6,6,0,8}; 1119 | ArbitraryWholeNumbers value5 = new ArbitraryWholeNumbers (false, a5); 1120 | int[] a6 = new int[] {3,2,1,7,6}; 1121 | ArbitraryWholeNumbers value6 = new ArbitraryWholeNumbers (false, a6); 1122 | RationalNumber f = new RationalNumber(value5, value6); 1123 | assertEquals(true, (g.add(e).toString()).equals(f.toString())); 1124 | /* Case 2: Test if the addition between two rational numbers with negative numerators and same denominators can 1125 | * yield correct answers */ 1126 | int[] a7 = new int[] {2,3,5,1,3}; 1127 | ArbitraryWholeNumbers value7 = new ArbitraryWholeNumbers (true, a7); 1128 | int[] a8 = new int[] {3,2,1,7,6}; 1129 | ArbitraryWholeNumbers value8 = new ArbitraryWholeNumbers (false, a8); 1130 | RationalNumber l = new RationalNumber(value7, value8); 1131 | int[] a9 = new int[] {9,2,1,9,4}; 1132 | ArbitraryWholeNumbers value9 = new ArbitraryWholeNumbers (true, a9); 1133 | int[] a10 = new int[] {3,2,1,7,6}; 1134 | ArbitraryWholeNumbers value10 = new ArbitraryWholeNumbers (false, a10); 1135 | RationalNumber m = new RationalNumber(value9, value10); 1136 | int[] a11 = new int[] {1,6,6,0,8}; 1137 | ArbitraryWholeNumbers value11 = new ArbitraryWholeNumbers (true, a11); 1138 | int[] a12 = new int[] {3,2,1,7,6}; 1139 | ArbitraryWholeNumbers value12 = new ArbitraryWholeNumbers (false, a12); 1140 | RationalNumber n = new RationalNumber(value11, value12); 1141 | assertEquals(true, (l.add(m).toString()).equals(n.toString())); 1142 | } 1143 | 1144 | /** 1145 | * Case 3 of the add() method testing: Test if the inputs with different signs will throw an Unsupported Operation Exception: 1146 | */ 1147 | @Test 1148 | public void testIfAddRationalThrowsException() { 1149 | int[] a1 = new int[] {7,9,0,2,4}; 1150 | ArbitraryWholeNumbers value = new ArbitraryWholeNumbers (true, a1); 1151 | int[] a2 = new int[] {3,1,4}; 1152 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a2); 1153 | RationalNumber r1 = new RationalNumber (value, value1); 1154 | int[] a3 = new int[] {4,1,5}; 1155 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a3); 1156 | int[] a4 = new int[] {4,5}; 1157 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (true, a4); 1158 | RationalNumber r2 = new RationalNumber (value2, value3); 1159 | boolean thrown = false; 1160 | try { 1161 | r1.add(r2); 1162 | } 1163 | catch (UnsupportedOperationException e) { 1164 | thrown = true; 1165 | } 1166 | assertTrue(thrown); 1167 | } 1168 | 1169 | 1170 | /** 1171 | * Testing the equals() method with 2 cases: 1172 | */ 1173 | @Test 1174 | public void testEqualsRational() { 1175 | /* Case 1: Test if the two rational numbers to be compared have the same numerators and denominators */ 1176 | int[] a1 = new int[] {2,3,5,1,3}; 1177 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1178 | int[] a2 = new int[] {3,2,1,7,6}; 1179 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 1180 | RationalNumber g = new RationalNumber(value1, value2); 1181 | int[] a3 = new int[] {2,3,5,1,3}; 1182 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (false, a3); 1183 | int[] a4 = new int[] {3,2,1,7,6}; 1184 | ArbitraryWholeNumbers value4 = new ArbitraryWholeNumbers (false, a4); 1185 | RationalNumber e = new RationalNumber(value3, value4); 1186 | assertEquals(true, g.equals(e,g)); 1187 | /* Case 2: Test if the two rational numbers to be compared have different numerators and denominators */ 1188 | int[] a7 = new int[] {2,3,5,1,3}; 1189 | ArbitraryWholeNumbers value7 = new ArbitraryWholeNumbers (true, a7); 1190 | int[] a8 = new int[] {3,2,1,7,6}; 1191 | ArbitraryWholeNumbers value8 = new ArbitraryWholeNumbers (false, a8); 1192 | RationalNumber l = new RationalNumber(value7, value8); 1193 | int[] a9 = new int[] {9,2,1,9,4}; 1194 | ArbitraryWholeNumbers value9 = new ArbitraryWholeNumbers (true, a9); 1195 | int[] a10 = new int[] {3,2,1,7,6}; 1196 | ArbitraryWholeNumbers value10 = new ArbitraryWholeNumbers (false, a10); 1197 | RationalNumber m = new RationalNumber(value9, value10); 1198 | assertEquals(false, l.equals(l,m)); 1199 | } 1200 | 1201 | /** 1202 | * Testing the toString() method with 4 cases: 1203 | */ 1204 | @Test 1205 | public void testToStringRational() { 1206 | /* Case 1: Test if the rational number to be represented have positive numerator and positive denominator */ 1207 | int[] a1 = new int[] {2,3,5,1,3}; 1208 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1209 | int[] a2 = new int[] {3,2,1,7,6}; 1210 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 1211 | RationalNumber g = new RationalNumber(value1, value2); 1212 | assertEquals("31532 / 67123", g.toString()); 1213 | /* Case 2: Test if the rational number to be represented have positive numerator and negative denominator */ 1214 | int[] a7 = new int[] {2,3,5,1,3}; 1215 | ArbitraryWholeNumbers value7 = new ArbitraryWholeNumbers (false, a7); 1216 | int[] a8 = new int[] {3,2,1,7,6}; 1217 | ArbitraryWholeNumbers value8 = new ArbitraryWholeNumbers (true, a8); 1218 | RationalNumber l = new RationalNumber(value7, value8); 1219 | assertEquals("31532 / -67123", l.toString()); 1220 | // Case 3: Test if the rational number to be represented have negative numerator and positive denominator 1221 | int[] a3 = new int[] {2,3,5,1,3}; 1222 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (true, a3); 1223 | int[] a4 = new int[] {3,2,1,7,6}; 1224 | ArbitraryWholeNumbers value4 = new ArbitraryWholeNumbers (false, a4); 1225 | RationalNumber m = new RationalNumber(value3, value4); 1226 | assertEquals("-31532 / 67123", m.toString()); 1227 | // Case 4: Test if the rational number to be represented have negative numerator and negative denominator 1228 | int[] a5 = new int[] {2,3,5,1,3}; 1229 | ArbitraryWholeNumbers value5 = new ArbitraryWholeNumbers (true, a5); 1230 | int[] a6 = new int[] {3,2,1,7,6}; 1231 | ArbitraryWholeNumbers value6 = new ArbitraryWholeNumbers (true, a6); 1232 | RationalNumber n = new RationalNumber(value5, value6); 1233 | assertEquals("-31532 / -67123", n.toString()); 1234 | } 1235 | 1236 | /** 1237 | * Testing methods of class IntegerNumber: 1238 | */ 1239 | 1240 | /** 1241 | * Testing the getNumber() method with 2 cases: 1242 | */ 1243 | @Test 1244 | public void testGetNumberInteger() { 1245 | // Case 1: Test if the method correctly returns the number (which, in this case, is negative) 1246 | int[] a1 = new int[] {2,3,5,1,3}; 1247 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (true, a1); 1248 | IntegerNumber g = new IntegerNumber(value1); 1249 | assertEquals(true, g.getNumber().equals(value1)); 1250 | // Case 2: Test if the method correctly returns the number (which, in this case, is positive) 1251 | int[] a3 = new int[] {2,3,5,1,3}; 1252 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (false, a3); 1253 | IntegerNumber c = new IntegerNumber(value3); 1254 | assertEquals(true, c.getNumber().equals(value3)); 1255 | } 1256 | 1257 | /** 1258 | * Testing the getRealPart() method with 2 cases: 1259 | */ 1260 | @Test 1261 | public void testGetRealPartInteger() { 1262 | // Case 1: Test if the method correctly returns the number itself (which, in this case, is positive) 1263 | int[] a1 = new int[] {2,3,5,1,3}; 1264 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (true, a1); 1265 | IntegerNumber g = new IntegerNumber(value1); 1266 | assertEquals(true, g.getRealPart().equals(value1)); 1267 | // Case 2: Test if the method correctly returns the number itself (which, in this case, is negative) 1268 | int[] a3 = new int[] {2,3,5,1,3}; 1269 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (false, a3); 1270 | IntegerNumber c = new IntegerNumber(value3); 1271 | assertEquals(true, c.getRealPart().equals(value3)); 1272 | } 1273 | 1274 | /** 1275 | * Testing the getImaginaryPart() method with 1 case: 1276 | */ 1277 | @Test 1278 | public void testGetImaginaryPartInteger() { 1279 | /* Case 1: Test if the method correctly returns the denominator of an integer, which should be 1 1280 | * (assuming it needs to be 1 so that an integer can be correctly represented by 1 arbitrary whole number) */ 1281 | int[] a1 = new int[] {2,3,5,1,3}; 1282 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1283 | IntegerNumber g = new IntegerNumber(value1); 1284 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, new int[] {0}); 1285 | assertEquals(true, (g.getImaginaryPart().toString()).equals(value2.toString())); 1286 | } 1287 | 1288 | /** 1289 | * Testing the getNumerator() method with 2 cases: 1290 | */ 1291 | @Test 1292 | public void testGetNumeratorInteger() { 1293 | // Case 1: Test if the method correctly returns the numerator of a positive integer, which is the number itself 1294 | int[] a1 = new int[] {2,3,5,1,3}; 1295 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (true, a1); 1296 | IntegerNumber g = new IntegerNumber(value1); 1297 | assertEquals(true, g.getNumerator().equals(value1)); 1298 | // Case 2: Test if the method correctly returns the numerator of a negative integer, which is the number itself 1299 | int[] a3 = new int[] {2,3,5,1,3}; 1300 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (false, a3); 1301 | IntegerNumber c = new IntegerNumber(value3); 1302 | assertEquals(true, c.getNumerator().equals(value3)); 1303 | } 1304 | 1305 | /** 1306 | * Testing the getDenominator() method with 1 case: 1307 | */ 1308 | @Test 1309 | public void testGetDenominatorInteger() { 1310 | /* Case 1: Test if the method correctly returns the denominator of an integer, which should be 1 1311 | * (assuming it needs to be 1 so that an integer can be correctly represented by 1 arbitrary whole number) */ 1312 | int[] a1 = new int[] {2,3,5,1,3}; 1313 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1314 | IntegerNumber g = new IntegerNumber(value1); 1315 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, new int[] {1}); 1316 | assertEquals(true, (g.getDenominator().toString()).equals(value2.toString())); 1317 | } 1318 | 1319 | /** 1320 | * Testing the add() method with 2 cases: 1321 | */ 1322 | @Test 1323 | public void testAddInteger() { 1324 | // Case 1: Test if the addition of a positive integer number and a positive integer number yields the correct result 1325 | int[] a1 = new int[] {2,3,5,1,3}; 1326 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1327 | IntegerNumber g = new IntegerNumber(value1); 1328 | int[] a2 = new int[] {3,2,1,7,6}; 1329 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 1330 | IntegerNumber f = new IntegerNumber(value2); 1331 | int[] a3 = new int[] {5,5,6,8,9}; 1332 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (false, a3); 1333 | IntegerNumber c = new IntegerNumber(value3); 1334 | assertEquals(true, (g.add(f,g).toString()).equals(c.toString())); 1335 | // Case 2: Test if the addition of a negative integer number and a negative integer number yields the correct result 1336 | int[] a4 = new int[] {2,3,5,1,3}; 1337 | ArbitraryWholeNumbers value4 = new ArbitraryWholeNumbers (true, a4); 1338 | IntegerNumber k = new IntegerNumber(value4); 1339 | int[] a5 = new int[] {3,2,1,7,6}; 1340 | ArbitraryWholeNumbers value5 = new ArbitraryWholeNumbers (true, a5); 1341 | IntegerNumber l = new IntegerNumber(value5); 1342 | int[] a6 = new int[] {5,5,6,8,9}; 1343 | ArbitraryWholeNumbers value6 = new ArbitraryWholeNumbers (true, a6); 1344 | IntegerNumber m = new IntegerNumber(value6); 1345 | assertEquals(true, (k.add(k,l).toString()).equals(m.toString())); 1346 | // Case 3: Test if adding in hierarchy can produce a correct number 1347 | // Test if adding a real number with an integer number can produce a correct result 1348 | int[] a25 = new int[] {7,9,7,2,7}; 1349 | ArbitraryWholeNumbers value25 = new ArbitraryWholeNumbers (false, a25); 1350 | int[] a26 = new int[] {7,9,7,2,7}; 1351 | ArbitraryWholeNumbers value26 = new ArbitraryWholeNumbers (false, a26); 1352 | IntegerNumber number = new IntegerNumber (value25); 1353 | NaturalNumber number2 = new NaturalNumber (value26); 1354 | int[] a27 = new int[] {4,9,5,5,4,1}; 1355 | ArbitraryWholeNumbers value27 = new ArbitraryWholeNumbers (false, a27); 1356 | IntegerNumber number3 = new IntegerNumber (value27); 1357 | assertEquals(true, ((number.add(number, number2).toString()).equals(number3.toString()))); 1358 | } 1359 | 1360 | /** 1361 | * Case 4 of the add() method testing: Test if the inputs with different signs will throw an Unsupported Operation Exception: 1362 | */ 1363 | @Test 1364 | public void testIfAddIntegersThrowsException() { 1365 | int[] a1 = new int[] {7,9,0,2,4}; 1366 | ArbitraryWholeNumbers value = new ArbitraryWholeNumbers (true, a1); 1367 | IntegerNumber i1 = new IntegerNumber (value); 1368 | int[] a2 = new int[] {3,1,4}; 1369 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a2); 1370 | IntegerNumber i2 = new IntegerNumber (value1); 1371 | boolean thrown = false; 1372 | try { 1373 | i1.add(i1,i2); 1374 | } 1375 | catch (UnsupportedOperationException e) { 1376 | thrown = true; 1377 | } 1378 | assertTrue(thrown); 1379 | } 1380 | 1381 | /** 1382 | * Testing the equals() method with 2 cases: 1383 | */ 1384 | @Test 1385 | public void testEqualsInteger() { 1386 | // Case 1: Test if two integer numbers to be compared are equal to each other: 1387 | int[] a1 = new int[] {2,3,5,1,3}; 1388 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1389 | IntegerNumber g = new IntegerNumber(value1); 1390 | int[] a2 = new int[] {2,3,5,1,3}; 1391 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 1392 | IntegerNumber f = new IntegerNumber(value2); 1393 | assertEquals(true, g.equals(f, g)); 1394 | // Case 2: Test if two integer numbers to be compared are NOT equal to each other 1395 | int[] a3 = new int[] {2,3,5,1,3}; 1396 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (false, a3); 1397 | IntegerNumber l = new IntegerNumber(value3); 1398 | int[] a4 = new int[] {5,5,6,8,9}; 1399 | ArbitraryWholeNumbers value4 = new ArbitraryWholeNumbers (false, a4); 1400 | IntegerNumber m = new IntegerNumber(value4); 1401 | assertEquals(false, l.equals(m, l)); 1402 | } 1403 | 1404 | /** 1405 | * Testing the toString() method with 2 cases: 1406 | */ 1407 | @Test 1408 | public void testToStringInteger() { 1409 | // Case 1: Test if the integer number to be represented is negative 1410 | int[] a1 = new int[] {2,3,5,1,3}; 1411 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1412 | IntegerNumber g = new IntegerNumber(value1); 1413 | assertEquals("31532", g.toString()); 1414 | // Case 2: Test if the integer number to be represented is positive 1415 | int[] a3 = new int[] {2,3,5,1,3}; 1416 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (true, a3); 1417 | IntegerNumber l = new IntegerNumber(value3); 1418 | assertEquals("-31532", l.toString()); 1419 | } 1420 | 1421 | /** 1422 | * Testing the NaturalNumber class: 1423 | * Note: A natural number cannot be negative, therefore test cases involving negative natural number inputs 1424 | * will not be included. 1425 | */ 1426 | 1427 | /** 1428 | * Testing if the constructor of the NaturalNumber class will throw an exception when the input is negative: 1429 | * The constructor should be able to construct an instance of the class when the given ArbitraryWholeNumber input 1430 | * is positive. HOWEVER, it will throw an exception if the given input is negative, as a natural number cannot be 1431 | * negative. 1432 | */ 1433 | @Test 1434 | public void testNaturalNumberConstructor() { 1435 | int[] a1 = new int[] {1,4}; 1436 | ArbitraryWholeNumbers value = new ArbitraryWholeNumbers (true, a1); 1437 | boolean thrown = false; 1438 | try { 1439 | NaturalNumber n = new NaturalNumber (value); 1440 | } 1441 | catch (UnsupportedOperationException e) { 1442 | thrown = true; 1443 | } 1444 | assertTrue(thrown); 1445 | } 1446 | 1447 | 1448 | /** 1449 | * Testing the getNumber() method with 2 cases: 1450 | */ 1451 | @Test 1452 | public void testGetNumberNatural() { 1453 | // Case 1: Test if the method correctly returns the natural number 1454 | int[] a1 = new int[] {1,4}; 1455 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1456 | NaturalNumber g = new NaturalNumber(value1); 1457 | assertEquals(true, g.getNumber().equals(value1)); 1458 | } 1459 | 1460 | /** 1461 | * Testing the getRealPart() method with 2 cases: 1462 | */ 1463 | @Test 1464 | public void testGetRealPartNatural() { 1465 | // Case 1: Test if the method correctly returns the number itself 1466 | int[] a1 = new int[] {1,4}; 1467 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1468 | NaturalNumber g = new NaturalNumber(value1); 1469 | assertEquals(true, g.getRealPart().equals(value1)); 1470 | } 1471 | 1472 | /** 1473 | * Testing the getImaginaryPart() method with 1 case: 1474 | */ 1475 | @Test 1476 | public void testGetImaginaryPartNatural() { 1477 | /* Case 1: Test if the method correctly returns the denominator of a natural number, which should be 1 1478 | * (assuming it needs to be 1 so that an integer can be correctly represented by 1 arbitrary whole number) */ 1479 | int[] a1 = new int[] {1,4}; 1480 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1481 | NaturalNumber g = new NaturalNumber(value1); 1482 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, new int[] {0}); 1483 | assertEquals(true, (g.getImaginaryPart().toString()).equals(value2.toString())); 1484 | } 1485 | 1486 | /** 1487 | * Testing the getNumerator() method with 2 cases: 1488 | */ 1489 | @Test 1490 | public void testGetNumeratorNatural() { 1491 | // Case 1: Test if the method correctly returns the numerator of a positive integer, which is the number itself 1492 | int[] a1 = new int[] {1,4}; 1493 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1494 | NaturalNumber g = new NaturalNumber(value1); 1495 | assertEquals(true, g.getNumerator().equals(value1)); 1496 | } 1497 | 1498 | /** 1499 | * Testing the getDenominator() method with 1 case: 1500 | */ 1501 | public void testGetDenominatorNatural() { 1502 | /* Case 1: Test if the method correctly returns the denominator of an integer, which should be 1 1503 | * (assuming it needs to be 1 so that an integer can be correctly represented by 1 arbitrary whole number) */ 1504 | int[] a1 = new int[] {1,4}; 1505 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1506 | NaturalNumber g = new NaturalNumber(value1); 1507 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, new int[] {1}); 1508 | assertEquals(true, (g.getDenominator().toString()).equals(value2.toString())); 1509 | } 1510 | 1511 | /** 1512 | * Testing the add() method with 2 cases: 1513 | */ 1514 | @Test 1515 | public void testAddNatural() { 1516 | // Case 1: Test if the addition of a positive integer number and a positive natural number yields the correct result 1517 | int[] a1 = new int[] {1,4}; 1518 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1519 | NaturalNumber g = new NaturalNumber(value1); 1520 | int[] a2 = new int[] {1,4}; 1521 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 1522 | NaturalNumber f = new NaturalNumber(value2); 1523 | int[] a3 = new int[] {2,8}; 1524 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (false, a3); 1525 | NaturalNumber c = new NaturalNumber(value3); 1526 | assertEquals(true, (g.add(f).toString()).equals(c.toString())); 1527 | // Case 2: Testing of adding natural numbers with more digits 1528 | int[] a4 = new int[] {2,3,5,1,3}; 1529 | ArbitraryWholeNumbers value4 = new ArbitraryWholeNumbers (false, a4); 1530 | NaturalNumber k = new NaturalNumber(value4); 1531 | int[] a5 = new int[] {3,2,1,7,6}; 1532 | ArbitraryWholeNumbers value5 = new ArbitraryWholeNumbers (false, a5); 1533 | NaturalNumber l = new NaturalNumber(value5); 1534 | int[] a6 = new int[] {5,5,6,8,9}; 1535 | ArbitraryWholeNumbers value6 = new ArbitraryWholeNumbers (false, a6); 1536 | NaturalNumber m = new NaturalNumber(value6); 1537 | assertEquals(true, (k.add(l).toString()).equals(m.toString())); 1538 | } 1539 | 1540 | /** 1541 | * Testing the equals() method with 2 cases: 1542 | */ 1543 | @Test 1544 | public void testEqualsNatural() { 1545 | // Case 1: Test if two integer numbers to be compared are equal to each other: 1546 | int[] a1 = new int[] {1,4}; 1547 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1548 | NaturalNumber g = new NaturalNumber(value1); 1549 | int[] a2 = new int[] {1,4}; 1550 | ArbitraryWholeNumbers value2 = new ArbitraryWholeNumbers (false, a2); 1551 | NaturalNumber f = new NaturalNumber(value2); 1552 | assertEquals(true, g.equals(f)); 1553 | // Case 2: Test if two integer numbers to be compared are NOT equal to each other 1554 | int[] a3 = new int[] {1,4}; 1555 | ArbitraryWholeNumbers value3 = new ArbitraryWholeNumbers (false, a3); 1556 | NaturalNumber l = new NaturalNumber(value3); 1557 | int[] a4 = new int[] {5,5,6,8,9}; 1558 | ArbitraryWholeNumbers value4 = new ArbitraryWholeNumbers (false, a4); 1559 | NaturalNumber m = new NaturalNumber(value4); 1560 | assertEquals(false, l.equals(m)); 1561 | } 1562 | 1563 | /** 1564 | * Testing the toString() method with 1 case: 1565 | */ 1566 | @Test 1567 | public void testToStringNatural() { 1568 | // Case 1: Test to represent a natural number in String form 1569 | int[] a1 = new int[] {1,4}; 1570 | ArbitraryWholeNumbers value1 = new ArbitraryWholeNumbers (false, a1); 1571 | NaturalNumber g = new NaturalNumber(value1); 1572 | assertEquals("41", g.toString()); 1573 | } 1574 | 1575 | /* ALL METHODS HAVE BEEN TESTED SUCCESSFULLY */ 1576 | 1577 | } 1578 | -------------------------------------------------------------------------------- /IntegerNumber.java: -------------------------------------------------------------------------------- 1 | /** 2 | * A class that represents Integer Numbers 3 | * @author David Nguyen 4 | * @since 11/14/2022 5 | */ 6 | public class IntegerNumber implements IntegerNumberInterface{ 7 | 8 | //A field representing the real part 9 | private ArbitraryWholeNumbers number; 10 | 11 | /** 12 | * Creates a Integer Number. 13 | * Assigns the class's fields the values that the constructor got when a IntegerNumber object is created. 14 | * @param number An ArbitraryWholeNumbers that represents the input integer 15 | */ 16 | public IntegerNumber(ArbitraryWholeNumbers number) { 17 | this.number = number; 18 | } 19 | 20 | /** 21 | * A method that retrieves the number part of the integer number. 22 | * @return A whole number that represents the number part of an integer number 23 | */ 24 | public ArbitraryWholeNumbers getNumber() { 25 | return number; 26 | } 27 | 28 | /** 29 | * Overriding the getNumerator() method inherited from class RationalNumber. 30 | * Retrieves the numerator of the integer number - which should always be the number itself to correctly represent the 31 | * integer number. The denominator in this case must also be 1. 32 | * @return A whole number that represents the number part of an integer number 33 | */ 34 | public ArbitraryWholeNumbers getNumerator() { 35 | return number; 36 | } 37 | 38 | /** 39 | * Overriding the getDenominator() method inherited from class RationalNumber 40 | * Retrieves the denominator of the integer number - which should always be the number of 1 to correctly represent the 41 | * integer number 42 | * @return A whole number that represents the number part of an integer number 43 | */ 44 | public ArbitraryWholeNumbers getDenominator() { 45 | return (new ArbitraryWholeNumbers (false, new int[] {1})); 46 | } 47 | 48 | /** 49 | * Overriding the getRealPart() method inherited from class GaussianInteger 50 | * A method that retrieves the real part of the integer number - which should be the integer number itself 51 | * @return A whole number that represents the real part of an integer number 52 | */ 53 | public ArbitraryWholeNumbers getRealPart() { 54 | return number; 55 | } 56 | 57 | /** 58 | * Overriding the getImaginaryPart() method inherited from class GaussianInteger. 59 | * A method that retrieves the imaginary part of the Gaussian Integer - which should be 0 because an integer number. 60 | * cannot have any imaginary parts 61 | * @return A whole number that represents the imaginary part of an integer number 62 | */ 63 | public ArbitraryWholeNumbers getImaginaryPart() { 64 | return (new ArbitraryWholeNumbers (false, new int[] {0})); 65 | } 66 | 67 | /** 68 | * A method that retrieves the imaginary part of the integer number. 69 | * @param number1 Any IntegerNumber object that is given as input 70 | * @param number2 Any IntegerNumber object that is given as input 71 | * @return An Integer Number that is the result of the addition between this Gaussian Integer and the input number 72 | */ 73 | public IntegerNumber add(IntegerNumberInterface number1, IntegerNumberInterface number2) { 74 | ArbitraryWholeNumbers result = number1.getNumber().add(number1.getNumber(), number2.getNumber()); 75 | IntegerNumber output = new IntegerNumber(result); 76 | return output; 77 | } 78 | 79 | /** 80 | * A method that compares the two integer numbers. 81 | * @param value1 Any IntegerNumber object that is given as input 82 | * @param value2 Any IntegerNumber object that is given as input 83 | * @return An Integer Number that is the result of the addition between this Integer Number and the input number 84 | */ 85 | public boolean equals(IntegerNumberInterface value1, IntegerNumberInterface value2) { 86 | return (value1.getNumber().equals(value2.getNumber())); 87 | } 88 | 89 | /** 90 | * A method that returns the string representation of this integer number. 91 | * @return A string that is the string represenation of this integer number 92 | */ 93 | public String toString() { 94 | return getNumber().toString(); 95 | } 96 | } -------------------------------------------------------------------------------- /IntegerNumberInterface.java: -------------------------------------------------------------------------------- 1 | /** 2 | * An interface that represents Integer Numbers 3 | * @author David Nguyen 4 | * @since 11/14/2022 5 | */ 6 | public interface IntegerNumberInterface extends GaussianIntegerInterface, RationalNumberInterface { 7 | 8 | /** 9 | * An abstract method that retrieves the number part of the integer number. 10 | * @return A whole number that represents the number part of the integer number 11 | */ 12 | ArbitraryWholeNumbers getNumber(); 13 | 14 | } -------------------------------------------------------------------------------- /NaturalNumber.java: -------------------------------------------------------------------------------- 1 | /** 2 | * A class that represents Natural Numbers 3 | * @author David Nguyen 4 | * @since 11/14/2022 5 | */ 6 | public class NaturalNumber implements NaturalNumberInterface{ 7 | 8 | //A field representing the real part 9 | private ArbitraryWholeNumbers number; 10 | 11 | /** 12 | * Creates a Natural Number. A natural number is a whole number that is not negative 13 | * Assigns the class's fields the values that the constructor got when a NaturalNumber object is created. 14 | * @param number An ArbitraryWholeNumbers that represents the input integer 15 | */ 16 | public NaturalNumber(ArbitraryWholeNumbers number) { 17 | if (!number.isNegativeNumber()) 18 | this.number = number; 19 | else { 20 | throw new UnsupportedOperationException("Please enter a positive natural number!"); 21 | } 22 | } 23 | 24 | /** 25 | * A method that retrieves the number of the natural number. 26 | * @return A whole number that represents the natural number 27 | */ 28 | public ArbitraryWholeNumbers getNumber() { 29 | return number; 30 | } 31 | 32 | /** 33 | * Overriding the getRealPart() method inherited from class GaussianInteger. 34 | * A method that retrieves the imaginary part of the Gaussian Integer - which should be 0 because an integer number 35 | * cannot have any imaginary parts. 36 | * @return A whole number that represents the imaginary part of an integer number 37 | */ 38 | public ArbitraryWholeNumbers getImaginaryPart() { 39 | return (new ArbitraryWholeNumbers (false, new int[] {0})); 40 | } 41 | 42 | /** 43 | * Overriding the getRealPart() method inherited from class GaussianInteger. 44 | * A method that retrieves the real part of a natural number - which that natural number itself 45 | * @return A whole number that represents a natural number 46 | */ 47 | public ArbitraryWholeNumbers getRealPart() { 48 | return number; 49 | } 50 | 51 | /** 52 | * Overriding the getNumerator() method inherited from class RationalNumber. 53 | * A method that retrieves the numerator of a natural number - which is the number itself if the 54 | * denominator is 1, and the denominator must also be 1 in this case 55 | * @return A whole number that represents a natural number 56 | */ 57 | public ArbitraryWholeNumbers getNumerator() { 58 | return number; 59 | } 60 | 61 | /** 62 | * Overriding the getDenominator() method inherited from class RationalNumber. 63 | * A method that retrieves the denominator of a natural number - which should be 1 and always 1 for a natural number 64 | * to be correctly represented 65 | * @return A whole number that represents a natural number 66 | */ 67 | public ArbitraryWholeNumbers getDenominator() { 68 | return (new ArbitraryWholeNumbers (false, new int[] {1})); 69 | } 70 | 71 | /** 72 | * A method that computes the sum of this Natural Number and any input Natural Number. 73 | * @param number Any NaturalNumber object that is given as input 74 | * @return A NaturalNumber object that is the sum of these two natural numbers 75 | */ 76 | public NaturalNumber add(NaturalNumberInterface number) { 77 | ArbitraryWholeNumbers sum = this.getNumber().add(number.getNumber(), this.getNumber()); 78 | NaturalNumber result = new NaturalNumber (sum); 79 | return result; 80 | } 81 | 82 | /** 83 | * A method that compares this Natural Number and any given input Natural Number. 84 | * @param value Any given natural number 85 | * @return A boolean (true or false) depending on the equality of these two numbers 86 | */ 87 | public boolean equals(NaturalNumber value) { 88 | return (this.getNumber().equals(value.getNumber())); 89 | } 90 | 91 | /** 92 | * A method that creates a string representation of the give natural number 93 | * @return A String that is the string representation of the given natural number 94 | */ 95 | public String toString() { 96 | return this.getNumber().toString(); 97 | } 98 | } -------------------------------------------------------------------------------- /NaturalNumberInterface.java: -------------------------------------------------------------------------------- 1 | /** 2 | * An interface that represents Natural Numbers 3 | * @author David Nguyen 4 | * @since 11/14/2022 5 | */ 6 | public interface NaturalNumberInterface extends IntegerNumberInterface { 7 | 8 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Inheritance-in-Java 2 | I am uploading this project since this is my own, original work that I've spent a lot of time working on. These are programmed in Java and was done in Dr. Harold Connamacher in one of Case Western Reserve University's computer science classes. I also want to share my gained knowledge with you, and my work should not be taken as a way to cheat or take advantage of in your assignments. Please give credits to both me and Dr. Connamacher! 3 | -------------------------------------------------------------------------------- /RationalNumber.java: -------------------------------------------------------------------------------- 1 | /** 2 | * A class that represents Rational Numbers 3 | * @author David Nguyen 4 | * @since 11/14/2022 5 | */ 6 | public class RationalNumber implements RationalNumberInterface{ 7 | 8 | //A field representing the real part 9 | private ArbitraryWholeNumbers numerator; 10 | //A field representing the imaginary part 11 | private ArbitraryWholeNumbers denominator; 12 | 13 | /** 14 | * Creates a Rational Number. 15 | * Assigns the class's fields the values that the constructor got when a RationalNumber object is created. 16 | * @param numerator An ArbitraryWholeNumbers that represents the numerator 17 | * @param denominator An ArbitraryWholeNumbers that represents the denominator 18 | */ 19 | public RationalNumber (ArbitraryWholeNumbers numerator, ArbitraryWholeNumbers denominator) { 20 | this.numerator = numerator; 21 | this.denominator = denominator; 22 | } 23 | 24 | /** 25 | * A method that retrieves the numerator of the rational number. 26 | * @return A whole number that represents the numerator of the rational number 27 | */ 28 | public ArbitraryWholeNumbers getNumerator() { 29 | return numerator; 30 | } 31 | 32 | /** 33 | * A method that retrieves the denominator of the rational number. 34 | * @return A whole number that represents the denominator of the rational number 35 | */ 36 | public ArbitraryWholeNumbers getDenominator() { 37 | return denominator; 38 | } 39 | 40 | /** 41 | * Overriding the getNumber() method that is inherited from class RealNumber. 42 | * Retrieving the number of the rational number - Retrieving the real part of the rational number, which is the 43 | * number itself, by returning either null if the denominator is not 1, as the rational cannot be represented in one 44 | * whole number, or returns the numerator if the denominator is 1 45 | * @return A whole number that represents the denominator of the rational number 46 | */ 47 | public ArbitraryWholeNumbers getNumber() { 48 | return numerator; 49 | } 50 | 51 | /** 52 | * Overriding the getRealPart() method that is inherited from class RealNumber. 53 | * Retrieving the real part of the rational number, which is the number itself, by returning either null if the 54 | * denominator is not 1, as the rational cannot be represented in one whole number, or returns the numerator if 55 | * the denominator is 1 56 | * @return A whole number that represents the rational number 57 | */ 58 | public ArbitraryWholeNumbers getRealPart() { 59 | return numerator; 60 | } 61 | 62 | /** 63 | * Overriding the getImaginaryPart() method that is inherited from class RealNumber. 64 | * Retrieving the imaginary part of the rational number, which is assumed to be null because a rational number cannot 65 | * be imaginary 66 | * @return null 67 | */ 68 | public ArbitraryWholeNumbers getImaginaryPart() { 69 | return (new ArbitraryWholeNumbers (false, new int[] {0})); 70 | } 71 | 72 | /** 73 | * A method that computes the sum of this Rational Number and any input Rational Number. 74 | * @param number Any RationalNumber object that is given as input 75 | * @return A RationalNumber object that is the sum of these two natural numbers 76 | */ 77 | public RationalNumber add(RationalNumberInterface number) { 78 | if (this.getDenominator().equals(number.getDenominator())) { 79 | ArbitraryWholeNumbers newNumerator = this.getNumerator().add(this.getNumerator(), number.getNumerator()); 80 | return (new RationalNumber (newNumerator, number.getDenominator())); 81 | } 82 | else 83 | throw new UnsupportedOperationException("Invalid inputs"); 84 | } 85 | 86 | /** 87 | * A method that compares this Rational Number and any given input Rational Number 88 | * @param value1 Any given RationalNumber number 89 | * @param value2 Any given RationalNumber number 90 | * @return A boolean (true or false) depending on the equality of these two numbers 91 | */ 92 | public boolean equals (RationalNumber value1, RationalNumber value2) { 93 | return (value1.getNumerator().equals(value2.getNumerator())) && (value1.getDenominator().equals(getDenominator())); 94 | } 95 | 96 | /** 97 | * A method that creates a string representation of this Rational Number 98 | * @return A String that is the string representation of this Rational Number 99 | */ 100 | public String toString () { 101 | return (getNumerator() + " / " + getDenominator()); 102 | } 103 | } -------------------------------------------------------------------------------- /RationalNumberInterface.java: -------------------------------------------------------------------------------- 1 | /** 2 | * An interface that represents Rational Numbers 3 | * @author David Nguyen 4 | * @since 11/14/2022 5 | */ 6 | public interface RationalNumberInterface extends RealNumberInterface { 7 | 8 | /** 9 | * An abstract method that retrieves the numerator part of the rational numbers. 10 | * @return A whole number that represents the numerator part of the rational numbers 11 | */ 12 | ArbitraryWholeNumbers getNumerator(); 13 | 14 | /** 15 | * An abstract method that retrieves the denominator part of the rational numbers. 16 | * @return A whole number that represents the denominator part of the grational numbers 17 | */ 18 | ArbitraryWholeNumbers getDenominator(); 19 | 20 | } -------------------------------------------------------------------------------- /RealNumber.java: -------------------------------------------------------------------------------- 1 | /** 2 | * A class that represents Real Numbers 3 | * @author David Nguyen 4 | * @since 11/14/2022 5 | */ 6 | public class RealNumber implements RealNumberInterface { 7 | 8 | //A field representing a real, floating-point number 9 | private ArbitraryFloatingPointNumbers number; 10 | 11 | /** 12 | * Creates a Real Number. 13 | * Assigns the class's fields the values that the constructor got when a RealNumber object is created. 14 | * @param number An ArbitraryFloatingPointNumbers that represents the number 15 | */ 16 | public RealNumber (ArbitraryFloatingPointNumbers number) { 17 | this.number = number; 18 | } 19 | 20 | /** 21 | * A method that retrieves the number of the real number. 22 | * @return A floating point number that represents the number of the real number. 23 | */ 24 | public ArbitraryFloatingPointNumbers getNumber() { 25 | return number; 26 | } 27 | 28 | /** 29 | * Overriding the getRealPart() method inherited from class ComplexNumber. 30 | * A method that retrieves the real part of the real number - which is the number itself 31 | * @return A floating point number that represents the number of the real number 32 | */ 33 | public ArbitraryFloatingPointNumbers getRealPart() { 34 | return number; 35 | } 36 | 37 | /** 38 | * Overriding the getImaginaryPart() method inherited from class ComplexNumber 39 | * A method that retrieves the imaginary part of the real number - which should be 0 because a real number cannot 40 | * have any imaginary parts 41 | * @return A floating point number that represents the number of the real number 42 | */ 43 | public ArbitraryFloatingPointNumbers getImaginaryPart() { 44 | return (new ArbitraryWholeNumbers (false, new int[] {0})); 45 | } 46 | 47 | /** 48 | * A method that computes the sum of this Real Number and any input Real Number 49 | * @param number Any RealNumber object that is given as input 50 | * @return A RealNumber object that is the sum of the two real numbers 51 | */ 52 | public RealNumber add(RealNumberInterface number) { 53 | ArbitraryFloatingPointNumbers sum = this.getNumber().add(this.getNumber(), number.getNumber()); 54 | RealNumber result = new RealNumber (sum); 55 | return result; 56 | } 57 | 58 | /** 59 | * A method that compares this RealNumber and any given input RealNumber 60 | * @param value1 Any given RealNumber number 61 | * @param value2 Any given RealNumber number 62 | * @return A boolean (true or false) depending on the equality of these two numbers 63 | */ 64 | public boolean equals (RealNumber value1, RealNumber value2) { 65 | return (value1.getNumber().equals(value2.getNumber())); 66 | } 67 | 68 | /** 69 | * A method that creates a string representation of this Real Number 70 | * @return A String that is the string representation of this Real Number 71 | */ 72 | public String toString() { 73 | return number.toString(); 74 | } 75 | } -------------------------------------------------------------------------------- /RealNumberInterface.java: -------------------------------------------------------------------------------- 1 | /** 2 | * An interface that represents Real Numbers 3 | * @author David Nguyen 4 | * @since 11/14/2022 5 | */ 6 | public interface RealNumberInterface extends ComplexNumberInterface { 7 | 8 | /** 9 | * An abstract method that retrieves the number part of the integer number. 10 | * @return A whole number that represents the number part of the integer number 11 | */ 12 | ArbitraryFloatingPointNumbers getNumber(); 13 | 14 | } --------------------------------------------------------------------------------