Results 1 to 15 of 15
  1. #1
    DomBrown is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default Return positive values only

    Hi,

    I am new to JAVA and i am about half way through a JAVA course and i have been set a challenge by my tutor but i can't figure it out on my own :eek:

    I have writen some code that relates to cars, it takes the velocity and it can amend the return value depending on if it accelerates, brakes or stops (as you will see in the code below)

    I've been asked to look in more details at the outcome if the velocity is lower then the braking value (which will return a negative value) how do i make it return 0 instead of returning a negative value.

    This is my code i used for marking so it does contains things that are not needed for my above question but please ignore them. In its current form it returns posative numbers but if i changed the velocity or brake to make it return a negative value...

    Any help would be great!!!

    file 1 - Car.java

    public class Car
    {

    int velocity;
    public Car()
    {
    velocity = 55;
    System.out.println("Inside default constructor");
    }

    public void accelerate()
    {
    velocity += 10;
    }

    public void brake()
    {
    velocity -= 20;
    }

    public void stop()
    {
    velocity = 0;
    }

    public void setVelocity(int newVelocity)
    {
    velocity = newVelocity;
    }


    public int getVelocity()
    {
    return velocity;
    }
    public Car(int a)
    {
    velocity = a;
    System.out.println("Inside other constructor");

    }

    }


    File 2 - CarProgram.java

    public class CarProgram
    {
    public static void main(String [] args)
    {
    Car porsche = new Car();

    porsche.setVelocity(55);

    System.out.println(porsche.getVelocity());

    porsche.accelerate();

    System.out.println(porsche.getVelocity());

    porsche.brake();

    System.out.println(porsche.getVelocity());

    porsche.stop();

    System.out.println(porsche.getVelocity());

    Car ford = new Car(30);

    ford.setVelocity(30);

    System.out.println("The initial velocity is " + ford.getVelocity());
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    DomBrown is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default Not Sure

    I'm not too sure tbh

    Looking at that it shows to apply the brakes if the car is moving. But the car is moving, if the velocity can be inputted by the user but is lower then the brake value (-20) how do i get it to return 0

    example,

    velocity = 1

    brake -= 20

    the return would be -19

    but it needs to be 0

    I thought of an if before but was not sure how to use it properly in this case

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Well, if you read down to the bottom of the tutorial, it shows you how to use inequalities in an if statement.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    DomBrown is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default Thank you!!

    ah, i get it now :D

    public void brake()
    {
    if (velocity >= 20)
    {
    velocity -= 20;
    }
    else if (velocity <20)
    {
    velocity = 0;
    }
    }

    it seems to work to :rolleyes:

    thanks alot!!!!!

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    That's one way to do it. Or you could subtract 20, then check for negatives. Either one is fine, and there are probably dozens of other ways to structure that simple if statement.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    DomBrown is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default ...

    thanks,

    its the first time i've used an if funtion in JAVA, i am used to them in Excel :D

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Cool. To be nitpicky, they aren't functions- they're control flow statements. But I'm glad you got it working.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    there are probably dozens of other ways to structure that simple if statement.
    I would do it in this computationally inefficient but easy-on-the-eyes way ;)
    Java Code:
    public void brake() {
        velocity = Math.max(velocity - 20, 0);
    }
    db

  10. #10
    DomBrown is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Cool. To be nitpicky, they aren't functions- they're control flow statements. But I'm glad you got it working.
    thanks, again, my Excel background kicking in :D

  11. #11
    DomBrown is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    I would do it in this computationally inefficient but easy-on-the-eyes way ;)
    Java Code:
    public void brake() {
        velocity = Math.max(velocity - 20, 0);
    }
    db
    that is alot easier on the eye. I'll look up math.max and find out how it works for future

    thanks for your help

  12. #12
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    I would do it in this computationally inefficient but easy-on-the-eyes way ;)
    Java Code:
    public void brake() {
        velocity = Math.max(velocity - 20, 0);
    }
    db
    Wait what? Math.max is inefficient in Java? I'd been assuming it was as efficient as a simple if/else statement to determine the larger value is this not the case?
    Currently developing Cave Dwellers, a Dwarf Fortress/Minecraft style game for Android.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Skiller View Post
    Wait what? Math.max is inefficient in Java? I'd been assuming it was as efficient as a simple if/else statement to determine the larger value is this not the case?
    Nah, it's not much more than you hoped for:

    Java Code:
        public static int max(int a, int b) {
    	return (a >= b) ? a : b;
        }
    *Stolen from src.zip in my JDK directory.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Nah, it's not much more than you hoped for:

    Java Code:
        public static int max(int a, int b) {
    	return (a >= b) ? a : b;
        }
    Ahh cool, that's what I was expecting it to do, was a little worried there for a second as I'm using max and min a lot in my game.
    Currently developing Cave Dwellers, a Dwarf Fortress/Minecraft style game for Android.

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    But remember, if that's the only use of Math, it'll cause the class to be loaded just for executing a single method. That's inefficient.

    db

Similar Threads

  1. Return values and Recursion
    By blug in forum New To Java
    Replies: 4
    Last Post: 11-14-2010, 01:55 PM
  2. Replies: 0
    Last Post: 11-03-2010, 05:35 PM
  3. Using functions that return values?
    By Megapixelz in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 05:07 AM
  4. how to return values from hashmap
    By oregon in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 05:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •