Results 1 to 3 of 3
Thread: Complex Numbers Java Problem
 01012012, 11:19 PM #1Member
 Join Date
 Jan 2012
 Posts
 2
 Rep Power
 0
Complex Numbers Java Problem
Hey all! i'm having a problem with writing a complex number class.. i am having errors related to the toRect method, toPolar method, and conjugate (specifically the division part)... I can't figure out how to get around these errors. Basically, the program should have a tester(shown below)... the user should either use polar or rectangular... that is why i have created the toPolar method, it converts the rectangular form to a polar form but i'm not sure this is how it's supposed to be done. Any suggestions on how to work my errors out?
Thanks! :D
THIS IS THE CLASS
Java Code:public class ComplexNumber implements ComplexInterface { private double real,imaginary; //CONSTRUCTOR*********************************************** public ComplexNumber (double Re, double Im) { real = Re; imaginary = Im; } //************************************************************* public double getReal () { return real; } public double getImaginary () { return imaginary; } //CONJUGATE COMPLEX NUMBERS************************************ public ComplexNumber conjugate() { return new ComplexNumber (real, imaginary * (1)); } //ADD COMPLEX NUMBERS****************************************** public ComplexNumber add (ComplexNumber cn2) { double real1 = real + cn2.getReal(); double imaginary1 = imaginary + cn2.getImaginary(); return new ComplexNumber (real1, imaginary1); } //SUBTRACT COMPLEX NUMBERS************************************* public ComplexNumber subtract (ComplexNumber cn2) { double real2 = real  cn2.getReal(); double imaginary2 = imaginary  cn2.getImaginary(); return new ComplexNumber (real2, imaginary2); } //MULTIPLY COMPLEX NUMBERS************************************* public ComplexNumber multiply (ComplexNumber cn2) { double mul1 = real * cn2.getReal()  imaginary * cn2.getImaginary(); double mul2 = real * cn2.getImaginary() + imaginary * cn2.getReal(); return new ComplexNumber (mul1, mul2); } //DIVIDE COMPLEX NUMBERS************************************** public ComplexNumber divide (ComplexNumber cn2) { double con = cn2.getconjugate(); double phase1 = real * con.getReal()  imaginary * con.getImaginary(); double phase2 = real * con.getImaginary() + imaginary * con.getReal(); double phase3 = cn2.getReal() * con.getReal()  cn2.getImginary() * con.getImaginary(); double phase4 = phase1 / phase3; double phase5 = phase2 / phase3; return new ComplexNumber (phase4, phase5); } //CONVERT FROM POLAR TO RECTANGUAR**************************** public static ComplexNumber toRect (double mag, double angle) { double realv = mag * (Math.cos(angle)); double imaginaryv = mag * (Math.sin(angle)); return new ComplexNumber (realv, imaginaryv); } //CHECK IF COMPLEX NUMBERS ARE EQUAL************************** public boolean isLike (ComplexNumber cn2) { return ( real == cn2.getReal() && imaginary == cn2.getImaginary() ); } //Convert the numbers to polar******************************* public ComplexNumber toPolar (double real, double imaginary) { double magnitude = Math.sqrt(Math.pow(real, 2) + Math.pow(imaginary, 2)); double angles = Math.atan2(real, imaginary); return new ComplexNumber (magnitude, angles); } //Returns this complex number as a string********************* public String toString () { String result; if (real == 0) { if (imaginary == 0) { return "0"; } else{ if (imaginary == 0) { return String.valueOf(real); } else if (imaginary < 0) { return(real + " " + imaginary + "i"); } else { return(real + " +" + imaginary + "i"); } } }
Java Code:import java.util.Scanner; public class ComplexTester { public static void main (String[] args) { double num1, num2, num3, num4; String choose; Scanner scan = new Scanner(System.in); System.out.println ("Would you like to have polar or rectangular resuts?(polar/rectangular): "); choose = scan.nextLine(); System.out.println ("Enter the first real number: "); num1 = scan.nextDouble(); System.out.println ("Enter the first imaginary number: "); num2 = scan.nextDouble(); System.out.println ("Enter the second real number: "); num3 = scan.nextDouble(); System.out.println ("Enter the second imaginary number: "); num4 = scan.nextDouble(); ComplexNumber one = new ComplexNumber (num1, num2); ComplexNumber two = new ComplexNumber (num3, num4); ComplexNumber three, four, five, six, ad, sub, mult, div; System.out.println ("The first complex equation is:" +one); System.out.println ("The second complex equation is:" +two); if (one.isLike(two)) System.out.println ("one and two are equal"); else System.out.println ("one and two are not equal"); three = one.add(two); four = one.subtract(two); five = one.multiply(two); six = one.divide(two); ad = toPolar(three); sub = toPolar(four); mult = toPolar(five); div = toPolar(six); if (choose.equals("rectangular")) System.out.println ("complex equation one + complex equation two:" +three); System.out.println ("complex equation one  complex equation two:" +four); System.out.println ("complex equation one * complex equation two:" +five); System.out.println ("complex equation one / complex equation two:" +six); else System.out.println ("complex equation one + complex equation two:" +ad); System.out.println ("complex equation one  complex equation two:" +sub); System.out.println ("complex equation one * complex equation two:" +mult); System.out.println ("complex equation one / complex equation two:" +div); } }
 01012012, 11:57 PM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,690
 Rep Power
 13
Re: Complex Numbers Java Problem
Any suggestions on how to work my errors out?
Does the code compile but show unwanted or unintended behaviour at runtime (including runtime exceptions)? If so say exactly what the observed runtime behaviour is (output, exceptions). And also what behaviour you wanted or intended.

It is a *very* good plan in these cases to work one small step at a time, with much compiling and testing. The idea is that you should be dealing with one error at a time.
 01022012, 01:25 PM #3Member
 Join Date
 Jan 2012
 Posts
 2
 Rep Power
 0
Re: Complex Numbers Java Problem
@pbrockway2
well the errors start with the division Complex Number part:
Java Code:public ComplexNumber divide (ComplexNumber cn2) { double con = cn2.getconjugate(); double phase1 = real * con.getReal()  imaginary * con.getImaginary(); double phase2 = real * con.getImaginary() + imaginary * con.getReal(); double phase3 = cn2.getReal() * con.getReal()  cn2.getImginary() * con.getImaginary(); double phase4 = phase1 / phase3; double phase5 = phase2 / phase3; return new ComplexNumber (phase4, phase5); }
Java Code:public ComplexNumber toPolar (double real, double imaginary) { double magnitude = Math.sqrt(Math.pow(real, 2) + Math.pow(imaginary, 2)); double angles = Math.atan2(real, imaginary); return new ComplexNumber (magnitude, angles); }
Similar Threads

How complex is too complex for a single class?
By manji51 in forum AWT / SwingReplies: 7Last Post: 07082011, 07:20 PM 
Assigning null to Complex Type object in the context of memory in java
By sagngh8 in forum Advanced JavaReplies: 2Last Post: 06012011, 09:44 AM 
Problem getting numbers from user and finding smallest two numbers
By radhi16 in forum New To JavaReplies: 11Last Post: 01142011, 07:36 PM 
Java program problem.. Arrays.. Random Numbers
By Chewart in forum New To JavaReplies: 16Last Post: 11162009, 11:21 PM 
A bit of a complex reading and writing problem
By jigglywiggly in forum New To JavaReplies: 1Last Post: 10282009, 07:08 PM
Bookmarks