Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class Vehicle {
- protected int speed;
- protected int fuel;
- public Vehicle(int speed, int fuel) {
- this.speed = speed;
- this.fuel = fuel;
- }
- public abstract void accelerate();
- public abstract void brake();
- }
- class SimpleCar extends Vehicle implements Refuable{
- public SimpleCar(int speed, int fuel) {
- super(speed, fuel);
- }
- @Override
- public void accelerate() {
- if (fuel > 0) {
- speed += 10;
- fuel--;
- System.out.println("Speed after accelerate: " + speed + ", Fuel: " + fuel);
- if (fuel == 0) {
- System.out.println("Out of fuel!");
- }
- } else {
- System.out.println("Out of fuel!");
- }
- }
- @Override
- public void brake() {
- if (speed >= 10) {
- speed -= 10;
- System.out.println("Speed after brake: " + speed);
- } else {
- System.out.println("Car is already at minimum speed.");
- }
- }
- @Override
- public void refuel() {
- fuel += 10;
- System.out.println("Car refueled, Fuel left: " + fuel);
- }
- }
- class ElectricCar extends Vehicle implements Refuable{
- public ElectricCar(int speed, int battery) {
- super(speed, battery);
- }
- @Override
- public void accelerate() {
- if (fuel > 0) {
- speed += 15;
- fuel--;
- System.out.println("Speed after accelerate: " + speed + ", Battery: " + fuel);
- if (fuel == 0) {
- System.out.println("Battery empty!");
- }
- } else {
- System.out.println("Battery empty!");
- }
- }
- @Override
- public void brake() {
- if (speed >= 10) {
- speed -= 10;
- System.out.println("Speed after brake: " + speed);
- } else {
- System.out.println("Car is already at minimum speed.");
- }
- }
- @Override
- public void refuel() {
- fuel += 20;
- System.out.println("Electric car recharged, Battery left: " + fuel);
- }
- }
- public interface Refuable {
- void refuel();
- }
- public class Main {
- public static void main(String[] args) {
- int initialSpeedCar = 20;
- int initialFuelCar = 2;
- int initialSpeedElectricCar = 30;
- int initialBatteryElectricCar = 2;
- if (args.length >= 4) {
- initialSpeedCar = Integer.parseInt(args[0]);
- initialFuelCar = Integer.parseInt(args[1]);
- initialSpeedElectricCar = Integer.parseInt(args[2]);
- initialBatteryElectricCar = Integer.parseInt(args[3]);
- } else {
- System.out.println("Default values will be used for vehicles.");
- }
- SimpleCar simpleCar = new SimpleCar(initialSpeedCar, initialFuelCar);
- ElectricCar electricCar = new ElectricCar(initialSpeedElectricCar, initialBatteryElectricCar);
- System.out.println("SimpleCar:");
- simpleCar.accelerate();
- simpleCar.accelerate();
- simpleCar.refuel();
- System.out.println("\nElectricCar:");
- electricCar.accelerate();
- electricCar.accelerate();
- electricCar.refuel();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement