• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with "complex" code..  RSS feed

 
Cliff Karlsson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, this probably sucks to look at but I am trying to solve a exercise where I need to do some math-operations on complex numbers in polar and rectangular form. But I am both lacking skills and am dead tired right now. What am I doing wrong?

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What am I doing wrong? 

Can you explain why you think there is something wrong? 
Please Copy any error messages and paste them here
or the program's output and explain what is wrong with it.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the code as you have written it, all of the methods should be declared as static, since none of them operate on the "this" instance.

If you want your Complex instances to be immutable, you should make the real and imz variables private, and provide getters for the real and imaginary parts (as well as angle and magnitude for polar representation).

then you should make all of the methods public instance methods. For example, the add method would be:


Your rectangularToPolar and polarToRectangular methods are useless. You are not converting anything.
Your mul method is very redundant.
Your div method is just plain incorrect.

to create a Complex from magnitude and angle you would have something like::

 
Cliff Karlsson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well if I use a main class and create two complex numbers and then create a third complex number with no parameters but I use the ".add" for example and enter my two created complex objects. When I try to print the result I only get the memory referense number. Same if I use. toString()
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only get the memory referense number.

Please copy what you are talking about and paste it here.

Same if I use. toString()

Where is the toString() method code for your class?
 
Cliff Karlsson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I did not understand objects as well as I thought before but I have been reading some more to understand objects better. The code now works but I would like to know how I could improve the code to make it look nicer. We where told that all objects should return new objects in this task so I could not have it accept doubles as input.

 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really want this on lines 51 and following?
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of other things:
  • Access: I would make the methods public rather than package private, the default access.
  • Spacing: You formatting is good except for spacing.  No double lines and always a blank line before methods.
  •  
    Cliff Karlsson
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Knute Snortum wrote:Do you really want this on lines 51 and following?


    Ahh, I don't know how to edit the post but I fixed it in the real code. Thanks.
     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Do you need to go through all the bother of using Pythagoras when you can use this method (and maybe this one)?
     
    praveen kumaar
    Ranch Hand
    Posts: 461
    22
    Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    .Indent your code(knute mentioned it,everything is ok except spacing).
    .add comments to part which is quite not intensional or to state some policy,add the documentation.
    .don't use variable names like x,y,z...indeed it should be somewhat that reveals its intention like in the method-"add" the parameter can be named "toAdd" instead of simply "y" similarly toMul...etc.
    .method should do a stuff what actually it is revealing like in your case you are initially calculating a magnitude in the "rectangularToPolar" method,although it is needed better option is to provide a concrete method for calculating magnitude.
    .you should better use public modifier for your class methods.
    .think about implementing comparable.---complex number can be compared---
    .override equals() and hashCode() for your class.
    .provide getters for the variables-real part,imag..part,angle..
    .you can use toPolar() and toRectangular() names instead of rectangularToPolar()....
    this is optional--you can use StrictMath class instead of Math...read about the difference(Google it!).

    Kind regards,
    Praveen.
     
    Fred Kleinschmidt
    Bartender
    Posts: 571
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Don't use Math.pow() fo compute the square of a number! Just use

    Your Complex class should NOT have methods rectangularToPolar() and polarToRectangular().
    Given an instance of your Complex class, how can you tell whether the internal values are stored as rectangular or as polar? Why should you care?
    What happens if you do this:

    What does c3 represent? Garbage!

    The whole point of having OOP is that you hide the implementation of the classe, and just have public methods to retrieve the desired information. You shouldn't care whether the values are stored as (real, imaginary) or as (magnitude,angle). All you should care about is whether you can retrieve the real part, or the imaginary part, or the magnitude, or the angle.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!