Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 10th, 2005, 7:18 PM   #1
gamesage
Programmer
 
Join Date: Sep 2004
Location: Clemson, SC
Posts: 35
Rep Power: 0 gamesage is on a distinguished road
Send a message via AIM to gamesage
Smile Please Help ASAP algorithm problem

Could someone please help me with this? I'll admit this is a homework problem but I've tried to find a simple solution and I have yet to do so. The code is due tonight at 12pm EST. I realize there are small problems with the constructor, (i think size isn't very useful afterall) but my main problem is the add() method. I just can't get it to work properly. Any suggestions for the algorithm? You don't have to post code, but it would be nice.



import cucs.*;   
        
public class BigInt {
           
     private int[] digits;
     // size makes adding easier later
     private int size;
     
     public BigInt(int n) {
        int temp = n; 
        int count;
        for(count = 0; temp > 0; count++) {
           temp = temp / 10;
        }
        size = count;
        
        digits = new int[count];
        temp = n;
        for(count = digits.length - 1; count >= 0; count--) {
           digits[count] = temp % 10;
           temp = temp / 10;
        }
     }
           
     public void print() {
        StringBuffer sb = new StringBuffer();
        sb.append("");
        for(int i = 0; i <  digits.length; i++) {
           sb.append(digits[i]);
        }
        System.out.println(sb.toString());
     }
        
     public void read(BasicDataReader r) {
        digits = null;
        char temp;
        String s = r.readLine();
        // Determine integer value and store in digits array
        for(int i=0; i < s.length(); i++) {
           temp = s.charAt(i);
           digits[i] = (int)temp - (int)('0');
        }
     }
        
     public BigInt add(BigInt b) {
        int carry = 0;
        int num1;
        int num2;
        int sum;
        int lengthDiff;
        int largestSize;
        // Helps remember which array is longer.                      
        boolean first = false;  
        // Determine longest array and difference in length.
        if(digits.length > b.size) {
           largestSize = digits.length;
           lengthDiff = digits.length - b.size;
           first = true;
        }
        else {
           largestSize = b.size;
           lengthDiff = b.size - digits.length;
           first = false;
        }
        
        // Add the BigInts together
        for(int i = largestSize - 1; i >= 0; i--) {
           if(first) {
              num1 = digits[i];
              num2 = b.digits[i - lengthDiff];
           }
           else {
              num1 = digits[i - lengthDiff];   
              num2 = b.digits[i];
           }
        }
        return total;
     }
           
     public static void main(String [] args) {
     }  
}

I also get this error when trying to run a Test class. Know what it means? :

Exception in thread "main" java.lang.NoClassDefFoundError: Test/java


// Test the BigInt class.

public class Test {
     public Test () {
        BigInt testInt = new BigInt(12345);
        testInt.print();
     }

     public static void main(String [] args) {
     }
}
__________________
Free Porn!!
gamesage is offline   Reply With Quote
Old Feb 11th, 2005, 2:42 AM   #2
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
1. you dont need main in the class that you are calling.

2.
public BigInt add(BigInt b) {

return total }

when i put this into my compiler it said could not find total, and i cannot see total declared anywhere.

not sure your not calling anything in a main method which you need because thats the first method thats called when the program is started....
Berto is offline   Reply With Quote
Old Feb 11th, 2005, 7:59 AM   #3
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 381
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
The test class should be : .. to the best of my knoledge
// Test the BigInt class.

public class Test {
   public static void main(String [] args) {
     
        BigInt testInt = new BigInt(12345);
        testInt.print();
    

  }
}
i don't know about the rest ... don't have time to look at it
U'r trying to add 2 big numbers without BigInteger? .... or did i understood it all wrong?If u are, i can help u with some code ...
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Feb 14th, 2005, 12:30 AM   #4
gamesage
Programmer
 
Join Date: Sep 2004
Location: Clemson, SC
Posts: 35
Rep Power: 0 gamesage is on a distinguished road
Send a message via AIM to gamesage
Cool

thanks guys. I eventually figured it out. For some reason, while loops made it much easier. I'll never underestimate them again.
__________________
Free Porn!!
gamesage is offline   Reply With Quote
Old Feb 14th, 2005, 12:33 AM   #5
gamesage
Programmer
 
Join Date: Sep 2004
Location: Clemson, SC
Posts: 35
Rep Power: 0 gamesage is on a distinguished road
Send a message via AIM to gamesage
Talking

Oh, sorry about the lack of a description, I'll have to remember that next time. So is there a certain time frame that you're able to edit posts. I tried to edit the original the day after I posted but I wasn't able to...
__________________
Free Porn!!
gamesage is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC