• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Last minute java help

silverpig

Lifer
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...
 
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.
 
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
}










 
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.
 
Back
Top