Results 1 to 2 of 2
  1. #1
    fatedorigin is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default 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;
    	}
    
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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

  1. array of ArrayLists
    By imorio in forum New To Java
    Replies: 8
    Last Post: 09-24-2011, 02:05 PM
  2. ArrayLists
    By Freakzoyd in forum New To Java
    Replies: 4
    Last Post: 11-12-2010, 05:27 AM
  3. Problem with ArrayLists
    By g2beastie in forum New To Java
    Replies: 7
    Last Post: 04-08-2010, 04:46 AM
  4. arraylists problem
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 08:38 AM

Posting Permissions

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