Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Car {
- private String brand;
- private int speed;
- private double fuel;
- private double mileage;
- public Car(String brand, int speed, double fuel) {
- this.brand = brand;
- this.speed = speed;
- this.fuel = fuel;
- this.mileage = 0.0;
- }
- public void accelerate() {
- speed += 10;
- }
- public void refuel(double amount) {
- fuel += amount;
- }
- public void drive(int distance) {
- if (fuel > 0) {
- double fuelConsumed = (double) distance / speed;
- fuel -= fuelConsumed;
- mileage += distance;
- System.out.println("Distance: " + distance + " km.");
- System.out.println("Mileage: " + mileage + " km.");
- System.out.println("Left fuel: " + fuel + " l.");
- } else {
- System.out.println("Not enough fuel.");
- }
- }
- public static void main(String[] args) {
- Car myCar = new Car("Audi", 60, 30.0);
- myCar.drive(200);
- myCar.accelerate();
- myCar.drive(150);
- myCar.refuel(20.0);
- myCar.drive(100);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement