Back to the 2019 paper

Module III: Basics of Web Programming

201914m

Define a class called Fuel_Monitor that will be used to check the amount of fuel that is left over in a vehicle after travelling a certain distance. The class should have instance variables tank capacity to store initial size of the tank and efficiency to store initial efficiency of the vehicle. Also, set the variable fuel_in_tank to zero that is used to store initial fuel in tank. Include a method that returns ini_tank_size, ini_effi and fuel_in_tank. Include a method add_fuel that calculates how much fuel can be filled depending upon the fuel already in the tank and the capacity of the tank. Also, include a method drive_distance that returns how much distance can be travelled with the fuel available in the tank with the efficiency provided. Embed your class in a test program. You should decide which variables should be public, if any.

Worked SolutionAI Assisted

Answer: Java Implementation — Fuel_Monitor Class and Test Program

1. Class Design & Requirements Breakdown

Encapsulation Strategy:

  • Private Instance Variables: All state variables are declared private to enforce encapsulation and prevent direct unauthorized alteration.
    • tank_capacity: Total capacity of the fuel tank (in Litres).
    • efficiency: Fuel efficiency / mileage (in Kilometers per Litre).
    • fuel_in_tank: Current amount of fuel available in the tank (initialized to 0.0).

Public Methods:

  1. Fuel_Monitor(double capacity, double efficiency): Parameterized constructor to initialize the vehicle specifications.
  2. getStatus(): Returns a formatted summary of initial tank size, efficiency, and current fuel.
  3. add_fuel(double amount): Safely adds fuel up to maximum capacity and calculates how much fuel was actually filled.
  4. drive_distance(double distance): Simulates driving a specified distance, calculates fuel consumed (fuel_used=distanceefficiency\text{fuel\_used} = \frac{\text{distance}}{\text{efficiency}}), updates the remaining fuel in tank, and returns the actual distance travelled.
  5. max_drive_distance(): Returns the total distance the vehicle can travel with the current remaining fuel in tank (fuel_in_tank×efficiency\text{fuel\_in\_tank} \times \text{efficiency}).

2. Complete Java Code

import java.util.Scanner;

class Fuel_Monitor {
    // Private instance variables for encapsulation
    private double tank_capacity;  // Maximum fuel tank size (Litres)
    private double efficiency;     // Mileage (km per Litre)
    private double fuel_in_tank;   // Current fuel in tank (Litres)

    // Constructor to initialize tank capacity and vehicle efficiency
    public Fuel_Monitor(double tank_capacity, double efficiency) {
        this.tank_capacity = tank_capacity;
        this.efficiency = efficiency;
        this.fuel_in_tank = 0.0; // Initialized to zero as specified
    }

    // Method to return initial specifications and current fuel status
    public String getStatus() {
        return "Initial Tank Size: " + tank_capacity + " L | " +
               "Initial Efficiency: " + efficiency + " km/L | " +
               "Current Fuel: " + fuel_in_tank + " L";
    }

    // Accessor methods
    public double get_ini_tank_size() { return tank_capacity; }
    public double get_ini_effi() { return efficiency; }
    public double get_fuel_in_tank() { return fuel_in_tank; }

    // Method to add fuel to the tank
    public double add_fuel(double amount) {
        if (amount <= 0) {
            System.out.println("Invalid fuel amount. Please enter a positive value.");
            return 0.0;
        }

        double available_space = tank_capacity - fuel_in_tank;

        if (amount <= available_space) {
            fuel_in_tank += amount;
            System.out.println("Added " + amount + " L. Current fuel in tank: " + fuel_in_tank + " L");
            return amount;
        } else {
            // Fill tank to maximum capacity
            fuel_in_tank = tank_capacity;
            System.out.println("Tank Full! Only " + available_space + " L could be filled. (Overflow: " + (amount - available_space) + " L discarded)");
            return available_space;
        }
    }

    // Method to drive a given distance and compute leftover fuel
    public double drive_distance(double distance) {
        if (distance <= 0) {
            System.out.println("Distance must be greater than zero.");
            return 0.0;
        }

        // Maximum distance possible with current fuel
        double max_possible_distance = fuel_in_tank * efficiency;

        if (distance <= max_possible_distance) {
            double fuel_consumed = distance / efficiency;
            fuel_in_tank -= fuel_consumed;
            System.out.println("Travelled " + distance + " km. Fuel consumed: " + String.format("%.2f", fuel_consumed) + " L.");
            System.out.println("Fuel left in tank: " + String.format("%.2f", fuel_in_tank) + " L.");
            return distance;
        } else {
            System.out.println("Not enough fuel to travel " + distance + " km!");
            System.out.println("Vehicle travelled maximum possible: " + String.format("%.2f", max_possible_distance) + " km before running out of fuel.");
            fuel_in_tank = 0.0;
            return max_possible_distance;
        }
    }

    // Method returning max distance possible with current fuel
    public double get_max_possible_distance() {
        return fuel_in_tank * efficiency;
    }
}

// Test Program
public class TestFuelMonitor {
    public static void main(String[] args) {
        System.out.println("=== VEHICLE FUEL MONITOR SIMULATION ===
");

        // Create a vehicle with 50 Litre tank capacity and 15 km/L efficiency
        Fuel_Monitor car = new Fuel_Monitor(50.0, 15.0);

        // 1. Check initial status
        System.out.println("1. Initial Status:");
        System.out.println(car.getStatus());
        System.out.println();

        // 2. Add Fuel
        System.out.println("2. Adding 30 Litres of Fuel:");
        car.add_fuel(30.0);
        System.out.println("Max drivable range now: " + car.get_max_possible_distance() + " km
");

        // 3. Drive 150 km
        System.out.println("3. Driving 150 km:");
        car.drive_distance(150.0);
        System.out.println();

        // 4. Drive another 200 km
        System.out.println("4. Driving 200 km:");
        car.drive_distance(200.0);
        System.out.println();

        // 5. Try to drive more distance than fuel permits
        System.out.println("5. Attempting to drive 200 km with remaining fuel:");
        car.drive_distance(200.0);
        System.out.println();

        // 6. Overfill test
        System.out.println("6. Refueling with 60 Litres (Exceeds capacity):");
        car.add_fuel(60.0);
        System.out.println(car.getStatus());
    }
}

3. Sample Program Output

=== VEHICLE FUEL MONITOR SIMULATION ===

1. Initial Status:
Initial Tank Size: 50.0 L | Initial Efficiency: 15.0 km/L | Current Fuel: 0.0 L

2. Adding 30 Litres of Fuel:
Added 30.0 L. Current fuel in tank: 30.0 L
Max drivable range now: 450.0 km

3. Driving 150 km:
Travelled 150.0 km. Fuel consumed: 10.00 L.
Fuel left in tank: 20.00 L.

4. Driving 200 km:
Travelled 200.0 km. Fuel consumed: 13.33 L.
Fuel left in tank: 6.67 L.

5. Attempting to drive 200 km with remaining fuel:
Not enough fuel to travel 200.0 km!
Vehicle travelled maximum possible: 100.00 km before running out of fuel.

6. Refueling with 60 Litres (Exceeds capacity):
Tank Full! Only 50.0 L could be filled. (Overflow: 10.0 L discarded)
Initial Tank Size: 50.0 L | Initial Efficiency: 15.0 km/L | Current Fuel: 50.0 L

Similar questions