Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package vehicle;
- public class Vehicle {
- private final double DEFAULT_FUEL_CONSUMPTION = 1.25;
- private double fuelConsumption;
- private double fuel;
- private int horsePower;
- public Vehicle(double fuel, int horsePower) {
- this.fuel = fuel;
- this.horsePower = horsePower;
- this.setFuelConsumption(DEFAULT_FUEL_CONSUMPTION);
- }
- public double getFuelConsumption() {
- return this.fuelConsumption;
- }
- public void setFuelConsumption(double fuelConsumption) {
- if (this.fuelConsumption == 0.0) {
- this.fuelConsumption = fuelConsumption;
- }
- }
- public double getFuel() {
- return this.fuel;
- }
- public void setFuel(double fuel) {
- this.fuel = fuel;
- }
- public int getHorsePower() {
- return this.horsePower;
- }
- public void setHorsePower(int horsePower) {
- this.horsePower = horsePower;
- }
- public void drive(double kilometers){
- double fuelNeeded = kilometers*fuelConsumption;
- if (fuel >= fuelNeeded) {
- this.fuel-=fuelNeeded;
- }
- }
- }
- //===========================================================================================================================
- package vehicle;
- public class Car extends Vehicle{
- private final double DEFAULT_FUEL_CONSUMPTION = 3;
- public Car(double fuel, int horsePower) {
- super(fuel, horsePower);
- super.setFuelConsumption(DEFAULT_FUEL_CONSUMPTION);
- }
- }
- //===========================================================================================================================
- package vehicle;
- public class Motorcycle extends Vehicle {
- public Motorcycle(double fuel, int horsePower) {
- super(fuel, horsePower);
- }
- }
- //===========================================================================================================================
- package vehicle;
- public class CrossMotorcycle extends Motorcycle{
- public CrossMotorcycle(double fuel, int horsePower) {
- super(fuel, horsePower);
- }
- }
- //===========================================================================================================================
- package vehicle;
- public class RaceMotorcycle extends Motorcycle{
- private final double DEFAULT_FUEL_CONSUMPTION =8;
- public RaceMotorcycle(double fuel, int horsePower) {
- super(fuel, horsePower);
- super.setFuelConsumption(DEFAULT_FUEL_CONSUMPTION);
- }
- }
- //===========================================================================================================================
- package vehicle;
- public class SportCar extends Car {
- private final double DEFAULT_FUEL_CONSUMPTION = 10;
- public SportCar(double fuel, int horsePower) {
- super(fuel, horsePower);
- super.setFuelConsumption(DEFAULT_FUEL_CONSUMPTION);
- }
- }
- //===========================================================================================================================
- package vehicle;
- public class FamilyCar extends Car{
- public FamilyCar(double fuel, int horsePower) {
- super(fuel, horsePower);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement