Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 is " + "in gear " + this.gear
- + " with a cadence of " + this.cadence +
- " and travelling at a speed of " + this.speed + ". ");
- }
- }
- public class MountainBike extends Bicycle {
- // the MountainBike subclass adds one field
- private String suspension;
- // the MountainBike subclass has one constructor
- 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 RocketBike extends Bicycle {
- public int getFuel;
- public int restOfFuel;
- public int Speed;
- public RocketBike(int newrestOfFuel, int newgetFuel, int newSpeed, int startCadence, int startSpeed, int startGear) {
- super(startCadence, startSpeed, startGear);
- this.setrestOfFuel(newrestOfFuel);
- this.setgetFuel(newgetFuel);
- this.setSpeed(newSpeed);
- }
- public void setgetFuel (int newValue) {
- getFuel = newValue;
- }
- public void setrestOfFuel (int newValue) {
- restOfFuel = newValue;
- }
- private void setSpeed(int newSpeed) {
- this.speed = newSpeed;
- }
- private int getSpeed(){
- while (speed < 60);
- return this.speed;
- }
- public int getFuelQuantity(){
- return this.restOfFuel;
- }
- public void setFuelQuantity(int newFuelQuantity) {
- this.restOfFuel = newFuelQuantity;
- }
- public int getRefuel(){
- return this.getFuel;
- }
- public void setRefuel(int newRefuel) {
- this.getFuel = newRefuel;
- }
- public String setCadence() {
- return "Setting cadence inside RocketBike";
- }
- public void printDescription(){
- super.printDescription();
- System.out.println("The RocketBike " + setCadence() );
- System.out.println("The RocketBike " + "has " + getFuelQuantity() + " litres of fuel.");
- System.out.println("The RocketBike " + "fill up " + getRefuel() + " litres of fuel");
- System.out.println("The RocketBike " + "speed is " + getSpeed() + " km/h. You exceeded the speed!");
- }
- }
- 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 TestBikes {
- public static void main(String[] args){
- Bicycle bike01, bike02, bike03, bike04;
- bike01 = new Bicycle(15, 50, 1);
- bike02 = new MountainBike(20, 45, 2, "One");
- bike03 = new RoadBike(13, 90, 17, 14);
- bike04 = new RocketBike(30, 20, 90, 30, 10, 10);
- bike01.printDescription();
- bike02.printDescription();
- bike03.printDescription();
- bike04.printDescription();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement