The Student Room Group

help C#

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

btw, this can easily be about C++ and Java too
(edited 9 years ago)
Original post by shawn_o1
(edit: sorry got that wrong. Write a class Car? So Car has a private int speed; The constructor is something like Car()
{ this.speed = 0;}
. Then the methods void Accelerate()
{ this.speed += 15;}
and 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 Car() {
speed = 0;
}


See http://msdn.microsoft.com/en-us/library/x9afc042.aspx for a second example.
Original post by shawn_o1
(edit: sorry got that wrong. Write a class Car? So Car has a private int speed; The constructor is something like Car()
{ this.speed = 0;}
. Then the methods void Accelerate()
{ this.speed += 15;}
and 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:


public Car()
{
speed = 0;
}


He also needs an (why 2 in the OP?) accessor method to get the variable:


public int GetSpeed()
{
return speed;
}


If I'm being really pedantic I'd also use '_speed' to name the private field :smile: :smile:

Quick Reply

Latest

Trending

Trending