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

Thread: New to Java!

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New to Java!

    Hello Javalites!

    I'm new here and new to programming as well. I have just started taking the java tutorials on the oracle website and so far, it's ok. I only have a quick question regarding the code snippet below (gotten from the oracle website still).

    class BicycleDemo {
        public static void main(String[] args) {
     
            // Create two different 
            // Bicycle objects
            Bicycle bike1 = new Bicycle();
            Bicycle bike2 = new Bicycle();
     
            // Invoke methods on 
            // those objects
            bike1.changeCadence(50);
            bike1.speedUp(10);
            bike1.changeGear(2);
            bike1.printStates();
     
            bike2.changeCadence(50);
            bike2.speedUp(10);
            bike2.changeGear(2);
            bike2.changeCadence(40);
            bike2.speedUp(10);
            bike2.changeGear(3);
            bike2.printStates();
        }
    }
     
    [B]The output of this test prints the ending pedal cadence, speed, and gear for the two bicycles:
     
    cadence:50 speed:10 gear:2
    cadence:40 speed:20 gear:3[/B]

    The output of the test is what I would like clarification for. Now for the first bike1 methods;

    bike1.changeCadence(50);
    bike1.speedUp(10);
    bike1.changeGear(2);
    bike1.printStates();

    I can understand if the output is cadence:50 speed:10 gear:2

    For the bike2 methods however;

    bike2.changeCadence(50);
    bike2.speedUp(10);
    bike2.changeGear(2);
    bike2.changeCadence(40);
    bike2.speedUp(10);
    bike2.changeGear(3);
    bike2.printStates();

    I can't understand why the output is cadence:40 speed:20 gear:3

    Can somebody please shed some light on the bike2 methods please? Thanks in anticipation


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: New to Java!

    What exactly don't you understand? The output is as expected.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: New to Java!

    What do you think the output should be and why?

  4. #4
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to Java!

    Thanks PhHein & GregBrannon for your prompt responses.

    I guess the specific question I need to ask is why speed in the second output is 20?

    Thanks again.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: New to Java!

    Again, reviewing the methods called on the object bike2, what do you think the speed of bike2 should be, and why? (I'm trying to get you to discover the answer yourself. Work with me.)

  6. #6
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to Java!



    Ok, cool. What I understand on the surface is this (and please forgive my fresher stupidity ):

    Bike 1 has 3 methods hence, the output is kind of obvious. Bike 2 on the other hand has 5 methods so first of all, I'm quite uncertain why there are just 3 outputs.

    2ndly, if I want to follow the pattern I observed, I feel the output should be:

    cadence:40 speed:10 gear:3

    My emphasis is on the speed method. Please I stand to be corrected and once again, forgive my fresher stupidity.

    Thanks!

  7. #7
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: New to Java!

    Uh oh, bike1 and bike2 are instances of Bicycle and have exactly the same number of methods. They're just called differently. Each method call changes the state of the instance. The state is defined by the Class members, it's attributes, those are always the same in number and type, no matter how many methods you call.
    You'll have to look at the implementation of those methods to see how they change the instance's state.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: New to Java!

    I'm glad you included more detail about your thinking, because it helps explain your confusion.

    I found the page you're working here.

    Understanding the bike1 and bike2 results requires the Bicycle class:
    class Bicycle {
     
        int cadence = 0;
        int speed = 0;
        int gear = 1;
     
        void changeCadence(int newValue) {
             cadence = newValue;
        }
     
        void changeGear(int newValue) {
             gear = newValue;
        }
     
        void speedUp(int increment) {
             speed = speed + increment;   
        }
     
        void applyBrakes(int decrement) {
             speed = speed - decrement;
        }
     
        void printStates() {
             System.out.println("cadence:" +
                 cadence + " speed:" + 
                 speed + " gear:" + gear);
        }
    }
    bike1 and bike2 are both instances of Bicycle. Both bike1 and bike2 have the same number of methods as Bicycle (5, can you name them?). The initial states of bike1 and bike2 are exactly the same and defined by the Bicycle class fields, cadence, speed, and gear.

    The final states of bike1 and bike2 are different, because bike1 and bike2 both have their own copies of the fields cadence, speed, and gear, and some of those fields are acted upon differently by the Bicycle class' methods. Since you're interested specifically in the differences in the speed, you should review the Bicycle.speedUp() method and then compare the bike1.speedUp() calls to the bike2.speedUp() calls to understand why the final speeds of bike1 and bike2 are different.

    Let us know if you still don't get it. This is a key point to understanding OOP, by the way, so don't move on UNTIL you get it.