Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

* I need help modifing my code to fit the client program, it should have these constructors and remove all leading zeros. Please keep it

* I need help modifing my code to fit the client program, it should have these constructors and remove all leading zeros. Please keep it as an ArrayList. Thank You

multiply(BigInt):BigInt - BigInt modulus(BigInt):BigInt - BigInt divide(BigInt):BigInt - BigInt equals(BigInt):boolean - BigInt equalsIgnoreSign(Bibint):boolean -BigInt

*This is my program

package bigIntegerPackage;

import java.util.ArrayList;//ArrayList is from util package

public class BigInt{

private Boolean positive =true;

private ArrayList array;

public BigInt (String b1) {

String value = b1;

Character c = b1.charAt(0);

positive = true;

if (c.equals('+') || c.equals('-')){

if (b1.length() > 1){

value = b1.substring(1);

}

else {// throw new BigIntException();}

if (b1.charAt(0) == '-') {

positive = false;}

}

array = new ArrayList<>(value.length());

initialize(array,value.length());

for (int index = 0; index< value.length(); index++){

if (Character.isDigit(value.charAt(index))){

array.set(array.size() - 1 - index,Character.digit(value.charAt(index), 10));//set() method to change the value(assign new value)

}

else {// throw new BigIntException();}

}

System.out.println(this.toString());

}

private ArrayList removeLeadingZero(ArrayList first){

int index= first.size() - 1;

while (index> 0 && first.get(index) == 0)

index--;

ArrayList last = new ArrayList(index+1);

initialize(last, index+1);

for (int j = 0; j < last.size(); j++)

last.set(j,first.get(j));

return last;

}

private int compareArr(ArrayList first, ArrayList second){

if (first.size() != second.size())

return first.size()- second.size();

for (int index= first.size() - 1; index>= 0; index--){

if (first.get(index) != second.get(index))

return first.get(index) - second.get(index);

}

return 0;

}

private ArrayList addArr(ArrayList first, ArrayList second){

int size = Math.max(first.size(), second.size()) + 1;

ArrayList last = new ArrayList(size);

initialize(last,size);

// Please input a method to merge the two arraylist and use carry.

}

return removeLeadingZero(last);

}

private ArrayList subArr(ArrayList first, ArrayList second){

ArrayList last = new ArrayList(first.size());

initialize(last,first.size());

int carry = 0;

for (int index= 0; index< first.size() || index< second.size()|| carry != 0; index++){

int result = carry;

if (index< first.size())

result += first.get(index);

if (index< second.size())

result -= second.get(index);

carry = (result < 0 ? -1 : 0);

if (result < 0)

result += 10;

last.set(index,result);

}

return removeLeadingZero(last);

}

public BigInt add(BigInt second) //throws BigIntException //added here{

BigInt result = new BigInt("0");

if (positive && second.positive){

result.positive =true;

result.array = addArr(array, second.array);

}

else if (!positive && !second.positive){

result.positive = false;

result.array = addArr(array, second.array);

}

else if (!positive){

if (compareArr(array, second.array) >= 0){

result.positive = false;

result.array = subArr(array, second.array);

}

else{

result.positive = true;

result.array = subArr(second.array, array);

}

}

else{

if (compareArr(array, second.array) <= 0){

result.positive = false;

result.array = subArr(second.array, array);

}

else{

result.positive = true;

result.array = subArr(array, second.array);

}

}

return result;

}

public BigInt subtract(BigInt second)// throws BigIntException{

BigInt result = new BigInt("0");

if (!positive && second.positive){

result.positive = false;

result.array = addArr(array, second.array);

}

else if (positive && !second.positive){

result.positive = true;

result.array = addArr(array, second.array);

}

else if (!positive && !second.positive){

if (compareArr(array, second.array) >= 0){

result.positive = false;

result.array = subArr(array, second.array);

}

else{

result.positive = true;

result.array = subArr(second.array, array);

}

}

else{

if (compareArr(array, second.array) <= 0){

result.positive =false;

result.array = subArr(second.array, array);

}

else{

result.positive = true;

result.array = subArr(array, second.array);

}

}

return result;

}

@Override

public String toString(){

StringBuilder string = new StringBuilder();

for(int index=0; index

string.append(array.get(array.size() - 1 - index));

}

if (positive) {

string.append('+');

}

else {

string.append('-');

}

return string.toString();

}

private void initialize(ArrayList array,int length){

for(int i=0;i

array.add(0);

}

}

}

This is the client program: package bigIntegerPackage;

import java.util.Scanner; public class BigIntMathTesterOne { private static Scanner keyboard = new Scanner(System.in); 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 bigNumberAnswerAdd = null; BigInt bigNumberAnswerSub1 = null; BigInt bigNumberAnswerSub2 = null; BigInt bigNumberAnswerMul = null; BigInt bigNumberAnswerDiv1 = null; BigInt bigNumberAnswerDiv2 = null; BigInt bigNumberAnswerMod1 = null; BigInt bigNumberAnswerMod2 = 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("Adding " + bigNumberOne + " to " + bigNumberTwo); bigNumberAnswerAdd = bigNumberOne.add(bigNumberTwo); System.out.println("Subtracting " + bigNumberOne + " from " + bigNumberTwo); bigNumberAnswerSub1 = bigNumberOne.subtract(bigNumberTwo); System.out.println("Subtracting " + bigNumberTwo + " from " + bigNumberOne); bigNumberAnswerSub2 = bigNumberTwo.subtract(bigNumberOne); : System.out.println("Multiplying " + bigNumberOne + " by " + bigNumberTwo); bigNumberAnswerMul = bigNumberTwo.multiply(bigNumberOne); System.out.println("Dividing " + bigNumberOne + " by " + bigNumberTwo); bigNumberAnswerDiv1 = bigNumberOne.divide(bigNumberTwo); System.out.println("Dividing " + bigNumberTwo + " by " + bigNumberOne); bigNumberAnswerDiv2 = bigNumberTwo.divide(bigNumberOne); System.out.println("Modulus " + bigNumberOne + " by " + bigNumberTwo); bigNumberAnswerMod1 = bigNumberOne.modulus(bigNumberTwo); System.out.println("Modulus " + bigNumberTwo + " by " + bigNumberOne); bigNumberAnswerMod2 = bigNumberTwo.modulus(bigNumberOne); ); 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

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Luc Perkins, Eric Redmond, Jim Wilson

2nd Edition

1680502530, 978-1680502534

More Books

Students also viewed these Databases questions