Respuesta :
Answer:
The program to this question can be given as:
Program:
import java.util.*; //import package.
public class Main //define class main.
{
public static double drivingCost(double drivenMiles,double milesPerGallon,double dollarsPerGallon) //define method drivingCost.
{
return (drivenMiles/milesPerGallon)*dollarsPerGallon; //return value.
}
public static void main(String[] args) //define main method
{
double milesPerGallon,dollarsPerGallon,cost; //define variables.
Scanner ob=new Scanner(System.in); //creating scanner class object
System.out.println("Enter milesPerGallon value :");
milesPerGallon=ob.nextDouble(); //taking input from user.
System.out.println("Enter dollarsPerGallon value :");
dollarsPerGallon=ob.nextDouble(); //taking input from user.
cost=drivingCost(10,milesPerGallon,dollarsPerGallon); //calling a function.
System.out.println(cost); //print value.
cost=drivingCost(50,milesPerGallon,dollarsPerGallon); //calling a function.
System.out.println(cost); //print value.
cost=drivingCost(400,milesPerGallon,dollarsPerGallon); //calling a function.
System.out.println(cost); //print value.
}
}
Output:
Enter milesPerGallon value :
20.0
Enter dollarsPerGallon value :
3.1599
1.57995
7.89975
63.198
Explanation:
The above java program description can be given as:
- In this program first, we import the package for user input. Then we define a class. In this class, we define a method that is "drivingCost()".
- The drivingCost() method takes three parameters that are drivenMiles, milesPerGallon, and dollarsPerGallon. The return type of this method is double. In this method, we calculate and return the dollar cost to drive those miles.
- Then we define the main method. In the main method, we declare variable and create the scanner class object and take input in the variable milesPerGallon, and dollarsPerGallon. and pass the value to the drivingCost() method. and define a variable cost that is used to take the return value of this method.