Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2012
    Location
    NYC
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Problems with the code

    Here is my code ( or rather, the code that I ran from oracle)
    public class Main{
    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);
    }



    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();
    }
    }



    Now it wouldn't run through, and I assumed that it was the curly braces that didn't match up. I checked, but the code still would not run. Does anybody have any suggestion?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by dannyboi View Post
    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();
    }
    What are you doing with the curly braces there? Whilst you can open and close braces randomly in Java, the main does have a closing brace which places the calls on bike1 and bike2 later within the class body which isn't applicable.

    Are these both supposed to be combined in the class main? If so, it is missing a closing brace somewhere, else it's still missing a closing brace but I don't know where it would go. Nested classes also cannot have a main method.

    I'm also trying to figure out if the properties and the methods actually belong to the Bicycle (which they appear to) or if they belong to the main. The current code has them in main.
    PHP Code:
    public class Main{
        class 
    Bicycle {
        } 


  •  

    Tags for this Thread

    Posting Permissions

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