OK so I have a programming assignment and I need to create a class that represents Complex Numbers (good so far), add them to a list(good so far), and then output whether each individual number is real (a+0x), imaginary (0+bx), or complex (a+bx). A Complex object is made by initializing the real portion and imaginary portion
Complex(int _real, int _imag) { real = _real; imag = _imag; };
Complex(int _real) {real = _real; imag = 0;};
Complex() { real, imag = 0;}
The problem is my professor has silly stipulations such as all of the data fields must be private and there can be no get methods to access any portion of an indivdual Complex number from outside the class.
Instead the only public methods I have to work with are plus, subtract, and multiply methods that take another Complex number and return a sum, difference and product of the two respectively and a conjugate method that returns an individual Complex’s conjugate.
With only those four public methods at my disposal, what formula can I use to compute whether an individual number is real, imaginary or complex?
This is less a coding issue and more of a logic issue I know.
10
Let c = a + bi
and q
be the conjugate of c
.
Then c + q = 2a
and c - q = 2bi
. Now you’ve isolated twice the real and imaginary parts.
If your professor allows you to have an equals
method, you can compare these to zero (i.e. Complex(0, 0)
), and you have your answers.
If you literally can use only the four methods you mentioned (addition, subtraction, multiplication, and conjugation), determining your answer is provably impossible.
1
Note: This is a hack and not how “real” (ha-ha) code should be written
As Bogie said to Bergman, “We’ll always have toString()”.
Given the overly-restrictive parameters given by OP, the only solution I can think of is to put the information into the toString() method. With no serious thought about good code or testing, something like:
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(this.real);
sb.append( (this.imag >= 0.0) ? " +" : " ");
sb.append(this.imag);
if (this.real == 0.0) {
if (this.imag != 0.0)
sb.append(" imaginary");
}
else {
sb.append( (this.imag == 0.0) ? " real" : " complex");
}
// TODO note : not sure what 0.0, 0.0 counts as...
return sb.toString();
}
This is more of a “gee I’m clever Google Interview Answer”. In real life you’d ask your professor (or your stakeholder) what the heck they actually want.
p.s. Note that my direct comparisons to 0.0 are dubious, you probably want to test if the absolute value is less than a small epsilon.
6
<edit>I think you have misunderstood your professor’s intent. A complex number class without any way to access internal values would be useless. I would expect getRealPart() and getImaginaryPart() would be implemented, and getMagnitude() and getAngle() as well.
As to the down-voter, what exactly is your concern? </edit>
Your professor is probably steering you towards making your Complex class immutable. Take a look at the BigDecimal class – it uses this pattern
Don’t forget that in a member method like with a signature like Complex add(Complex x) you can access the fields of x directly – you don’t need getters, as the access rules of Java allow access.
I’m not sure why you need to test for real, imaginary or complex. If you need an isReal() member method, just test the imaginary part for zero.
Your class will have an equals() method, as it is a method of all Java objects. You will need to override this, and you should also override hashCode() while in the area.
1