In: Computer Science
In Java, please write a tester code.
Here's my code:
public class Bicycle {
public int cadence;
public int gear;
public int speed;
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear =
startGear;
cadence = startCadence;
speed = startSpeed;
}
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;
}
}
public class MountainBike extends Bicycle {
public int seatHeight;
public MountainBike(int startGear, int startCadence, int startSpeed, int startHeight) {
super(startGear, startCadence, startSpeed);
seatHeight = startHeight;
}
public void setHeight(int newValue) {
seatHeight = newValue;
}
}
1. Bicycle constructor parameter order should be correct
super(startGear, startCadence, startSpeed); // wrong
parameters order
super(startCadence, startSpeed, startGear); // correct
order
2. To retrieve the details of the bike, ideally we should
define
separate method in MountainBike class (I've added
it)
public String getInfo() {
String info = "startGear= " +
gear + "\n";
info += "startCadence= " +
cadence + "\n";
info += "startSpeed= " +
speed + "\n";
info += "startHeight= " +
seatHeight + "\n";
return info;
}
//-------- Tester class BicycleTester.java
public class BicycleTester {
public static void main (String args[]) {
//create object of sub/child class
MountainBike mb = new MountainBike(1,2,3,4);
System.out.println(mb.getInfo()); //call method of mb
// if we dont want to use getInfo() method then we have to
//get info using MountainBike object mb, as follows
/*
String info = "MountainBike info: \n";
info += "startGear= " + mb.gear + "\n";
info += "startCadence= " + mb.cadence + "\n";
info += "startSpeed= " + mb.speed + "\n";
info += "startHeight= " + mb.seatHeight + "\n";
System.out.println(info);
*/
}
}
//-------- end of BicycleTester.java
//-------- Bicycle.java
public class Bicycle {
public int cadence;
public int gear;
public int speed;
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
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;
}
}
//-------- end of Bicycle.java
//-------- MountainBike.java
public class MountainBike extends Bicycle {
public int seatHeight;
public MountainBike(int startGear, int startCadence, int startSpeed, int startHeight) {
//super(startGear, startCadence, startSpeed); wrong parameters order
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}
public void setHeight(int newValue) {
seatHeight = newValue;
}
//added this method to get bike details
public String getInfo() {
String info = "MountainBike info: \n";
info += "startGear= " + gear + "\n";
info += "startCadence= " + cadence + "\n";
info += "startSpeed= " + speed + "\n";
info += "startHeight= " + seatHeight + "\n";
return info;
}
}
//-------- end of MountainBike.java
Output
Thanks...