Create a class HugeInteger which uses a 40-element array of digits to store integers as large as 40 digits each. Provide methods parse, toString, add and subtract. Method parse should receive a String, extract each digit using method charAt and place the integer equivalent of each digit into the integer array. For comparing HugeInteger objects, provide the following methods: isEqualTo, isNotEqualTo, isGreaterThan, isLessThan, isGreaterThanOrEqualTo and isLessThanOrEqualTo. Each of these is a predicate method that returns true if the relationship holds between the two HugeInteger objects and returns false if the relationship does not hold. Provide a predicate method isZero. If you feel ambitious, also provide methods multiply, divide and remainder.
All things are lawful, but not all things are profitable.
Knute Snortum wrote:* Don't import java.lang.*, that's automatically done. import java.util.* is unused. In general, I don't like wildcards in import statements. Be explicit.
Knute Snortum wrote:* I'd rather see you throw an InvalidArgumentException is the arguments are bad.
Knute Snortum wrote:* This code confused me:
Why the test for whether the character is a digit? And what about if the string starts with a "+"?
Knute Snortum wrote:* 'this' is generally used only to disambiguate an argument from a field. You don't need this.someMethod().
* 'this' is just plain wrong when calling static methods.
Knute Snortum wrote:* Your toString() method should use StringBuilder
Knute Snortum wrote:* I think you have a bug in this method:
What would this method return if it were passed { 1, 2, 5, 1 } and { 1, 2, 4, 9 }?
Knute Snortum wrote:* Is there an elegant way that add() and sub() can call a common method? There's a lot of common code.
* Can intAdd() and intSubtract() call a common method?
Knute Snortum wrote:* What happens in intAdd() if two maximum numbers are passed? (10 ^ 40 - 1)
* Same with intSubtract(): What is two minimum numbers are passed?
Knute Snortum wrote:Looks like you started multiply and divide but didn't finish.
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:What is the advantage of using an array instead of an ArrayList? And why limit te magnitude to a max of 40?
Junilu wrote:The name val is generic and it's an abbreviation. (...)
Junilu wrote:As for line 786, throwing a NumberFormatException doesn't make much sense.
Junilu wrote:that may be cool if you're trying to impress the ladies but it's not cool when you're designing an object.
Junilu wrote:I've seen more code where you use that internal intParse() method so it looks like I have to take back my previous statement that the intParse() method makes your HugeInteger class mutable. If it's called only from the constructors, then I was wrong.
Carlos Reves wrote:I indeed did resist the temptation of using BigIntegers to implement this HugeInteger class. But that doesn't mean that i wasn't curious to see how BigInteger is actually implemented! So i went to this site at grepcode. There i saw those two designations and i got curious about them. So i searched about them and found out about magnitude and about the Signum Function (or Sign Function). That was why i decided to use them also. I used -1, 0 and 1 for the signum because this is how this function is defined as you can see in the page.
Carlos Reves wrote:When you spoke about the mag name, i corrected that in my code and started to look at all the other abbreviations i had in my code. Val was one of them that i use a lot. I'm doing those changes all over the code to make the names more meaningfull.
Carlos Reves wrote:I just threw them where i thought they would fit and they are all the same NumberFormatException. I'll change it to an Arithmetic Exception.
Junilu wrote:f I'm correct on this, then I'd probably prefer to name this array something like segments instead.
Carlos Reves wrote:Since it's a mathematical related class and those are mathematical concepts it made sense in my head to use them (not because Joshua Bloch and his guys did it over there. In fact i used magnitude for my field name and not mag as they did. I only used mag in the arguments of constructors and methods).
Carlos Reves wrote:
Junilu wrote:f I'm correct on this, then I'd probably prefer to name this array something like segments instead.
Or maybe digits? Afterall each of magnitude[i] is a digit from the HugeInteger...
Junilu Lacar wrote:Of course. Poor choice of words on my part; it's a reflection of what I was thinking, not in any way trying to take away from your ability to make your own decisions.
Junilu Lacar wrote:
I don't think digits fits what the array is about.
Also, I'm not sure it's obvious (or even correct) to see magnitude[i] as representing one digit in a HugeInteger. The ints are converted to their binary equivalents and represent a segment of the HugeInteger's binary representation. That's why I thought of "segments" as an appropriate name.
Junilu Lacar wrote:Now that I know you're using BigInteger as a point of reference, "magnitude" is probably good enough, too.
Carlos Reves wrote:
Are they being converted? In BigInteger i know they are (it's a byte array). But i use an int array...
That's why i thought digits would fit...
Junilu Lacar wrote:I just realized why you'd think that. We weren't on the same page. I was still thinking in terms of binary representations. I see what you're getting at now. Another one of those wrinkles that I wasn't expecting. If that's how you want to use them, I suppose that's fine. I hesitate to say this because it's about optimization but to use an int to represent a value from 0-9 seems like a bit of a waste. Your intent might be more obvious if you declared a byte[] instead. This is where I think an explanatory comment would be appropriate to explain why you're declaring this field with whatever type and name you decide to go with.
Carlos Reves wrote:I could make that explanatory comment you suggest but... i believe it wouldn't be much of a flattering one...
See what i mean?
Junilu Lacar wrote:You don't have to be that self-deprecating. I actually have put comments like "Developer Note: just doing this for expediency right now; we'll need to refactor this once we know more about (the more complicated approach)" in my code just to remind myself and others what I was thinking at the moment.
Junilu Lacar wrote:By the way, if you're going to use that array to represent single digits, then it does make sense to name it digits. And I guess signum as used in BigInteger does stand for something that's not exactly reflected by the name sign very well, so if you want to go back to using signum that's probably fine, too, as long as you're going to use it the same way as it's used in BigInteger. Remember, I didn't know what the significance of the signum name was when I commented while under the impression that it was supposed to just represent the sign. Now that I know otherwise, I have no objection to signum.
Liutauras Vilda wrote:One small point:
When you read this line out loud, what you get is: "if not is less", now, consider having it just as "less", so you could read it "if not less".
It seems small thing, but in my opinion it improves code readability significantly, as makes you feel confident about the code you read, so you can be sure it indeed does what the spelt out words actually meant.
Liutauras Vilda wrote:And have a cow for incredibly good code formatting. I like it a lot.
Maybe then firstLessThanSecond instead of isLess? Because isLess could represent that second is less than first, you don't know that until you find relevant parts. But these are just mine thoughts, somebody else can have different opinion or think it is not important at all.Carlos Reves wrote:I used isLess because the logic for that variable was "the first is less than the second" as you can see in the following lines (i presume you took this from my compareMagnitude() method).
Carlos Reves wrote:So i went and read the page about code formatting and tried to improve and not make the same mistakes!
See how much time we spend for one variable? It can take even longer, for me usually takes long until I find what I like. All those small details makes you feel good about what you have come up with. When you need to keep up with deadlines, you might not always have time to spend decent amount of time to think about all well, but when you come back to code with more time, you should aim to polish every corner you can. With current IDE's we have, you need just to come up with a good name, and to change it in all places in one go it just seconds, so it shouldn't be a scary task.earlier I wrote wrote:Maybe then firstLessThanSecond instead of isLess? Because isLess could represent that second is less than first, you don't know that until you find relevant parts.
All things are lawful, but not all things are profitable.
All things are lawful, but not all things are profitable.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |