Last minute java help

silverpig

Lifer
Jul 29, 2001
27,703
12
81
Okay, I've got an object called ComplexNumber with 2 attributes, realpt and imagpt.

I've made a few of these objects via

ComplexNumber A = new ComplexNumber(realpt, imagpt);

Same for B.

I now want to add the two numbers. How do I define my add function so that the call:

A.add(B);

Adds the real part of A to the real part of B and the imaginary part of A to the imaginary part of B?

I know how to do the math behind it, I just don't know what my add function should look like. I've got:

public void add(what goes here??? ComplexNumber?)
{
real = real + realpt???
imaginary = imaginary + imagpt???
}


I've got two .java files. One is ComplexNumber, and it has my constructors, and my add/set/write/divide functions, and a ComplexNumberUser which asks the user for realpt and imagpt, creates the instances of ComplexNumber, and does the function calls...
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Something like this:

public void add(ComplexNumber B){
realpt += B.realpt;
imagpt += B.imagpt;
}
 

dexvx

Diamond Member
Feb 2, 2000
3,899
0
0
Assuming realpt and imagpt are instance variables (lets say int) inside your object, ComplexNumber.

If you want to make it so:

ComplexNumber A,B; (assume you assign values)
A.Add(B);

runs as, B's values gets added into A's. heres the following:

public void Add (ComplexNumber addMe)
{
this.realpt += addMe.realpt;
this.imagpt += addMe.imagpt;
}

That is the simplest verson I can think of, minus all the Exception checking.
 

Juniper

Platinum Member
Nov 7, 2001
2,025
1
0
Originally posted by: silverpig
Okay, I've got an object called ComplexNumber with 2 attributes, realpt and imagpt.

I've made a few of these objects via

ComplexNumber A = new ComplexNumber(realpt, imagpt);

Same for B.

I now want to add the two numbers. How do I define my add function so that the call:

A.add(B);

Adds the real part of A to the real part of B and the imaginary part of A to the imaginary part of B?

I know how to do the math behind it, I just don't know what my add function should look like. I've got:

public void add(what goes here??? ComplexNumber?)
{
real = real + realpt???
imaginary = imaginary + imagpt???
}


I've got two .java files. One is ComplexNumber, and it has my constructors, and my add/set/write/divide functions, and a ComplexNumberUser which asks the user for realpt and imagpt, creates the instances of ComplexNumber, and does the function calls...


Add function should be in the same class as ComplexNumber.

constructor:
ComplexNumber(realpt, imagpt)
my_realpt = realpt;
my_imagpt = imagpt;

add function:

add(ComplexNumber)
{
sum_realpt = this.my_realpt + ComplexNumber.realpt
sum_imagpt = this.my_imagpt + ComplexNumber.imagpt
}










 

silverpig

Lifer
Jul 29, 2001
27,703
12
81
Thanks guys. I got it working somehow. I have NO idea why it works, it just does with everything I can throw at it. I'm sure it's the most horribly written code ever, but I don't really care so long as it works :)


Oh, and I ended up with something like this:

public void subtract(ComplexNumber ComplexNumber)
{

temp1 = ComplexNumber.getReal();
temp2 = ComplexNumber.getImaginary();

real = real - temp1;
imaginary = imaginary - temp2;
JOptionPane.showMessageDialog(null, "Difference = " + real + " + " + imaginary + "i");
}


where getReal() and getImaginary() are double returning functions that just pull off that specific part of the complex number. Ugly, but it works.