Java Reference
In-Depth Information
Exercise P3.5. Implement a class
Car
with the following properties. A car
has a certain fuel efficiency (measured in miles/gallon or liters/kmȌpick
one) and a certain amount of fuel in the gas tank. The efficiency is
specified in the constructor, and the initial fuel level is 0. Supply a method
drive
that simulates driving the car for a certain distance, reducing the
amount of gasoline in the fuel tank. Also supply methods
getGasInTank
, returning the current amount of gasoline in the fuel
tank, and
addGas
, to add gasoline to the fuel tank. Sample usage:
Car myHybrid = new Car(50); // 50 miles per gallon
myHybrid.addGas(20); // Tank 20 gallons
myHybrid.drive(100); // Drive 100 miles
double gasLeft = myHybrid.getGasInTank(); // Get
gas remaining in tank
You may assume that the drive method is never called with a distance that
consumes more than the available gas. Supply a
CarTester
class that
tests all methods.
Exercise P3.6. Implement a class
Student
. For the purpose of this
exercise, a student has a name and a total quiz score. Supply an appropriate
constructor and methods
getName()
,
addQuiz(int score)
,
getTotalScore()
, and
getAverageScore()
. To compute the
latter, you also need to store the number of quizzes that the student took.
Supply a
StudentTester
class that tests all methods.
Exercise P3.7. Implement a class
Product
. A product has a name and a
price, for example
new Product(ÑToasterÑ, 29.95)
. Supply
methods
getName
,
getPrice
, and
reducePrice
. Supply a program
ProductPrinter
that makes two products, prints the name and price,
reduces their prices by $5.00, and then prints the prices again.
126
127
Exercise P3.8. Provide a class for authoring a simple letter. In the
constructor, supply the names of the sender and the recipient:
public Letter(String from, String to)
Supply a method