Question: Bigint I need help finishing up this project. I need to make a big into class that can add to big int using arraylist so
Bigint
I need help finishing up this project. I need to make a big into class that can add to big int using arraylist so far i have the add method but i need help creating a dividie method,multiplication method, and finishing the subtract method.
import java.util.*;
public class BigInt {
//Instance variables for the class, a boolean for the sign and an ArrayList to store the input String
private boolean pos = true;
private ArrayList
public BigInt () {
this.pos = true;
bigInt.add(0);
}
//Constructor for big integers input as strings
public BigInt (String newBigInt) {
dealWithSign(newBigInt);
cleanUpNumber(newBigInt);
for(int i = newBigInt.length()-1; i >=0; i--) { //My Modified code is length()+1 to length()-1
bigInt.add(Integer.parseInt(newBigInt.substring(i, i+1)));
}
}
//Private method to deal with the sign of the incoming big integer
private void dealWithSign(String num) {
if(num.charAt(0) == '+' || num.charAt(0) == '-') {
if(num.length() == 1) {
System.out.println("Invalid value: sign only, no integer.");
System.exit(0);
}
if(num.charAt(0) == '-') {
this.pos = false;
}
else {
this.pos = true;
}
num = num.substring(1);
}
}
//Private method to clean up the number; remove leading zeros, check for other errors, etc
private void cleanUpNumber(String num) {
if(num.charAt(0) == ' ') {
System.out.println("Invalid value: leading blank space.");
System.exit(0);
}
while(num.charAt(0) == '0' && num.length() > 1) {
num = num.substring(1);
}
}
public BigInt addBigInts(BigInt one, BigInt two) {
BigInt result = new BigInt();
if (one.pos && two.pos && one.bigInt.size() == two.bigInt.size()) {
for(int i=one.bigInt.size()-1; i >=0; i--) { int v1=one.bigInt.get(i); int v2=two.bigInt.get(i); result.bigInt.add(v1+v2);
if (result.bigInt.get(i) > 9) {
result.bigInt.add(0);
result.bigInt.add(i+1, (result.bigInt.set(i+1,result.bigInt.get(i+1) + 1))); }
}
} return result;
}
//Public method that performs subtraction
public void subtract(BigInt one, BigInt two) {
}
//toString method
public String toString() {
String answer = "";
for(int i=bigInt.size()-1; i>=0;i--)
answer = answer + bigInt.get(i); return answer;
} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
