Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.85 KB | None | 0 0
  1. public class Bicycle {
  2.     // the Bicycle class has three fields
  3.  
  4.     public int cadence;
  5.  
  6.     public int gear;
  7.  
  8.     public int speed;
  9.  
  10.     // the Bicycle class has one constructor
  11.  
  12.     public Bicycle(int startCadence, int startSpeed, int startGear) {
  13.  
  14.         gear = startGear;
  15.  
  16.         cadence = startCadence;
  17.  
  18.         speed = startSpeed;
  19.  
  20.     }
  21.  
  22.  
  23.  
  24.     // the Bicycle class has four methods
  25.  
  26.     public void setCadence(int newValue) {
  27.  
  28.         cadence = newValue;
  29.  
  30.     }
  31.  
  32.     public void setGear(int newValue) {
  33.  
  34.         gear = newValue;
  35.  
  36.     }
  37.  
  38.     public void applyBrake(int decrement) {
  39.  
  40.         speed -= decrement;
  41.  
  42.     }
  43.  
  44.     public void speedUp(int increment) {
  45.  
  46.         speed += increment;
  47.  
  48.     }
  49.  
  50.  
  51.  
  52.     public void printDescription(){
  53.  
  54.         System.out.println("\nBike is " + "in gear " + this.gear
  55.  
  56.                 + " with a cadence of " + this.cadence +
  57.  
  58.                 " and travelling at a speed of " + this.speed + ". ");
  59.  
  60.     }
  61.  
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. public class MountainBike extends Bicycle {
  72.  
  73.     // the MountainBike subclass adds one field
  74.  
  75.  
  76.     private String suspension;
  77.  
  78.     // the MountainBike subclass has one constructor
  79.  
  80.     public MountainBike(int startCadence,
  81.  
  82.                         int startSpeed,
  83.  
  84.                         int startGear,
  85.  
  86.                         String suspensionType) {
  87.  
  88.         super(startCadence, startSpeed, startGear);
  89.         this.setSuspension(suspensionType);
  90.  
  91.  
  92.  
  93.     }
  94.     public String getSuspension(){
  95.  
  96.         return this.suspension;
  97.  
  98.     }
  99.     public void setSuspension(String suspensionType) {
  100.  
  101.         this.suspension = suspensionType;
  102.  
  103.     }
  104.  
  105.  
  106.     public void printDescription() {
  107.  
  108.         super.printDescription();
  109.  
  110.         System.out.println("The " + "MountainBike has a " +
  111.  
  112.                 getSuspension() + " suspension.");
  113.  
  114.     }
  115.  
  116.  
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127. public class RocketBike extends Bicycle {
  128.     public int getFuel;
  129.     public int restOfFuel;
  130.     public int Speed;
  131.  
  132.     public RocketBike(int newrestOfFuel, int newgetFuel, int newSpeed, int startCadence, int startSpeed, int startGear) {
  133.         super(startCadence, startSpeed, startGear);
  134.         this.setrestOfFuel(newrestOfFuel);
  135.  
  136.         this.setgetFuel(newgetFuel);
  137.  
  138.         this.setSpeed(newSpeed);
  139.     }
  140.  
  141.     public void setgetFuel (int newValue) {
  142.  
  143.         getFuel = newValue;
  144.     }
  145.     public void setrestOfFuel (int newValue) {
  146.         restOfFuel = newValue;
  147.  
  148.     }
  149.     private void setSpeed(int newSpeed) {
  150.         this.speed = newSpeed;
  151.     }
  152.  
  153.     private int getSpeed(){
  154.  
  155.         while (speed < 60);
  156.  
  157.         return this.speed;
  158.  
  159.     }
  160.     public int getFuelQuantity(){
  161.         return this.restOfFuel;
  162.     }
  163.  
  164.  
  165.     public void setFuelQuantity(int newFuelQuantity) {
  166.         this.restOfFuel = newFuelQuantity;
  167.     }
  168.  
  169.  
  170.  
  171.     public int getRefuel(){
  172.         return this.getFuel;
  173.     }
  174.  
  175.     public void setRefuel(int newRefuel) {
  176.         this.getFuel = newRefuel;
  177.     }
  178.  
  179.  
  180.     public String setCadence() {
  181.         return "Setting cadence inside RocketBike";
  182.  
  183.     }
  184.  
  185.  
  186.  
  187.     public void printDescription(){
  188.  
  189.         super.printDescription();
  190.  
  191.         System.out.println("The RocketBike " + setCadence() );
  192.  
  193.         System.out.println("The RocketBike " + "has " + getFuelQuantity() + " litres of fuel.");
  194.  
  195.         System.out.println("The RocketBike " + "fill up " + getRefuel() + " litres of fuel");
  196.  
  197.         System.out.println("The RocketBike " + "speed is " + getSpeed() + " km/h. You exceeded the speed!");
  198.  
  199.     }
  200. }
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215. public class RoadBike extends Bicycle{
  216.  
  217.     // In millimeters (mm)
  218.  
  219.     private int tireWidth;
  220.  
  221.     public RoadBike(int startCadence,
  222.  
  223.                     int startSpeed,
  224.  
  225.                     int startGear,
  226.  
  227.                     int newTireWidth){
  228.  
  229.         super(startCadence,
  230.  
  231.                 startSpeed,
  232.  
  233.                 startGear);
  234.  
  235.         this.setTireWidth(newTireWidth);
  236.  
  237.     }
  238.  
  239.     public int getTireWidth(){
  240.  
  241.         return this.tireWidth;
  242.  
  243.     }
  244.  
  245.     public void setTireWidth(int newTireWidth){
  246.  
  247.         this.tireWidth = newTireWidth;
  248.  
  249.     }
  250.  
  251.     public void printDescription(){
  252.  
  253.         super.printDescription();
  254.  
  255.         System.out.println("The RoadBike" + " has " + getTireWidth() +
  256.  
  257.                 " MM tires.");
  258.  
  259.     }
  260.  
  261. }
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274. public class TestBikes {
  275.  
  276.     public static void main(String[] args){
  277.  
  278.         Bicycle bike01, bike02, bike03, bike04;
  279.  
  280.  
  281.  
  282.         bike01 = new Bicycle(15, 50, 1);
  283.  
  284.         bike02 = new MountainBike(20, 45, 2, "One");
  285.  
  286.         bike03 = new RoadBike(13, 90, 17, 14);
  287.  
  288.         bike04 = new RocketBike(30, 20, 90, 30, 10, 10);
  289.  
  290.  
  291.  
  292.         bike01.printDescription();
  293.  
  294.         bike02.printDescription();
  295.  
  296.         bike03.printDescription();
  297.  
  298.         bike04.printDescription();
  299.  
  300.  
  301.  
  302.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement