Basic Question
I am new to java and I have a basic question. I will post the code I am having problems with and ask the questions at the end.
public class Bicycle {
// the Bicycle class has three fields
public int cadence;
public int gear;
public int speed;
// the Bicycle class has one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
// the Bicycle class has four methods
public void setCadence(int newValue) {
cadence = newValue;
}
public void setGear(int newValue) {
gear = newValue;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}
Ok here is my problem. When you call the constructor you create an object of type Bicycle. Say the 3 number i enter into the constructor are 1,2,3. So in the constructor method it has the code
gear = startGear;
cadence = startCadence;
speed = startSpeed;
this means gear =1, cadence=2 and startgear=3
My problem is I am unsure if the gear, cadence and startgear at the start of the code is now 1,2,3 or is it only in the constructor block?
public int cadence; //does this now equal 1?
public int gear; //does this equal 2?
public int speed; //does this equal 3?
My second problem is with the 4 methods at the end of the code. I believe these are called mutator methods. Do these methods need to be called in order to run their code or do they run automatically when the constructor is called?
I hope I was clear and thanks for reading my questions.
Message was edited by:
Josiah

