467,462 views
19 votes
19 votes
Define that function in a program whose inputs are the car's miles/gallon and the gas dollars/gallon (both floats). Output the gas cost for 10 miles, 50 miles, and 400 miles, by calling your DrivingCost function three times.

Ex: If the input is 20.0 3.1599, the output is:
Write a function DrivingCost with parameters drivenMiles, milesPerGallon, and dollarsPerGallon, that returns the dollar cost to drive those miles. All items are of type float.
Ex: If the function is called with 50 20.0 3.1599, the function returns 7.89975.

User Nic Scozzaro
by
3.0k points

1 Answer

7 votes
7 votes

Answer:

Step-by-step explanation:

The following is written in Java. The first DrivingCost function requested in the question cannot be made and will not work because the cost cannot be calculated without the miles driven being input as a seperate variable. The second DrivingCost function with all three variables was created and tested using the example in the question. The test and output can be seen in the attached image below highlighted in red.

public static float DrivingCost(float drivenMiles, float milesPerGallon, float dollarsPerGallon) {

float gallonsUsed = drivenMiles / milesPerGallon;

float cost = dollarsPerGallon * gallonsUsed;

return cost;

}

Define that function in a program whose inputs are the car's miles/gallon and the-example-1
User Mnk
by
2.8k points