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.

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.

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.

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.

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.

Step 5: Testing the Class

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

Full Code

Here is the complete code, combining all steps:

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:

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

    Comments