I'll repeat what I have to do in java, in the way I think I need to think in order to complete this mission. (Sorry, I'm new to programming).
First class; define class for complex numbers. I found this fairly easy and my answer is below.
public class Complex {
private double real;
private double imaginary;
public Complex()
{
this( 0.0, 0.0 );
}
public Complex( double r, double i )
{
real = r;
imaginary = i;
}
}
Second class; add and subtract with public static methods, recalling real and imaginary from first class. This part I found a little bit more challenging as I don't 100% grasp this.
Public class ComplexArith
public static ComplexAdd(Complex one, Complex two)
return Complex(one.getReal() + two.getReal(), one.getImaginary() + two.getImaginary());
public static ComplexSub(Complex one, Complex two)
return Complex(one.getReal() - two.getReal(), one.getImaginary - two.getImaginary());
The third part is to ask for user input, and add and subtract the set of complex numbers. I'm unfamiliar with this as I've never had to ask for user input in a (0.0,0.0) format.
Any insight on the overall code? Am I even on the right track?
EDIT:
The first class I banged out. Thanks to you guys.
Second class I'm having compiling issues because I'm not fully understanding something.
public class ComplexArith{
public static Complex add(Complex one, Complex two)
{
return Complex(one.getReal() + two.getReal(), one.getImaginary() + two.getImaginary());
}
public static Complex sub(Complex one, Complex two)
{
return Complex(one.getReal() - two.getReal(), one.getImaginary - two.getImaginary());
}
}
I know that one and two need to be defined, but I'm not understanding how to define them. What would I define them as? I thought it was being called from class Complex from double r, double i.
I also thought .getImaginary was being defined as well in the first class. Here is the first class.
public class Complex
{
private double real;
private double imaginary;
public Complex()
{
this( 0.0, 0.0 );
}
public Complex( double r, double i )
{
real = r;
imaginary = i;
}
public double getReal() {
return this.real;
}
public double getImaginary() {
return this.imaginary;
}
}
Well you're on the right track, but you need getters on your Complex object.
For example:
public class Complex
{
private double real;
private double imaginary;
public Complex()
{
this( 0.0, 0.0 );
}
public Complex( double r, double i )
{
real = r;
imaginary = i;
}
public double getReal() {
return this.real;
}
public double getImaginary() {
return this.imaginary;
}
}
You also need return types on your methods:
public class ComplexArith
{
public static Complex complexAdd(Complex one, Complex two) {
return Complex(one.getReal() + two.getReal(),
one.getImaginary() + two.getImaginary());
}
public static Complex complexSub(Complex one, Complex two) {
return Complex(one.getReal() - two.getReal(),
one.getImaginary - two.getImaginary());
}
}
Also, it has nothing to do with the functionality of your program, but it's customary to have your methods use camelCase. So your methods should look like this:
public static Complex complexAdd(Complex one, Complex two) {
return Complex(one.getReal() + two.getReal(),
one.getImaginary() + two.getImaginary());
}
Personally, I'd put those arithmetic operations in the Complex class. Those are truly operations on Complex numbers, so I wouldn't encapsulate them outside the Complex class.
I'd think about making Complex immutable. It's thread safe that way.
I like the static add, sub, mul, div methods. Be sure that they return a Complex (they don't now). Other methods, like cosine, sine, etc. might belong in a Math class in your complex package. See the java.lang.Math for an example for real numbers.
You need to return "new Complex". The code you wrote won't compile.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With