Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/** A class suitable for the simulating a car driving. */ public class Car implements Comparable { private String name; private double fuelEfficiency; private double

/**

A class suitable for the simulating a car driving.

*/

public class Car implements Comparable

{

private String name;

private double fuelEfficiency;

private double gasLevel;

/**

Initializes a car with a given fuel efficiency

@param fuelEfficiency the default fuel efficiency

*/

public Car(String name, double fuelEfficiency)

{

this.name = name;

this.fuelEfficiency = fuelEfficiency;

gasLevel = 0;

}

/**

Puts gas in the tank.

@param gas amount of gas to add

*/

public void addGas(double gas)

{

this.gasLevel = gasLevel + gas;

}

/**

Simulates driving the car and thus reducing the gas in the tank

@param distance miles driven

*/

public void drive(double distance)

{

gasLevel -= distance * fuelEfficiency;

}

/**

Returns the current gas level.

@return current gas level

*/

public double getGasLevel()

{

return gasLevel;

}

//-----------Start below here. To do: approximate lines of code = 4

// write a method to implement the Comparable interface

// The method should compare two cars based on fuel efficiency (higher fuel efficiency is better)

//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.

public String toString()

{

return "Name: " + name + " Gas Level: " + gasLevel + " FuelEfficiency: " + fuelEfficiency;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learn To Program Databases With Visual Basic 6

Authors: John Smiley

1st Edition

1902745035, 978-1902745039

More Books

Students also viewed these Databases questions

Question

What are the need and importance of training ?

Answered: 1 week ago

Question

What is job rotation ?

Answered: 1 week ago