Advertisement
Denis_s

LR7

Jul 25th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.17 KB | None | 0 0
  1.  
  2. public class TestBikes {
  3.     public static void main(String[] args){
  4.         Bicycle bike01, bike02, bike03;
  5.         RocketBike bike04;
  6.  
  7.         bike01 = new Bicycle(20, 10, 1);
  8.         bike02 = new MountainBike(20, 10, 5, "Dual");
  9.         bike03 = new RoadBike(40, 20, 8, 23);
  10.         bike04 = new RocketBike(20, 10, 1, 1);
  11.  
  12.         bike01.printDescription();
  13.         bike02.printDescription();
  14.         bike03.printDescription();
  15.  
  16.         bike04.speedUp(5);
  17.         bike04.setCadence(15);
  18.         bike04.printDescription();
  19.  
  20.     }
  21. }
  22.  
  23.  
  24. public class Bicycle {
  25.     // the Bicycle class has three fields
  26.     public int cadence;
  27.     public int gear;
  28.     public int speed;
  29.  
  30.     // the Bicycle class has one constructor
  31.     public Bicycle(int startCadence, int startSpeed, int startGear) {
  32.         gear = startGear;
  33.         cadence = startCadence;
  34.         speed = startSpeed;
  35.     }
  36.  
  37.     // the Bicycle class has four methods
  38.  
  39.     public void setCadence(int newValue) {
  40.         cadence = newValue;
  41.     }
  42.  
  43.     public void setGear(int newValue) {
  44.         gear = newValue;
  45.     }
  46.  
  47.     public void applyBrake(int decrement) {
  48.         speed -= decrement;
  49.     }
  50.  
  51.     public void speedUp(int increment) {
  52.         speed += increment;
  53.     }
  54.  
  55.     public void printDescription() {
  56.         System.out.println("\nBike in gear " + this.gear
  57.                 + " with a cadence of " + this.cadence +
  58.                 " and travelling at a speed of " + this.speed + ". ");
  59.     }
  60. }
  61.  
  62.  
  63. public class MountainBike extends Bicycle {
  64.     private String suspension;
  65.     public MountainBike(
  66.             int startCadence,
  67.             int startSpeed,
  68.             int startGear,
  69.             String suspensionType){
  70.         super(  startCadence,
  71.                 startSpeed,
  72.                 startGear);
  73.         this.setSuspension(suspensionType);
  74.     }
  75.     public String getSuspension(){
  76.         return this.suspension;
  77.     }
  78.     public void setSuspension(String suspensionType) {
  79.         this.suspension = suspensionType;
  80.     }
  81.  
  82.     public void printDescription() {
  83.         super.printDescription();
  84.         System.out.println("The " + "MountainBike has a" +
  85.                 getSuspension() + " suspension.");
  86.     }
  87. }
  88.  
  89. public class RoadBike extends Bicycle{
  90.     // In millimeters (mm)
  91.     private int tireWidth;
  92.     public RoadBike(int startCadence,
  93.                     int startSpeed,
  94.                     int startGear,
  95.                     int newTireWidth){
  96.         super(  startCadence,
  97.                 startSpeed,
  98.                 startGear);
  99.         this.setTireWidth(newTireWidth);
  100.     }
  101.     public int getTireWidth(){
  102.         return this.tireWidth;
  103.     }
  104.     public void setTireWidth(int newTireWidth){
  105.         this.tireWidth = newTireWidth;
  106.     }
  107.     public void printDescription(){
  108.         super.printDescription();
  109.         System.out.println("The RoadBike" + " has " + getTireWidth() +
  110.                 " MM tires.");
  111.     }
  112. }
  113.  
  114. public class RocketBike extends Bicycle {
  115.  
  116.     private int fuel;
  117.     public String className = "RocketBike";
  118.  
  119.     public RocketBike(int startCadence,
  120.                       int startSpeed,
  121.                       int startGear,
  122.                       int newFuel) {
  123.         super(startCadence,
  124.                 startSpeed,
  125.                 startGear);
  126.         this.setFuel(newFuel);
  127.     }
  128.  
  129.  
  130.     public int getFuel() {
  131.         return this.fuel;
  132.     }
  133.  
  134.     public void setFuel(int newFuel) {
  135.         this.fuel = newFuel;
  136.     }
  137.  
  138.     public void setCadence(int newValue) {
  139.         System.out.print("Setting cadence inside " );
  140.         cadence = newValue;
  141.     }
  142.  
  143.     public void speedUp(int increment) {
  144.         System.out.println("\nThe " + className + " Speed " + super.speed + "kmh increment by " + increment + " kmh");
  145.         if ((super.speed + increment) > 60) {
  146.             super.speed = 60;
  147.             System.out.println("Reach the speed limit = 60 kmh");
  148.         } else {
  149.             super.speed += increment;
  150.         }
  151.     }
  152.  
  153.     public void printDescription() {
  154.         super.printDescription();
  155.         System.out.println("The " + className + " has a fuel " + getFuel() +
  156.                 " litres.");
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement