Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

* Using Java Language and keeping the BigInt as a String Add four public methods to the BigInt Class. add(BigInt):BigInt subtract(BigInt):BigInt For each of these

*Using Java Language and keeping the BigInt as a String

Add four public methods to the BigInt Class.

  • add(BigInt):BigInt
  • subtract(BigInt):BigInt

For each of these new public methods use private helper methods to solve the arithmetic.

The BigIntTesterAdditionAndSubtractionOutput document shows the terminal output of a correctly working BigInt class with its error handler.

The method should also remove the zeros in front of the imputed big integer, and deals with adding and subtraction negative numbers to positive numbers.

* This is what I wrote so far:

package bigIntegerPackage; import java.util.ArrayList;

public class BigInt{ // variable that keeps tracks of the positive/negative number private Boolean positive = true; // list containing the absolute value of the number private ArrayList nums = new ArrayList<>();

public BigInt(String string) throws InvalidIntegerException { String s = setSignAndRemoveItIfItIsThere(string); removeZeros(string); for (int i = s.length() - 1; i >= 0; i--) { Character c = s.charAt(i); Boolean val = Character.isDigit(c);

if (val) { nums.add(Integer.parseInt(s.substring(i, i + 1))); } else { throw new InvalidIntegerException(); } } } private String setSignAndRemoveItIfItIsThere(String string)throws InvalidIntegerException {

Character c = string.charAt(0); if (c.equals('+') || c.equals('-')) {

// if only sign is there or empty string if (string.length() == 1 || c.equals(' ')) { throw new InvalidIntegerException(); } if (c.equals('-')) {// negative number positive = false; return string.substring(1, string.length()); } else if (c.equals('+')) {// positive number positive = true; return string.substring(1, string.length()); } while (c.equals('0')) {// positive number positive = null; return string.substring(1, (string.length()-1)); }

} else if (c.equals(' ')) {// case when the string starts with space throw new InvalidIntegerException(); } return string; } @Override public String toString() {

String result = ""; for (int i = nums.size() - 1; i >= 0; i--) { result = result + nums.get(i); }

if (!positive) { result = "-" + result;

} else if (nums.size() > 0) { result = "+" + result; }

return result;

} }

*This is the driver method: package bigIntegerPackage;

import java.util.Scanner; public class BigIntMathTesterOne { private static Scanner keyboard = new Scanner(System.in);

/** * @param args */ public static void main(String[] args) { /** * Sample valid input and resulting output * "44444444445555555555666666666677777777770000000000" * stores ["4","4","4","4","4","4","4","4","4","4","5","5","5","5","5","5","5","5","5","5","6","6","6","6","6","6","6'<'6","6","6","7","7","7","7","7","7","7","7","7","7","0","0","0","0","0","0","0","0","0","0"] in ArrayList and sets the value to positive *returns string "44444444445555555555666666666677777777770000000000" * "100000" stores ["1","0","0","0","0","0"] in ArrayList and sets the value to positive *returns string "100000" *"+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" *Sample errors *"5stt7" stores empty ArrayList, sets value to positive and returns string "empty" *"+" stores empty ArrayList, sets value to positive and returns "empty" *"-" stores empty ArrayList, sets value to positive and returns "empty" *" 500" stores empty ArrayList, sets value to positive and returns "empty" * */ BigInt bigNumberOne = null; String yesNo = "no"; String menuChoice = "q"; BigInt bigNumberTwo = null; BigInt bigNumberAnswer = null;

System.out.println("This program will perform basic math on large integer numbers"); do { 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. Ignore numbers and start over"); do { System.out.println("Please enter a menu choice number."); menuChoice = keyboard.nextLine(); }while(!menuChoice.matches("([1-9])")); 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 = bigNumberOne.subtract(bigNumberTwo); default: break; } System.out.println("Produces: " + bigNumberAnswer); 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(Exception myException) { System.out.println(myException.getMessage()); newBigInt = null; } }while(newBigInt == null); return newBigInt; }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions