• Post Reply Bookmark Topic Watch Topic
  • New Topic

Minor Question on Adding Mechanism for Strings  RSS feed

 
Jimmy Howard
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, yes, this is a school project. I'm pretty sure I'm really close, but would love a bit of help or just a push in the right direction... I've put a decent amount of work into this.

Basically, there is a class "InfiniteInteger" which implements Comparable and can be used to store extremely long numbers as strings (i.e. "37493274839247923749234"). It has a couple functions such as "toString()", "compareTo()" and "isNegative()". Then there's the method I can't quite push through, "add(InfiniteInteger anInfiniteInteger)".

Somewhere along the way, it just won't carry numbers correctly... It should work for positive and negative numbers but doesn't quite.



I really appreciate any/all help! Thanks so much!

Further info: I can't use any external packages (i.e. Arrays, BigDecimal, ArrayList) and can't use long as a datatype either. If any more info is needed please let me know ASAP!!
 
Knute Snortum
Sheriff
Posts: 4284
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Wow, you like ternary operators!

Could you be more specific about the problem by Telling the Details? This help us not have to debug you program from scratch.

But I can tell you that line 44 is too complicated and needs to be broken into smaller lines. It makes it easier to understand and debug.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I right in believing that your code for figuring out whether to carry 10 over to the next column is that statement starting at line 44? That's one monster line of code... you might benefit from breaking it up into if-statements. I can't tell what it's supposed to do -- maybe it isn't doing it right.

However the following line wants the result variable to be 10 to cause carrying to happen. Here's an example to test it with: 7 + 6. What is "result" going to be? (Remember, I can't tell what that other code does so maybe it produces 10. Or maybe not.)
 
Jimmy Howard
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for the quick responses and warm welcome!

I'm sorry it's so convoluted. Honestly I wish line 44 didn't exist. Essentially, there is an if-else if-else statement (beginning at Line 40), which checks and assigns the sum[i] digit as well as the number to carry over. If it is over 10 it puts it in as whatever number (14) subtract 10 (so 4) and then carry = 1. But, if the numbers are negative things get more complicated... that's the "else if" statement.

The problem is focused on (1) -500 + -500 and (2) -500 + -499. The first one has -5 + -5 equaling -10 and so should equal zero and carry over a -1, while the second gives us -9 so no carry over. I'm having trouble differentiating those situations, and am worried that if I change too much it will stop working for any positive numbers, i.e. (3) -500 + 1 or (4) 1 + -500 or (5) 500 + 500.

Basically line 44 is horrid and it's me desperately trying to get something/anything to work, which hasn't been very successful.
 
Knute Snortum
Sheriff
Posts: 4284
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this: write down in English what line 44 is supposed to do. Don't worry about Java when you do. Then take each sentence and create a Java line from it. Now you can put print lines or assertions in between the lines.

Really, line 44 could be its own subroutine.
 
Jimmy Howard
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue is, I'm not really sure how to English it out, or even put it in pseudo-code.

I mean:

-5 + -5 = -10. Number is less than 0, less than -9: Carry the negative one, default to zero.
-4 + -5 = -9. Number is less than 0, greater than -9: Carry 0, assign "-9".
-4 + 5 = 1. Number is greater than 0, less than 10: ?

There are just a bunch of specific scenarios and it's a bit overwhelming to differentiate between them all...

Thank you for your response!
 
Knute Snortum
Sheriff
Posts: 4284
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd start like this:

  • The result of adding two digits will never be greater than 18 or less than -18
  • The digit I set will always be the right-most digit
  • I will always carry 0, 1, or -1


  • Can you make a working algorithm out of that? Do you need more? Exceptions?

    Create an algorithm and test it on some numbers. DON"T CODE!

    Does the algorithm work? Now you can code it into Java.
     
    Jimmy Howard
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry for the late reply, but I did actually get this to work. Granted, my answer was too slow and had some points deducted, but the hints here were very helpful steering in the right direction. I ended up doing the following (which I think could be consolidated into one loop, but I digress):

     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!