• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Java Help

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.
It's not that terrible. A couple of variables and you're set. And you're right that mod and integer division are your friends.
 
Here is the quick mock up of what I was thinking:

Code:
public HugeInteger add(HugeInteger i)
	{
		HugeInteger result = new HugeInteger();
		int remainder = 0;
		for (int index = 0; index < input.length(); index++) 
		{
			result.list[index] = list[index] + i.list[index] + remainder;
			if(result.list[index] < 10) 
			{
				remainder = 0;
			}
			else {
			    remainder = 1;
			    result.list[index] %= 10;
			}
		}
		
		return result;
	}

I'll have to write a toString method when I get home to test if it works.
 
I flipped it around.

Code:
	public HugeInteger add(HugeInteger i)
	{
		HugeInteger result = new HugeInteger();
		int remainder = 0;
		for (int index = input.length(); index >=0; index--) 
		{
			result.list[index] = list[index] + i.list[index] + remainder;
			if(result.list[index] < 10) 
			{
				remainder = 0;
			}
			else {
			    remainder = 1;
			    result.list[index] %= 10;
			}
		}
		
		return result;
	}

Now would it be possible to print the result from inside this method? When I try print command it just returns the memory address. I'm not sure if I can invoke a toString on an object. I know it works on integers.
 
Scratch that last one, I got it working :)

Code:
	public HugeInteger add(HugeInteger i)
	{
		HugeInteger result = new HugeInteger();
		int remainder = 0;
		for (int index = input.length(); index >=0; index--) 
		{
			result.list[index] = list[index] + i.list[index] + remainder;
			if(result.list[index] < 10) 
			{
				remainder = 0;
			}
			else 
			{
			    remainder = 1;
			    result.list[index] %= 10;
			}
		}
		for (int index = 0; index < input.length(); index++)
		{
			System.out.print(result.list[index]);
		}
		return result;
	}

petteyg359 thanks for pointing that out :thup:
 
Make a toString() method, and call that from the code where you're using the HugeInteger. Don't put statements that generate output inside a method that shouldn't be generating any output. That method should add and return a HugeInteger, and nothing more.
 
Ok so I'm at a brick wall again. This has been by far the most confusing project I have ever had.

We are required to add two of the numbers. I know it sounds simple but it really is not. First of all the previous method stored one number at a time into 25 element array and I have instantiated two object that will represent users input for two numbers.

Problem One; It is not possible two add two objects
Problem Two; It is not possible two add two numbers of that size and store them as int

I'm just lost as how we are supposed to add these two numbers? Like how can i add something that can't be stored into any primitive data type?


Are you able to use the "BigInteger" class? its part of the math library.

http://download.oracle.com/javase/6/docs/api/java/math/BigInteger.html
 
Are you able to use the "BigInteger" class? its part of the math library.

http://download.oracle.com/javase/6/docs/api/java/math/BigInteger.html

No because that would be doing the work for us, we are required to sort of write our own version of BigInteger class.

On a side note if anyone comes back to this thread for help there is a class which is part of standard java library called Character which has two methods that let you convert characters to integers. I just wish I knew about it before we figured out how to do it the hard way :p
 
You mean the easy way! :) I think it's better to know how it's done, personally. I mentioned the Character class later, but I intentionally didn't tell you about the method for getting a number from a primitive char because it's useful to know how char works.
 
You mean the easy way! :) I think it's better to know how it's done, personally. I mentioned the Character class later, but I intentionally didn't tell you about the method for getting a number from a primitive char because it's useful to know how char works.


You're right. I think this will come handy down the road plus it will look cool when I turn it in since our professor encourages thinking outside the box :thup:
 
So I got every other method working but I found a bug in my add method.

Ex:

If I add:
111
111
222 (equals)

If I add
111
11
221 (equals)

If I add
11
111
22 (equals)

It only seems to work if I add the digits of the same size otherwise it produces those results. Its adding numbers from left to right instead of right to left.
 
How are you storing values? If there were five digits to each value, how would you store "12"?

[0, 0, 0, 1, 2] ?
[1, 2, 0, 0, 0] ?

To make sure this doesn't break, you need to add right to left and you also need to make sure your numbers are right-aligned. There are other ways to do it, but this seems the most straightforward.
 
This is my method for reading in and storing numbers:

Code:
public void inputHugeIntegere()
	{
		Scanner scan = new Scanner (System.in);
		System.out.print("Please enter your huge integer: ");
		input = scan.next();
		
		for (int index = 0; index < input.length(); index++)
		{
			char c = input.charAt(index);
			int charValue = c - '0';
			list[index] = charValue;
		}
	}
 
So you tell me - would the string "12" get stored with [1, 2] at the left of the array or the right?

I used to TA for an intro Java class, so I can't help but be...unhelpful. :)
 
Its getting store from left to right.

I tested by printing index[0] for numbers like 256, 456...

and it always prints the first digit.
 
Right - and because you're iterating through the string from left to right, it's going to store the character at string index 0 at array index 0. So I think your add method will work properly if you store the string at the right side of the array instead of the left. Which makes sense if you consider the way zero padding works. 000015 is not the same as 150000, and right now you're storing "15" as [1, 5, 0, 0, 0, 0, ...].

Props for actually testing your add method!
 
Back