Define a class called Vehicle that will be used to check the amount of fuel left in a vehicle after traveling a certain distance. 6 min read

Define a class called Vehicle that will be used to check the amount of fuel left in a vehicle after traveling a certain distance.

Homework Help: Questions and Answers: Define a class called Vehicle that will be used to check the amount of fuel left in a vehicle after traveling a certain distance. The class should have the instance variable tankSize to store the initial size of the tank and efficiency to store initial efficiency of the vehicle. Set to zero the variable fuelInTank that is used to store the initial amount of fuel in a tank. Include a mutator method that returns iniTankSize, initEfficiency and fuelInTank. Include an accessor method addPetrol that calculates how much fuel can be filled depending on the fuel existing in the tank and the tank’s capacity. Also, include a method driveTo that returns what distance can be traveled with the available fuel and provided efficiency. Use your class with a test program. You should decide which variables should be public, if any. Also, define if any other method(s) are needed.

Define a class called Vehicle that will be used to check the amount of fuel left in a vehicle after traveling a certain distance.

Answer

To solve this question step-by-step, first we'll define a class Vehicle in Python. This class will include the required instance variables, mutator methods, accessor methods, and additional methods needed to achieve the described functionality.

Step 1: Define the Class and Initialize Instance Variables

First, we'll define the class Vehicle and initialize the instance variables tankSize, efficiency, and fuelInTank.

 class Vehicle:
    def __init__(self, tank_size, efficiency):
        self.tankSize = tank_size          # Capacity of the fuel tank
        self.efficiency = efficiency       # Fuel efficiency (distance per unit of fuel)
        self.fuelInTank = 0                # Current amount of fuel in the tank, initially zero

Step 2: Define the Mutator Method

We'll create a method called get_initial_values() to return the initial values of tankSize, efficiency, and fuelInTank.

def get_initial_values(self):
        return self.tankSize, self.efficiency, self.fuelInTank

Step 3: Define the Accessor Method

We'll define a method add_petrol(amount) that adds a specified amount of fuel to the tank, ensuring it does not exceed the tank's capacity.

def add_petrol(self, amount):
        if self.fuelInTank + amount > self.tankSize:
            print("Cannot add that much fuel. The tank will overflow!")
        else:
            self.fuelInTank += amount
            print(f"Added {amount} units of fuel. Current fuel: {self.fuelInTank} units.")

Step 4: Define the Method to Calculate Distance

We'll define a method drive_to(distance) that calculates whether the vehicle can travel a specified distance with the available fuel, and if possible, reduces the fuel accordingly.

def drive_to(self, distance):
        required_fuel = distance / self.efficiency
        
        if required_fuel > self.fuelInTank:
            print("Not enough fuel to drive that distance.")
            max_distance = self.fuelInTank * self.efficiency
            print(f"With current fuel, you can only drive up to {max_distance} units.")
        else:
            self.fuelInTank -= required_fuel
            print(f"Traveled {distance} units. Remaining fuel: {self.fuelInTank} units.")

Step 5: Testing the Class

Finally, let's create an instance of Vehicle and test its functionality.

# Create a vehicle with a tank size of 50 units and efficiency of 10 units per fuel
my_vehicle = Vehicle(50, 10)

# Get initial values
print("Initial Values:", my_vehicle.get_initial_values())

# Add 30 units of fuel
my_vehicle.add_petrol(30)

# Try to drive 200 units of distance
my_vehicle.drive_to(200)

# Check remaining fuel and attempt another drive
my_vehicle.drive_to(50)

Full Code

Here is the complete code, combining all steps:

class Vehicle:
    def __init__(self, tank_size, efficiency):
        self.tankSize = tank_size          # Capacity of the fuel tank
        self.efficiency = efficiency       # Fuel efficiency (distance per unit of fuel)
        self.fuelInTank = 0                # Current amount of fuel in the tank, initially zero
    
    def get_initial_values(self):
        return self.tankSize, self.efficiency, self.fuelInTank
    
    def add_petrol(self, amount):
        if self.fuelInTank + amount > self.tankSize:
            print("Cannot add that much fuel. The tank will overflow!")
        else:
            self.fuelInTank += amount
            print(f"Added {amount} units of fuel. Current fuel: {self.fuelInTank} units.")
    
    def drive_to(self, distance):
        required_fuel = distance / self.efficiency
        
        if required_fuel > self.fuelInTank:
            print("Not enough fuel to drive that distance.")
            max_distance = self.fuelInTank * self.efficiency
            print(f"With current fuel, you can only drive up to {max_distance} units.")
        else:
            self.fuelInTank -= required_fuel
            print(f"Traveled {distance} units. Remaining fuel: {self.fuelInTank} units.")

# Test the Vehicle class
my_vehicle = Vehicle(50, 10)

# Get initial values
print("Initial Values:", my_vehicle.get_initial_values())

# Add 30 units of fuel
my_vehicle.add_petrol(30)

# Try to drive 200 units of distance
my_vehicle.drive_to(200)

# Check remaining fuel and attempt another drive
my_vehicle.drive_to(50)

Explanation

  • Initialization: The Vehicle class is initialized with a specific tankSize and efficiency.
  • Mutator Method: get_initial_values() returns the initial tank size, efficiency, and current fuel (which is initially zero).
  • Accessor Method: add_petrol() adds fuel to the tank, ensuring the tank doesn’t overflow.
  • Driving Method: drive_to() calculates whether a specific distance can be traveled with the current fuel and adjusts the fuel level accordingly.

By running the above program, you can observe how the class behaves and how the fuel level affects the ability to travel certain distances.

Learn More: Homework Help

Q. Walk though the execution of the equals() method in class User for a few well-chosen objects as the parameter. What happens at each point in the execution?

Q. Write a script file on Octave based on the following criteria. The users should be able to add a value for ‘x’ and then see the output ‘y’ when the program is run.

Q. What is a sequence of characters enclosed within quotation marks or apostrophes, and is python’s data type for storing text?

Q. Decide whether or not the following are statements. In the case of a statement, say if it is true or false, if possible.

Q. Express each statement or open sentence in a symbolic form such as P∧Q, P∨Q, P∨∼Q or ∼P, etc. Be sure to also state exactly what statements P and Q stand for:

Was this guide helpful?
Fdaytalk

The Fdaytalk editorial team tests games, apps and devices and writes clear, step-by-step fixes and guides.

More from Fdaytalk →