Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    0
    ReputationRep:
    Thanks alot
    Offline

    21
    ReputationRep:
    (edit: sorry got that wrong. Write a class Car? So Car has a
    Code:
    private int speed;
    The constructor is something like
    Code:
    Car() 
    { this.speed = 0;}
    . Then the methods
    Code:
    void Accelerate() 
    { this.speed += 15;}
    and
    Code:
    void Brake() 
    {this.speed -= 15;}
    )

    btw, this can easily be about C++ and Java too
    • PS Reviewer
    Offline

    16
    ReputationRep:
    (Original post by shawn_o1)
    (edit: sorry got that wrong. Write a class Car? So Car has a
    Code:
    private int speed;
    The constructor is something like
    Code:
    Car() 
    { this.speed = 0;}
    . Then the methods
    Code:
    void Accelerate() 
    { this.speed += 15;}
    and
    Code:
    void Brake() 
    {this.speed -= 15;}
    )

    btw, this can easily be about C++ and Java too
    We don't allow full solutions to be posted.. This is probably homework.

    You also don't need to reference yourself when changing the speed field. It's fine to write
    Code:
    Car() {
      speed = 0;
    }
    See http://msdn.microsoft.com/en-us/library/x9afc042.aspx for a second example.
    Offline

    1
    ReputationRep:
    (Original post by shawn_o1)
    (edit: sorry got that wrong. Write a class Car? So Car has a
    Code:
    private int speed;
    The constructor is something like
    Code:
    Car() 
    { this.speed = 0;}
    . Then the methods
    Code:
    void Accelerate() 
    { this.speed += 15;}
    and
    Code:
    void Brake() 
    {this.speed -= 15;}
    )

    btw, this can easily be about C++ and Java too
    This is correct except you are using a private constructor, which means the Car class can't be instantiated outside the class. At this level, I think the OP really wants a public constructor:

    Code:
    public Car() 
    {
    speed = 0;
    }
    He also needs an (why 2 in the OP?) accessor method to get the variable:

    Code:
    public int GetSpeed() 
    { 
    return speed;
    }
    If I'm being really pedantic I'd also use '_speed' to name the private field
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    What newspaper do you read/prefer?
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.