- Joined
- Jul 24, 2002
- Location
- Seattle, WA
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.
Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!
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;
}
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;
}
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;
}
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?
We were instructed to generate output through our methods and only use methods that were specified.
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
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.
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;
}
}