tags:

views:

101

answers:

3

I've made a program for my OOP class that does the following:

  1. Defines a class to represent complex numbers
  2. Uses a constructor to initialize a Complex Number object
  3. passes and returns complex number objects from a function
  4. tests the complex number class in a driver, and
  5. optionally adds and subtracts complex numbers.

My Program works as follows:

  1. Prompts the user to Enter in the values for the real and imaginary parts of a complex number.
  2. Creates a ComplexNumber object using the user provided values as parameters to the ComplexNumber constructor.
  3. Prints the value of the complex number object, using the stand-alone print function.

I'm a little confused at this requirement though:

static void printComplexNumber(ComplexNumber n)

The function will print the Complex Number object passed as a parameter in the form realPart + imaginaryPart i

The realPart should be signed only if it is negative. The sign between the realPart and the imaginaryPart should be positive if the imaginaryPart is positive, and negative if the imaginaryPart is negative. If the real part is zero, only print the imaginary part, with the corrrect sign. If the imaginary part is zero, only print the real part. If the imaginary part is equal to one, just print the symbol i for the imaginary part.

Here's my question: What's the best(most efficient, clear, concise) way to determine when to print what?

My Code for my Print method:

static void PrintComplexNumber(ComplexNumber n)
{
    // what's the best thing to do here to determine what to print? 
    // if-else?
    // to get my real value and complex value, I have methods of the
    // ComplexNumber class entitled: GetRealPart(), and GetComplexPart().
}
+1  A: 

I'd just store the real and complex parts in (I presume integers or floats?) early on by invoking those methods. Then use if-else logic to figure out what you need.

In most cases, for something like this where you're just designing for logic and it's not high-performance (...or even something you'd actually have to write in the real world!) if-else statements should be all you need.

My advice:

  • Don't overthink it. Just write an if-else to implement each sentence in the description (where applicable). The compiler will make it reasonably fast.
  • If you're not sure about how (exactly) you should write the if logic, just write SOMETHING and test it, refining the logic as you discover mistakes.

I'm going to guess you're somewhat new to programming, so please permit me to add a general piece of advice...

  • Above all, don't stress out. Seriously. If you get bogged down in the details, it will only slow you down. If you don't know how to use a particular bit of syntax, look it up and try to find examples. And don't be afraid to ask questions, like you're already doing.

If you're not new to programming after all (maybe just C#?), please accept my apology. I don't mean to be condescending or anything.

EDIT: After the comments, I've decided to edit my answer.

You're already in the testing phase now (as I'd mentioned above-- you've written SOMETHING). Great start. Now you need to refine.

Your problem is the double minus, right? Well, you have a couple of options.

One, if you want a space between the sign and the real part (the requirements suggest this is possible), then the negative sign that's right before the real part is unusable since it will be tagged on with no space. So you can use some kind of absolute value function to make the number (temporarily) positive.

Something like this (pseudocode, sorry, since I don't really know C#):

double complexPart = getComplexPart();
double realPart = getRealPart();

bool realIsNegative = false;
if (Math.abs(realPart) != realPart) realIsNegative = true;

print complexPart + " ";
if (realIsNegative) print "- ";
else print "+ ";
print Math.abs(realPart);

It's a little unwieldy, and it could be neater if you use things like the conditional ternary operator and better indentation. But hopefully that makes some sense and will get you started.

Platinum Azure
real and complex parts are doubles.
Alex
and no, you are not condescending, I really appreciate the advice, I am new to programming, but intend to major in it. This is my first Object-Oriented Programming class.
Alex
but my main issue is that if the user enters a negative number for either the real part or complex part, it will print the negative sign with that number as well as the +/- sign inbetween.
Alex
Ah, in that case I'll amend my answer...
Platinum Azure
thanks! I'm testing now...but as far as my original question, I consider it answered. :)
Alex
A: 

For the sign conditions you could use ToString() with formatting. That leaves you with the zero and one cases that can be handled with if - else.

vladhorby
That could lead to more concise code, sure, and in C# that's a good approach. But I think it might be better (since this is homework after all) to write the logic directly, to get some practice.
Platinum Azure
+1  A: 

I'm going to answer your underlying question, which is what to do when there are confusing or complex conditions like this : you can solve it in a systematic way.

What I do is to first break it up into parts, it doesn't matter how verbose (as long as you have the time). So let's say, we divide the output as:

[RealSign][RealPart][ImSign][ImPart][iSymbol]

We then analyze when each will be displayed by writing down rules:

  • RealSign : RealPart < 0
  • RealPart : RealPart <> 0
  • ImSign : ImPart <> 0
  • ImPart : ImPart <> 0 || ImPart <> 1
  • iSymbol : ImPart <> 1

From here, you can start simplifying out stuff.

moogs
great approach!
Alex