Advertisement
Guest User

Untitled

a guest
Jul 8th, 2020
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. package vehicle;
  2.  
  3. public class Vehicle {
  4. private final double DEFAULT_FUEL_CONSUMPTION = 1.25;
  5. private double fuelConsumption;
  6. private double fuel;
  7. private int horsePower;
  8.  
  9. public Vehicle(double fuel, int horsePower) {
  10. this.fuel = fuel;
  11. this.horsePower = horsePower;
  12. this.setFuelConsumption(DEFAULT_FUEL_CONSUMPTION);
  13. }
  14.  
  15. public double getFuelConsumption() {
  16. return this.fuelConsumption;
  17. }
  18.  
  19. public void setFuelConsumption(double fuelConsumption) {
  20. if (this.fuelConsumption == 0.0) {
  21. this.fuelConsumption = fuelConsumption;
  22. }
  23.  
  24. }
  25.  
  26. public double getFuel() {
  27. return this.fuel;
  28. }
  29.  
  30. public void setFuel(double fuel) {
  31. this.fuel = fuel;
  32. }
  33.  
  34. public int getHorsePower() {
  35. return this.horsePower;
  36. }
  37.  
  38. public void setHorsePower(int horsePower) {
  39. this.horsePower = horsePower;
  40. }
  41.  
  42. public void drive(double kilometers){
  43. double fuelNeeded = kilometers*fuelConsumption;
  44.  
  45. if (fuel >= fuelNeeded) {
  46. this.fuel-=fuelNeeded;
  47. }
  48. }
  49. }
  50. //===========================================================================================================================
  51. package vehicle;
  52.  
  53. public class Car extends Vehicle{
  54. private final double DEFAULT_FUEL_CONSUMPTION = 3;
  55. public Car(double fuel, int horsePower) {
  56. super(fuel, horsePower);
  57. super.setFuelConsumption(DEFAULT_FUEL_CONSUMPTION);
  58. }
  59.  
  60. }
  61. //===========================================================================================================================
  62. package vehicle;
  63.  
  64. public class Motorcycle extends Vehicle {
  65. public Motorcycle(double fuel, int horsePower) {
  66. super(fuel, horsePower);
  67. }
  68.  
  69. }
  70. //===========================================================================================================================
  71. package vehicle;
  72.  
  73. public class CrossMotorcycle extends Motorcycle{
  74. public CrossMotorcycle(double fuel, int horsePower) {
  75. super(fuel, horsePower);
  76. }
  77.  
  78. }
  79. //===========================================================================================================================
  80. package vehicle;
  81.  
  82. public class RaceMotorcycle extends Motorcycle{
  83. private final double DEFAULT_FUEL_CONSUMPTION =8;
  84. public RaceMotorcycle(double fuel, int horsePower) {
  85. super(fuel, horsePower);
  86. super.setFuelConsumption(DEFAULT_FUEL_CONSUMPTION);
  87. }
  88.  
  89. }
  90. //===========================================================================================================================
  91. package vehicle;
  92.  
  93. public class SportCar extends Car {
  94. private final double DEFAULT_FUEL_CONSUMPTION = 10;
  95. public SportCar(double fuel, int horsePower) {
  96. super(fuel, horsePower);
  97. super.setFuelConsumption(DEFAULT_FUEL_CONSUMPTION);
  98. }
  99.  
  100. }
  101. //===========================================================================================================================
  102. package vehicle;
  103.  
  104. public class FamilyCar extends Car{
  105. public FamilyCar(double fuel, int horsePower) {
  106. super(fuel, horsePower);
  107. }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement