-1

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.

  • 5
    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 -- That's because he wants you to think about what sort of meaningful methods you might write. Setters and getters are not meaningful methods; they don't have any logic in them. Think about what sort of operations you might want to perform on a complex number. Hint: "output whether each individual number is real (a+0x), imaginary (0+bx), or complex (a+bx)" – Robert Harvey Mar 7 '15 at 0:53
  • 3
    Also, are you sure those are the only operations you're allowed to use, or does he want you to simply implement an isReal() method instead of making the "real" member public? – Ixrec Mar 7 '15 at 0:54
  • 1
    @Ixrec Just an equals method. – RudolphRedNose Mar 7 '15 at 1:44
  • 1
    If someone does not present all necessary information in a question to solve a given problem, I get the impression the person did not read the problem description carefully, otherwise he would not have left out important details. And as it now seems, the problem would not be very interesting without the stipulations you called "silly". – Doc Brown Mar 7 '15 at 7:33
  • 2
5

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.

  • Thank you, yes I am allowed an equals method. Nothing else. – RudolphRedNose Mar 7 '15 at 1:47
0

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.

  • Thanks, specifications explicitly state that I can't use toString() though. Should have mentioned that. – RudolphRedNose Mar 7 '15 at 1:59
  • Given that you can use equals(), Thomas' answer works. Though it's convoluted. – user949300 Mar 7 '15 at 2:18
  • 1
    @user949300: for people beeing used to work with complex numbers, Thomas' answer looks pretty straightforward, I guess. – Doc Brown Mar 7 '15 at 9:02
  • @Doc Brown. Creating a new Object, then doing some math subject to roundoff errors, vs. comparing a field to zero is convoluted. – user949300 Mar 8 '15 at 23:22
  • @user949300, How could round off errors happen with that algorithm? The only thing you're subtracting is the exact same number, both in real and in imaginary? Can subtracting the same number give not 0.0? – Arturo Torres Sánchez Mar 22 '15 at 23:27
-1

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

  • The downvote is because you're assuming the assignment is wrong, which it clearly is not. A Complex class with no isReal() method is useless for production code, but this is an exercise. – Arturo Torres Sánchez Mar 22 '15 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.