64 642
Assignments Done
99,2%
Successfully Done
In September 2018

Answer to Question #4079 in Java | JSP | JSF for ellen

Question #4079
How to write/start a class with 2 objects?
Expert's answer
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();
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions