Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TestBikes {
- public static void main(String[] args){
- Bicycle bike01, bike02, bike03;
- RocketBike bike04;
- bike01 = new Bicycle(20, 10, 1);
- bike02 = new MountainBike(20, 10, 5, "Dual");
- bike03 = new RoadBike(40, 20, 8, 23);
- bike04 = new RocketBike(20, 10, 1, 1);
- bike01.printDescription();
- bike02.printDescription();
- bike03.printDescription();
- bike04.speedUp(5);
- bike04.setCadence(15);
- bike04.printDescription();
- }
- }
- 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;
- }
- public void printDescription() {
- System.out.println("\nBike in gear " + this.gear
- + " with a cadence of " + this.cadence +
- " and travelling at a speed of " + this.speed + ". ");
- }
- }
- public class MountainBike extends Bicycle {
- private String suspension;
- public MountainBike(
- int startCadence,
- int startSpeed,
- int startGear,
- String suspensionType){
- super( startCadence,
- startSpeed,
- startGear);
- this.setSuspension(suspensionType);
- }
- public String getSuspension(){
- return this.suspension;
- }
- public void setSuspension(String suspensionType) {
- this.suspension = suspensionType;
- }
- public void printDescription() {
- super.printDescription();
- System.out.println("The " + "MountainBike has a" +
- getSuspension() + " suspension.");
- }
- }
- public class RoadBike extends Bicycle{
- // In millimeters (mm)
- private int tireWidth;
- public RoadBike(int startCadence,
- int startSpeed,
- int startGear,
- int newTireWidth){
- super( startCadence,
- startSpeed,
- startGear);
- this.setTireWidth(newTireWidth);
- }
- public int getTireWidth(){
- return this.tireWidth;
- }
- public void setTireWidth(int newTireWidth){
- this.tireWidth = newTireWidth;
- }
- public void printDescription(){
- super.printDescription();
- System.out.println("The RoadBike" + " has " + getTireWidth() +
- " MM tires.");
- }
- }
- public class RocketBike extends Bicycle {
- private int fuel;
- public String className = "RocketBike";
- public RocketBike(int startCadence,
- int startSpeed,
- int startGear,
- int newFuel) {
- super(startCadence,
- startSpeed,
- startGear);
- this.setFuel(newFuel);
- }
- public int getFuel() {
- return this.fuel;
- }
- public void setFuel(int newFuel) {
- this.fuel = newFuel;
- }
- public void setCadence(int newValue) {
- System.out.print("Setting cadence inside " );
- cadence = newValue;
- }
- public void speedUp(int increment) {
- System.out.println("\nThe " + className + " Speed " + super.speed + "kmh increment by " + increment + " kmh");
- if ((super.speed + increment) > 60) {
- super.speed = 60;
- System.out.println("Reach the speed limit = 60 kmh");
- } else {
- super.speed += increment;
- }
- }
- public void printDescription() {
- super.printDescription();
- System.out.println("The " + className + " has a fuel " + getFuel() +
- " litres.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement