To get the info out of an object.
I have the following code:
public class NaturalNumber
{
private char[] data;
public NaturalNumber(char[] d)
{
data = d;
}
public NaturalNumber add(NaturalNumber n)
{
char[] first = this.data;
For a school we are creating a binary math project. I can't recall how to get the NaturalNumber object that I have created from a char[]. I thought that the line of char[] first = this.data; would work, but it doesn't appear to. Could somebody please refresh me on how to accomplish this task.
Thanks
Glenn
[588 byte] By [
heelera] at [2007-10-2 11:23:39]

Can you please rephrase your question. Please use the Code Tags http://forum.java.sun.com/help.jspa?sec=formatting when posting code
Maybe you to archive something like
NaturalNumber n1 = new NaturalNumber("123".toCharArrry()) ;
n1.add(new NaturalNumber("456".toCharArrray() );
Your add method than looks something like
public NaturalNumber add(NaturalNumber n)
{
char[] rhs = n.data;
// do something magic like
// this.data[0] += rhs[0];
}
The line
char[] first = this.data
copies a reference the data member points to into a local variable.
Sorry, I'll try to follow the rules better.
This code is in a NaturalNumberProject class.
NaturalNumber m = new NaturalNumber(d);
NaturalNumber r = m.add(n);
So I pass my call the add method on my NaturalNumber M and send it NaturalNumber n
My method is to add the two binary NaturalNumber objects together and return the sum as r.
public NaturalNumber add(NaturalNumber n)
{
char[] first = this.data;
char[] second = n.data;
I am able to get char[] second to work and give me the data, but have been unable to get the char[] first to work. When I step through my algorithm, it treats the array as if it is full of '0' chars. even though I have the NaturalNumber m object filled with '1's and '0's. Hope that this is a better explination. There is a program that this has to interface with that was given to us by the Professor.
Thanks again
Glenn
> When I step through my algorithm, it treats the array as if it is full of '0' chars. even though I have the NaturalNumber m object filled with '1's and '0's.
The line
char[] first = this.data;
is correct.
There must be another problem with your Algorithm or how you initialize your first NaturalNumber object.
Hope i understood what you want:
public class NaturalNumber
{
private char[] data;
public NaturalNumber(char[] d)
{
data = d;
}
public NaturalNumber add(NaturalNumber n)
{
char[] first = this.data;
char[] second = n.data;
char[] sum; //do your calculation for the sum[]....
NaturalNumber r = new NaturalNumber(sum);
return r;
}
}
you have just to write the formula for the summation, dunno how you expect it.. just give it a value (depending of "first" and "second" of course) before you use it in the constructor..
abeda at 2007-7-13 4:27:53 >

Here is my code for my add process.
package math;
public class NaturalNumber
{
private char[] data;
public NaturalNumber(char[] d)
{
data = d;
}
public NaturalNumber add(NaturalNumber n)
{
char[] first = this.data;
char[] second = n.data;
int arrayLength = first.length;
if(second.length > arrayLength)
{
arrayLength = second.length;
}
char[] sum = new char[arrayLength+1];
int i = 0;
char carry = '0';
while(i < arrayLength)
{
if(first[i] == '1')
{
if(second[i] == '1' && carry == '1')
{
sum[i] = '2';
carry = '1';
}
else if(second[i] == '0' && carry == '0')
{
sum[i] = '3';
carry = '0';
}
else
{
sum[i] = '4';
carry = '1';
}
}
else if(first[i] == '0');
{
if(second[i] == '0' && carry == '0')
{
sum[i] = '9';
carry = '8';
}
else if(second[i] == '1' && carry == '1')
{
sum[i] = '7';
carry = '6';
}
else
{
sum[i] = '5';
carry = '4';
}
}
i++;
}
if(carry == '1')
{
sum[i] = carry;
}
NaturalNumber newNumber = new NaturalNumber(sum);
return newNumber;
}
}
The data object that gets put in the char[] first array is created with the same method as
mport math.NaturalNumber;
public class NaturalNumberProject extends assignments.Project1{
public NaturalNumberProject(application.Math360Application parent){
super(parent);
}
protected String add(String a, String b)
{
char[] d = new char[a.length()];
for (int i = 0; i < a.length(); i++)
d[i] = a.charAt(a.length() -1 -i);
NaturalNumber n = new NaturalNumber(d);
d = new char[b.length()];
for (int i = 0; i < a.length(); i++)
d[i] = b.charAt(a.length() -1 -i);
NaturalNumber m = new NaturalNumber(d);
NaturalNumber r = m.add(n);
return r.writeToString();
}
}
So I really don't understand where it works differently than what you have put. My algorithm never goes into the if(first = '1') statement. It acts as if it is always '0'. It does go into the else if statement.
Thanks for your help.
Glenn
> ...> else if(first == '0');Else, if first == '0', do nothing.The lines that follow are not inside any if-statement, so they are executed no matter what the value of first[0] would be.
There is a semicolon
...
else if(first[i] == '0');
// the following block is allways executed because of the semicolon in the line above
{
if(second[i] == '0' && carry == '0')
{
...
}
...
}
Dang ; I was looking and looking and kept missing it.Thanks very much for you're help.Glenn