Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. public class Bicycle {
  2.  
  3. // the Bicycle class has three fields
  4. public int cadence;
  5. public int gear;
  6. public int speed;
  7.  
  8. // the Bicycle class has one constructor
  9. public Bicycle(int startCadence, int startSpeed, int startGear) {
  10. gear = startGear;
  11. cadence = startCadence;
  12. speed = startSpeed;
  13. }
  14.  
  15. // the Bicycle class has four methods
  16. public void setCadence(int newValue) {
  17. cadence = newValue;
  18. }
  19.  
  20. public void setGear(int newValue) {
  21. gear = newValue;
  22. }
  23.  
  24. public void applyBrake(int decrement) {
  25. speed -= decrement;
  26. }
  27.  
  28. public void speedUp(int increment) {
  29. speed += increment;
  30. }
  31.  
  32. }
  33. A class declaration for a MountainBike class that is a subclass of Bicycle might look like this:
  34.  
  35. public class MountainBike extends Bicycle {
  36.  
  37. // the MountainBike subclass adds one field
  38. public int seatHeight;
  39.  
  40. // the MountainBike subclass has one constructor
  41. public MountainBike(int startHeight,
  42. int startCadence,
  43. int startSpeed,
  44. int startGear) {
  45. super(startCadence, startSpeed, startGear);
  46. seatHeight = startHeight;
  47. }
  48.  
  49. // the MountainBike subclass adds one method
  50. public void setHeight(int newValue) {
  51. seatHeight = newValue;
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement