Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Complex Numbers

  1. #1
    Junior Member
    Join Date
    Feb 2019
    Posts
    21
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Unhappy Complex Numbers

    public class ComplexGet {
    	private double re, im;
     
    	public ComplexGet(double reŽel, double imag) {
    		re = reŽel;
    		im = imag;
    	}
     
    	public double real() {
    		return re;
    	}
     
    	public double imaginair() {
    		return im;
    	}
     
    	public void optellen(ComplexGet y) {
    		re = re + y.real();
    		im = im + y.imaginair();
     
    	}
     
    	public void vermenigvuldigen(ComplexGet y) {
     
    		re = ((re * y.real()) - (im * y.imaginair()));
    		im = ((re * y.imaginair()) + (im * y.real()));
     
    	}
     
    	public void inverteren(ComplexGet x) {
    		re = (x.real() / (x.real() * x.real()) + (x.imaginair() * x.imaginair()));
    		im = (x.imaginair() / (x.real() * x.real()) + (x.imaginair() * x.imaginair()));
    	}
     
    }

    public class main {
     
    	public static void main(String[] args) {
    		ComplexGet getal1 = new ComplexGet(1.0, 2.0);
    		ComplexGet getal2 = new ComplexGet(3.0, 4.0);
    		ComplexGet temp = getal1;
    		ComplexGet temp2= getal1;
    		ComplexGet temp3= getal1;
    		temp.optellen(getal2);
    		System.out.printf("Som = %.1f + %.1fi"+"\n", temp.real(), temp.imaginair());
    		temp2.vermenigvuldigen(getal2);
    		System.out.printf("Vermenigvuldiging = %.1f + %.1fi"+"\n", temp2.real(), temp2.imaginair());
    		temp3.inverteren(getal1);
    		System.out.printf("Inverteren = %.1f + %.1fi", temp3.real(), temp3.imaginair());
     
     
     
    	}
     
    }

    After the first "optellen" that means counting up the 2 numbers are counted up works fine. But then for "vermenigvuldigen", multiplication it takes the complex number of the som and getal2. And it should be getal1 and getal2 not the numbers from the som. Any help?
    Last edited by TmDee; March 16th, 2019 at 05:30 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,633
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Complex Numbers

    Can you copy the contents of the console from the program's execution and paste it here?
    Add some comments saying what is wrong and show what the desired output is.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Complex Numbers

    Not certain if this is the problem but the following code simply assigns temp, temp2, and temp3 the same reference. So if you make a change in one, the others are affected too.

    ComplexGet temp = getal1;
    ComplexGet temp2= getal1;
    ComplexGet temp3= getal1;

    Regards,
    Jim

  4. #4
    Junior Member
    Join Date
    Feb 2019
    Posts
    21
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Complex Numbers

    Quote Originally Posted by Norm View Post
    Can you copy the contents of the console from the program's execution and paste it here?
    Add some comments saying what is wrong and show what the desired output is.
    Knipsel.jpg
    Console says for "optellen 4.0+6.0i. <-- this is correct.
    for "vermenigvuldigen" : -12.0 + -30.0i
    for "inverteren" : 899.9 + 900.0i

    For multiplication, "vermenigvuldigen" the answer should be: 5.0 + 12i.
    It's wrong because he uses the (4.0 + 6.0i) to multiplicate with (3.0,4.0) and he should use (1.0,2.0) * (3.0,4.0) by the formula that is with "vermenigvuldigen".
    Same happens for 1/complex number . It should be 0.2 + -0.4i.

    Thanks in advance
    Last edited by TmDee; February 24th, 2019 at 03:59 PM.

  5. #5
    Junior Member
    Join Date
    Feb 2019
    Posts
    21
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Complex Numbers

    Quote Originally Posted by jim829 View Post
    Not certain if this is the problem but the following code simply assigns temp, temp2, and temp3 the same reference. So if you make a change in one, the others are affected too.

    ComplexGet temp = getal1;
    ComplexGet temp2= getal1;
    ComplexGet temp3= getal1;

    Regards,
    Jim
    Yeah saw that now too, thanks Jim!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,633
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Complex Numbers

    Please don't post images. The posted image is too small to read and text can not be copied from an image for including in a response.

    What is the results after you corrected the problem that Jim pointed out?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2019
    Posts
    21
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Complex Numbers

    Yeah well I saw that so i posted the answers of the console under the image and it just didn't make any difference by clearing those commands that Jim told me.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,633
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Complex Numbers

    it just didn't make any difference by clearing those commands that Jim told me.
    Can you post the new code that shows the changes you made?
    Also copy the contents of the console from when you execute the program and paste it here.


    A suggestion: Make the class immutable. Make the re and im variables final and have the computation methods return a new instance of the class.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2019
    Posts
    21
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Complex Numbers

    Quote Originally Posted by Norm View Post
    Can you post the new code that shows the changes you made?
    Also copy the contents of the console from when you execute the program and paste it here.


    A suggestion: Make the class immutable. Make the re and im variables final and have the computation methods return a new instance of the class.
    Som = 4,0 + 6,0i
    Vermenigvuldiging = -12,0 + -30,0i
    Inverteren = 899,9 + 900,0i
    This is the console log and it should be by "vermenigvuldigen" -5 + 12i.

    And your suggestion I don't really understand I'm just a beginner in Oriented and object programming I'm 18 and it's I only had like 3 weeks lessons about it

  10. #10
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Complex Numbers

    A subtle error I believe.

    re = ((re * y.real()) - (im * y.imaginair())); <----- re here is the current re
    im = ((re * y.imaginair()) + (im * y.real())); <---- re here is the just modified re which is not what you want.

    Regards,
    Jim

  11. #11
    Junior Member
    Join Date
    Feb 2019
    Posts
    21
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Complex Numbers

    public class main {
     
    	public static void main(String[] args) {
    		ComplexGet getal1 = new ComplexGet(1.0, 2.0);
    		ComplexGet getal2 = new ComplexGet(3.0, 4.0);
    		System.out.printf("Som = %.1f + %.1fi"+"\n", getal1.optellen(getal2).getReal(), getal1.optellen(getal2).getImaginair());
    		System.out.printf("Vermenigvuldiging = %.1f + %.1fi"+"\n", (getal1.vermenigvuldigen(getal2)).getReal(), (getal1.vermenigvuldigen(getal2)).getImaginair());
    		System.out.printf("Inverteren = %.1f + %.1fi", (getal1.inverteren()).getReal(), (getal1.inverteren()).getImaginair());
     
     
     
    	}
     
    }
    public class ComplexGet {
    	private double re, im;
     
    	public ComplexGet(double re, double im) {
    		this.re=re;
    		this.im=im;
    	}
     
    	public ComplexGet() {
    		this.re = 0;
    		this.im = 0;
    	}
     
    	public double getReal() {
    		return re;
    	}
     
    	public void setReal(double re) {
    		this.re = re;
    	}
     
    	public double getImaginair() {
    		return im;
    	}
     
    	public void setImaginair(double im) {
    		this.im = im;
    	}
     
    	public ComplexGet optellen(ComplexGet x) {
    		ComplexGet resultaat = new ComplexGet();
    		resultaat.re = this.getReal() + x.getReal();
    		resultaat.im = this.getImaginair() + x.getImaginair();
    		return resultaat;
     
    	}
     
    	public ComplexGet vermenigvuldigen(ComplexGet x) {
    		ComplexGet resultaat = new ComplexGet();
    		resultaat.re = ((this.getReal() * x.getReal()) - (this.getImaginair() * x.getImaginair()));
    		resultaat.im = ((this.getReal() * x.getImaginair()) + (this.getImaginair() * x.getReal()));
    		return resultaat;
     
    	}
     
    	public ComplexGet inverteren() {
    		ComplexGet resultaat = new ComplexGet();
    		resultaat.re = ((this.getReal()) / (this.getReal() * this.getReal())+ (this.getImaginair() * this.getImaginair()));
    		resultaat.im = ((this.getImaginair()) / (this.getReal() * this.getReal())+ (this.getImaginair() * this.getImaginair()));
    		return resultaat;
    	}

    The "vermenigvuligen" en "optellen" are now correct the only this that is wrong is "inverteren". It means the invert of an complex number.
    Som = 4,0 + 6,0i
    Vermenigvuldiging = -5,0 + 10,0i
    Inverteren = 5,0 + 6,0i
    This is what the console gives. The right solution by "inverteren" should be 0,2 + -0.4i

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,633
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Complex Numbers

    The conversion to an immutable (unchangeable) class is a good start.
    The next step would be to make re and im final so that their values can not be changed after being set in the constructor.
    Then in the optellen and vermenigvuldigen methods compute the re and im values and use them in the ComplexGet's constructor with the two arguments to create the instance to be returned.

    Why does the inverteren method use the current object's values? The old version did not which meant that it could have been a static method.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Complex Numbers

    You need to verify your calculations in the inverteren method.

    Regards,
    Jim

  14. #14
    Junior Member
    Join Date
    Feb 2019
    Posts
    21
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Complex Numbers

    Thanks Norm and Jim for the help! I found it let me show it.

    This is the main:
    public class main {
     
    	public static void main(String[] args) {
    		ComplexGet getal1 = new ComplexGet(1.0, 2.0);
    		ComplexGet getal2 = new ComplexGet(3.0, 4.0);
    		System.out.printf("Som = %.1f + %.1fi"+"\n", getal1.optellen(getal2).getReal(), getal1.optellen(getal2).getImaginair());
    		System.out.printf("Vermenigvuldiging = %.1f + %.1fi"+"\n", (getal1.vermenigvuldigen(getal2)).getReal(), (getal1.vermenigvuldigen(getal2)).getImaginair());
    		getal1.inverteren();
     
     
     
    	}
     
    }

    And this is the Class ComplexGet:
    public class ComplexGet {
    	private double re, im;
     
    	public ComplexGet(double re, double im) {
    		this.re=re;
    		this.im=im;
    	}
     
    	public ComplexGet() {
    		this.re = 0;
    		this.im = 0;
    	}
     
    	public double getReal() {
    		return re;
    	}
     
    	public double getImaginair() {
    		return im;
    	}
     
    	public ComplexGet optellen(ComplexGet x) {
    		ComplexGet resultaat = new ComplexGet();
    		resultaat.re = this.getReal() + x.getReal();
    		resultaat.im = this.getImaginair() + x.getImaginair();
    		return resultaat;
     
    	}
     
    	public ComplexGet vermenigvuldigen(ComplexGet x) {
    		ComplexGet resultaat = new ComplexGet();
    		resultaat.re = ((this.getReal() * x.getReal()) - (this.getImaginair() * x.getImaginair()));
    		resultaat.im = ((this.getReal() * x.getImaginair()) + (this.getImaginair() * x.getReal()));
    		return resultaat;
     
    	}
     
    	public void inverteren() {
    		System.out.printf("Inverteren = %.1f + %.1fi", (this.getReal()) / (this.getReal() * this.getReal()+ this.getImaginair() * this.getImaginair()), (-this.getImaginair()) / (this.getReal() * this.getReal()+ this.getImaginair() * this.getImaginair()));
    	}
     
    }

    And this is the console like it should be:
    Som = 4,0 + 6,0i
    Vermenigvuldiging = -5,0 + 10,0i
    Inverteren = 0,2 + -0,4i

    Thanks for the help again, appreciate it!

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,633
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Complex Numbers

    The class is not correct yet. The variables: re and im need to be final so that their values can not be changed.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Feb 2019
    Posts
    21
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Complex Numbers

    What do you mean final?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,633
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Complex Numbers

    final is a keyword that sets a variable's definition to say the variable's value can not be changed.

    final int notToBeChanged = 123;
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Complex Numbers

    But if you define it like this.

    final int val;

    It can be set initially, like in a constructor, but then can't change again. If you set the variables to private without outside access the users of your class can't change it. But making it final does protect you against accidentally changing the values yourself. Remember the 're' situation. It would have caught that. So it's a good idea to make your immutable variables final.

    EDIT: One more thing about immutable classes. When using getters to get some field, if you are not returning primitives (int, double, etc) or other immutable objects (String, Integer, Double), you should return copies of the objects. For example, consider an array of items. Don't return the array reference because someone can change the internals and affect the original value. Return a copy of the array. With a collection, wrap it in an unmodifiable wrapper or make a copy to return.

    Regards,
    Jim
    Last edited by jim829; February 26th, 2019 at 09:01 PM.

  19. #19
    Junior Member
    Join Date
    Feb 2019
    Posts
    21
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Complex Numbers

    Okay I read this and kinda understand this, but I think my prof will teach us later about that thanks tho both for the help! Really appreciate it!!

Similar Threads

  1. Complex Number Calculations
    By javaStooge in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 26th, 2014, 09:23 AM
  2. complex input parsing
    By Wolfone in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: September 3rd, 2013, 12:56 AM
  3. Complex numbers need help finishing
    By therealvasile in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 1st, 2013, 12:26 PM
  4. Complex Numbers in Java
    By JavaNovice03355 in forum Member Introductions
    Replies: 1
    Last Post: May 18th, 2011, 07:30 PM
  5. How do you design a complex program
    By mydarkpassenger in forum Java Theory & Questions
    Replies: 5
    Last Post: March 19th, 2010, 06:52 PM

Tags for this Thread