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 3 of 3

Thread: java is disregarding negatives at will?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default java is disregarding negatives at will?

    here is my current code:

    removing code for privacy purposes.
    as you can see, subtract and multiply are not giving what they should output (i haven't fiddled with divide yet). i have no idea how it got to -2 as it supposed to be doing (-3-2) . i'm just not sure what's causing it to go wrong as my code seems perfectly logical. help?


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: java is disregarding negatives at will?

    Quote Originally Posted by taze View Post
    as you can see, subtract and multiply are not giving what they should output
    I have not yet analyzed your code in detail but ..... at conceptual level if you do for example:

    a.add(b)

    The result should be provided in one of these two ways:
    - updating the a object (eventually returning the this, which is a).
    or
    - returning a new object.

    depending on if the objects of this class are "mutable" or "immutable". But normally not updating b which can lead to confusion!
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: java is disregarding negatives at will?

    Wrong output is because real and imaginary fields of oneValue and anotherValue are updated after add operation.

    Thats obvious when you use oneValue.add(anotherValue) and use the same variables to set the data.

    Rather you should have created a new Complex2() object inside those mathematical methods and return that value.
    Thanks and regards,
    Sambit Swain

  4. The Following User Says Thank You to Sambit For This Useful Post:

    taze (January 23rd, 2014)

Similar Threads

  1. Program should display number of positives\negatives\total\average
    By alias22 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 21st, 2012, 04:21 PM