Results 1 to 2 of 2
Thread: Problem with ArrayLists
- 03-14-2011, 11:21 PM #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; // the class header would be read "HugeInteger is comparable // 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(){ digits.add(0); } /** * @param s the HugeInteger represented as a string */ public HugeInteger(String s){ loadString(s); } /** * @param i the HugeInteger as an int */ public HugeInteger(int n){ String num = n + ""; loadString(num); } private void loadString(String num){ if (num.length() == 0) digits.add(0); else{ for (int i = num.length()-1; i >= 0; i-- ){ int n = Integer.parseInt(num.charAt(i)+ ""); digits.add(n); } } } /** * @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 */ public HugeInteger add(HugeInteger rhs){ 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){ sum.digits.add(temp % 10); carry = 1; } else { sum.digits.add(temp); carry = 0; } } if (carry > 0) // changed from carry == 1 sum.digits.add(carry); // changed from .add(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++) { product.add(sumTest); //sumTest.digits.clear(); for (int j = 0; j < biggerNum.size(); j++){ temp = smallerNum.get(i) * biggerNum.get(i) + carry; if ( temp >= 10){ sumTest.digits.add(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 { sumTest.digits.add(temp); carry = 0; } } if (carry > 0) sumTest.digits.add(carry); for ( int k = 0; k < i; k++) sumTest.digits.add(0,0); } return product; } /* (non-Javadoc) * @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; } }
- 03-15-2011, 12:18 AM #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.
Similar Threads
-
array of ArrayLists
By imorio in forum New To JavaReplies: 8Last Post: 09-24-2011, 01:05 PM -
ArrayLists
By Freakzoyd in forum New To JavaReplies: 4Last Post: 11-12-2010, 04:27 AM -
Problem with ArrayLists
By g2beastie in forum New To JavaReplies: 7Last Post: 04-08-2010, 03:46 AM -
arraylists problem
By newtojava7 in forum New To JavaReplies: 1Last Post: 03-12-2008, 07:38 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks