217k views
4 votes
Write a method drivingCost() with input parameters drivenMiles, milesPerGallon, and dollarsPerGallon, that returns the dollar cost to drive those miles. All items are of type double. If the method is called with 50 20.0 3.1599, the method returns 7.89975. Define that method in a program whose inputs are the car's miles/gallon and the gas dollars/gallon (both doubles). Output the gas cost for 10 miles, 50 miles, and 400 miles, by calling your drivingCost() method three times.

1 Answer

5 votes

Final answer:

The student's question pertains to writing a method called drivingCost() that calculates the cost of driving given the distance to be driven, fuel efficiency of the car, and fuel cost per gallon. The method will be used in a program to calculate and display the costs for driving 10, 50, and 400 miles.

Step-by-step explanation:

The question asks for a method to be written in a programming context that calculates the cost of driving a certain number of miles given the car's fuel efficiency (miles per gallon) and the cost of fuel (dollars per gallon). The method drivingCost() takes three parameters: drivenMiles, milesPerGallon, and dollarsPerGallon, all of which are of type double. It calculates and returns the dollar cost of driving the specified number of miles.

For example, if the drivingCost() method is called with the arguments 50 for drivenMiles, 20.0 for milesPerGallon, and 3.1599 for dollarsPerGallon, it should return 7.89975. This is calculated as follows: (50 miles / 20.0 miles per gallon) * 3.1599 dollars per gallon = 7.89975 dollars.

To provide the full requested functionality, the program will prompt for the two input parameters (miles per gallon and dollars per gallon), and then output the cost of gas for three different mileages (10, 50, and 400 miles) by calling the drivingCost() method.

User Benoit Pasquier
by
8.0k points