Ask Question
27 June, 17:21

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.

+4
Answers (1)
  1. 27 June, 18:03
    0
    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.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a method drivingCost () with input parameters drivenMiles, milesPerGallon, and dollarsPerGallon, that returns the dollar cost to ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers