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

Thread: Balloon code

  1. #1
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Balloon code

    When posting code, please use highlight tags to preserve formatting. Also, you haven't described what exactly is going on. What do you mean by "it starts to go wrong"? What do you expect to happen? What happens instead? Have you stepped through this with a debugger, or at least with a piece of paper and a pencil? When does the code deviate from your expectations?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  2. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Pinares (January 26th, 2013)


  3. #2
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Balloon code

    the blue should produce that the first balloon's alt is 250 - 100, 150. instead it gives 400, and the next should say its alt is at the same, instead it stays at its own.

  4. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Balloon code

    I can't really read unformatted code. What happened when you stepped through this with a debugger?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #4
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Balloon code

    It gave me wrong outputs.

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Balloon code

    Where is the definition for the Balloon class with the method that is being debugged?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    Pinares (January 26th, 2013)

  8. #6
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Balloon code

    its on a different file in my src folder.

    --- Update ---

    When I replace the adjustAltitude methods in
    myBalloon.adjustAltitude(myBalloon.getAltitude() - 100);
    and
    myBalloon.adjustAltitude(myBalloon.getAltitude() * 3)
    with descendTo and ascendTo it gives me correct results. Any idea why that makes a difference?

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Balloon code

    Any idea why that makes a difference?
    Are you asking what the adjustAltitude() method does? It is Hard to say without seeing its code.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #8
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Balloon code

    I posted the class definition. The adjustAltitude method adjusts the altitude to w.e. i put it as long as its > 0. When I call it in the statement
    myBalloon.adjustAltitude(myBalloon.getAltitude() - 100);
    it should produce the output that myBalloon 'Spider-Man' has an alt of 150 (250-100), instead it gives me 400. But when i replace that method with the method descendTo, it gives me the correct output. Same thing happens with the line
    myBalloon.adjustAltitude(myBalloon.getAltitude() * 3)

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Balloon code

    When I execute the code I get this:
    The name of myBalloon is Spider-Man and has an altitude of 100
    The name of alsoMyBalloon is The Green-Goblin and has an altitude of 0
    Spider-Man's new altitude is 250
    The Green-Goblin's new altitude is 200
    Spider-Man's new altitude is 400
    The Green-Goblin's new altitude is 200
    Spider-Man's new altitude is 1600
    The Green-Goblin's new altitude is 0

    I can't easily see from your post of the output what is wrong with it.
    Can you add some comments to the output that says what is wrong with the output?
    Something like this: <<<<<<<<<< The altitude here should be 350
    If you don't understand my answer, don't ignore it, ask a question.

  12. #10
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Balloon code

    From my reading adjustAltitude *adds* the parameter to the current altitude.

  13. The Following User Says Thank You to curmudgeon For This Useful Post:

    Pinares (January 26th, 2013)

  14. #11
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Balloon code

    It just replaces the altitude to w.e is in the argument. The current altitude at the time of that first statement is 250, and when i subtract 100 it gives me 400 in the output.

  15. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Balloon code

    The normal meanings:
    To adjust is to change the current value by some amount.
    Adjust 10 by +5 gives 15
    To set means to change the value to a new value.
    Set 10 to 5 gives 5

    Check the code to see how it is changing the values.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #13
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Balloon code

    The problem is solved. using
    myBalloon.adjustAltitude(myBalloon.getAltitude() - 100);
    would mean add the altitude to itself (doubling it) then subtracting 100. by jusing
    myBalloon.adjustAltitude( - 100);
    it subtracts 100 from the current altitude. Thanks everybody for all your help.

Similar Threads

  1. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  2. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  3. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  4. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM
  5. Balloon.java, what in the world do i do?
    By toterzuko in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 13th, 2010, 05:19 PM