![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
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!! |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
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.... |
|
|
|
|
|
#3 |
|
Professional Programmer
|
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();
}
}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 ! |
|
|
|
|
|
#4 |
|
Programmer
|
thanks guys. I eventually figured it out. For some reason, while loops made it much easier. I'll never underestimate them again.
__________________
Free Porn!! |
|
|
|
|
|
#5 |
|
Programmer
|
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!! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|