Question: USING JAVA LANGUAGE: The purpose of the BigInt class is to solve a very large integer problem using short methods that work together to solve
USING JAVA LANGUAGE: The purpose of the BigInt class is to solve a very large integer problem using short methods that work together to solve the operations of add, subtract multiply, divide, and modulus. A constructor can call a method called setSignAndRemoveItIfItIsThere(). It receives the string that was sent to the constructor and sets a boolean variable positive to true or false and then returns a string without the sign that can then be processed by the constructor to store the string in an ArrayList. A public add method does not add. It calls helper methods to do the work. A public multiply method does not multiply. It calls two private methods. One is a multiply method that calls other methods to do the actual multiplication. A public divide method does not divide. It calls many private helper methods that do the work. *Sample errors *"5stt7" returns an exception *"+" returns an exception *"-" returns an exception *" 500" returns an exception * dividing by zero or modulus by zero returns a BigInException */ This is a challenging project that requires information hiding to be successful.
TEST METHOD:
import java.util.Scanner; public class BigIntMathTesterFinal{ public static void main(String[] args) { /** * Sample valid input and resulting output *"+0" stores ["0"] in ArrayList and sets the value to positive *returns string "0" *"-0" stores ["0"] in ArrayList and sets the value to positive *returns string "0" *"-0023401" stores ["2","3","4","0","1"] in ArrayList and sets the value to negative *returns string "-23401" *-00100000 stores ["1","0","0","0","0","0"] in ArrayList and sets the value to negative *returns string "-100000" */ BigInt bigNumberOne = null; String yesNo = "no"; String menuChoice = "q"; BigInt bigNumberTwo = null; BigInt bigNumberAnswer = null; boolean mathError = false; boolean areEqual = false; boolean compairing = false; boolean validNumbers = false;
System.out.println("This program will perform basic math on large integer numbers");
do { validNumbers = true; mathError = false; areEqual = false; compairing = false; bigNumberOne = inputBigInteger(); bigNumberTwo = inputBigInteger(); System.out.println("You input the following integer numbers: " + bigNumberOne + " " + bigNumberTwo); System.out.println("You may perform one of the following opperations:"); System.out.println("1. Add"); System.out.println("2. Subtract the first number from the second number"); System.out.println("3. Subtract the second number from the first number"); System.out.println("4. Multiplication"); System.out.println("5. Divide the first number by the second number"); System.out.println("6. Divide the second number by the first number"); System.out.println("7. Modulus the first number by the second number"); System.out.println("8. Modulus the second number by the first number"); System.out.println("9. Compare numbers for absolute value"); System.out.println("A. Compare numbers for equality"); System.out.println("B. Ignore numbers and start over"); do { System.out.println("Please enter a menu choice number."); menuChoice = keyboard.nextLine(); } while (!menuChoice.matches("([1-9A-Ba-b])")); switch (menuChoice) { case "1": System.out.println("Adding " + bigNumberOne + " to " + bigNumberTwo); bigNumberAnswer = bigNumberOne.add(bigNumberTwo); break; case "2": System.out.println("Subtracting " + bigNumberOne + " from " + bigNumberTwo); bigNumberAnswer = bigNumberOne.subtract(bigNumberTwo); break; case "3": System.out.println("Subtracting " + bigNumberTwo + " from " + bigNumberOne); bigNumberAnswer = bigNumberTwo.subtract(bigNumberOne); break; case "4": System.out.println("Multiplying " + bigNumberOne + " by " + bigNumberTwo); bigNumberAnswer = bigNumberTwo.multiply(bigNumberOne); break; case "5": try { System.out.println("Dividing " + bigNumberOne + " by " + bigNumberTwo); bigNumberAnswer = bigNumberOne.divide(bigNumberTwo); } catch (BigIntException dError) { System.out.println(dError.getLocalizedMessage()); mathError = true; } break; case "6": try { System.out.println("Dividing " + bigNumberTwo + " by " + bigNumberOne); bigNumberAnswer = bigNumberTwo.divide(bigNumberOne); } catch (BigIntException dError) { System.out.println(dError.getLocalizedMessage()); mathError = true; } break; case "7": try { System.out.println("Modulus " + bigNumberOne + " by " + bigNumberTwo); bigNumberAnswer = bigNumberOne.modulus(bigNumberTwo); } catch (BigIntException dError) { System.out.println(dError.getLocalizedMessage()); mathError = true; } break; case "8": try { System.out.println("Modulus " + bigNumberTwo + " by " + bigNumberOne); bigNumberAnswer = bigNumberTwo.modulus(bigNumberOne); } catch (BigIntException dError) { System.out.println(dError.getLocalizedMessage()); mathError = true; } break; case "9": compairing = true; System.out .println("Comparing the absolute value of " + bigNumberTwo + " to the aboslute value of " + bigNumberOne); areEqual = bigNumberTwo.equalsIgnoreSign(bigNumberOne); break; case "A": case "a": compairing = true; System.out.println("Comparing the value of " + bigNumberTwo + " to the value of " + bigNumberOne); areEqual = bigNumberTwo.equals(bigNumberOne); break; default: validNumbers = false; break; } if (validNumbers) { if (!compairing) { if (!mathError) System.out.println("Produces: " + bigNumberAnswer); } else { System.out.println( "Produces that it is: " + areEqual + " that " + bigNumberOne + " and " + bigNumberTwo + " are equal."); } } System.out.println("Would you like to enter new numbers?"); do{ System.out.println("Please enter yes or no"); yesNo = keyboard.nextLine();
} while (!yesNo.equalsIgnoreCase("yes") && !yesNo.equalsIgnoreCase("no")); } while (yesNo.equalsIgnoreCase("yes")); }
private static BigInt inputBigInteger() { BigInt newBigInt = null; String userIntInput = null; do { try{ System.out.println("Please enter an integer of any size."); userIntInput = keyboard.nextLine(); newBigInt = new BigInt(userIntInput); } catch (BigIntException myException){ System.out.println(myException.getMessage()); newBigInt = null;} } while (newBigInt == null); return newBigInt; }}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
