how/where to add new method and attributes
hi
new-noob here looking for some1 to explain these to me:
having code (found in one tutorials) and it works all-right want to add some new methods and attributes but need you to point me into right direction, please.
Code :
public class Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
int sits = 0;
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 sitsNumber(int newValue){
sits = newValue;
}
void printStates() {
System.out.println("cadence:" +
cadence + " speed:" +
speed + " gear:" + gear + " sits: " + sits );
}
}
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();
bike1.sitsNumber(1);
bike2.changeCadence(50);
bike2.speedUp(10);
bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.printStates();
bike2.sitsNumber(2);
}
}
Re: how/where to add new method and attributes
Not really sure what you're asking. What have you tried? What happened when you tried it?
Re: how/where to add new method and attributes
hi Kevin,
I am trying to add an bicycle carrier option and wonder how to have it displayed that bicke1 has carrier and bicke2 doesn't? it will be string not integer
Quote:
public class Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
int sits = 0;
String carrier = "";
Quote:
bike1.changeCadence(50);
bike1.speedUp(10);
bike1.changeGear(2);
bike1.printStates();
bike1.sitsNumber(1);
bike1.carrier ("yes");
Re: how/where to add new method and attributes
And what happened when you tried that? Are you also adding a method called carrier() that takes a String argument? That seems strange to me- take a look at how the other methods set variable values.
Also, if your variable should hold yes/no values, you should use a boolean instead of a String.