1. Member
Join Date
Mar 2011
Posts
1
Rep Power
0

Problem with ArrayLists

I am having a bit of problem with my code for this assignment I am currently working on. We are supposed to workout code to do each of the following methods. All of the methods I have working other than the multiplication one. We are supposed to use array lists to compute the multiplication of two big integers such as "12345 x 56789", for example. It needs to deal with different lengths of integers. If you can help at all, I would appreciate it. Thanks.

Java Code:
```import java.util.ArrayList;

// to another HugeInteger"
public class HugeInteger implements Comparable<HugeInteger>{

private ArrayList<Integer> digits = new ArrayList<Integer>();

@Override
public int compareTo(HugeInteger o) {

if ( digits.size() > o.getSize() )
return 1;
else if (digits.size() < o.getSize())
return -1;
for (int i = digits.size()-1; i >= 0; i--){
if (digits.get(i) > o.digits.get(i))
return 1;
else if (digits.get(i) < o.digits.get(i))
return -1;
}
return 0;
}

public int getSize(){
return digits.size();
}

/** Default constructor
*
*/
public HugeInteger(){

}

/**
* @param s the HugeInteger represented as a string
*/
public HugeInteger(String s){

}

/**
* @param i the HugeInteger as an int
*/
public HugeInteger(int n){
String num = n + "";

}
if (num.length() == 0)
else{
for (int i = num.length()-1; i >= 0; i-- ){
int n = Integer.parseInt(num.charAt(i)+ "");
}
}
}

/**
* @param o the HugeInteger object, compared to this
* @return
*/
public boolean equals(HugeInteger o){
if (this.compareTo(o) == 0)
return true;
return false;
}

/** Determines if the number is zero
* @return
*/
public boolean isZero(){
for ( int digit : digits){
if (digit != 0)
return false;
}
return true;
}

/**
* @param rhs the right hand siSde
* @return
*/
HugeInteger sum = new HugeInteger();
sum.digits.remove(0);

ArrayList<Integer> smallerNum;

ArrayList<Integer> biggerNum;

if ( digits.size() > rhs.digits.size()){
biggerNum = digits;
smallerNum = rhs.digits;
}
else{
biggerNum = rhs.digits;
smallerNum = digits;
}

int carry = 0;

for (int i=0;i < biggerNum.size();i++){
int temp;
if (i >= smallerNum.size())
temp = biggerNum.get(i) + 0 + carry;
else
temp = biggerNum.get(i) + smallerNum.get(i) + carry;

if ( temp >= 10){
carry = 1;
}
else {
carry = 0;
}

}
if (carry > 0) // changed from carry == 1

return sum;

}
public HugeInteger multiply(HugeInteger rhs){

HugeInteger product = new HugeInteger();
product.digits.remove(0);

ArrayList<Integer> smallerNum;

ArrayList<Integer> biggerNum;

if ( digits.size() > rhs.digits.size()){
biggerNum = digits;
smallerNum = rhs.digits;
}
else{
biggerNum = rhs.digits;
smallerNum = digits;
}

int carry = 0;
int temp;
int test;
HugeInteger sumTest = new HugeInteger();
sumTest.digits.remove(0);

for (int i = 0; i < smallerNum.size(); i++) {

//sumTest.digits.clear();

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

temp = smallerNum.get(i) * biggerNum.get(i) + carry;

if ( temp >= 10){
carry = temp / 10; // this will determine what the carry is. e.g. 81/10 = 8 (which is the largest carry can be with 9*9)
}
else {
carry = 0;
}

}
if (carry > 0)
for ( int k = 0; k < i; k++)

}
return product;

}

* @see java.lang.Object#toString()
*/
public String toString(){

String s = "";
for (int i = digits.size()-1; i >= 0; i--){
s +=  digits.get(i);
}
return s;
}

}```

2. You have to try them yourself, show us your result(s), why it doesn't work, and any errors you receive, before anyone here is going to work away at this. You have to show some effort before you'll receive any in return.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•