Question

Write a method drivingCost() with input parameters drivenMiles, milesPerGallon, and dollarsPerGallo

6.26 LAB: Driving cost - methods

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.

Output each floating-point value with two digits after the decimal point, which can be achieved as follows:
System.out.printf("%.2f", yourValue);

The output ends with a newline.

Ex: If the input is:

20.0 3.1599

the output is:

1.58 7.90 63.20

Your program must define and call a method:
public static double drivingCost(double drivenMiles, double milesPerGallon, double dollarsPerGallon)

Note: This is a lab from a previous chapter that now requires the use of a method.


4 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1


Program

import java.util.Scanner;


public class cost_driving {

    

    /* Method definition */

    public static double drivingCost(double drivenMiles, double milesPerGallon, double dollarsPerGallon) {

        double totalCost = (dollarsPerGallon * drivenMiles / milesPerGallon);

        return totalCost;

    }

    

    public static void main(String[] args) {

        Scanner scnr = new Scanner(System.in);

        System.out.print("Enter miles per gallon: ");

        double milesPerGallon = scnr.nextDouble();

        System.out.print("Enter dollars per gallon: ");

        double dollarsPerGallon = scnr.nextDouble();

        double drivenMiles = 1;

        System.out.printf("\nGas cost for 10 miles = %.2f ", drivingCost(drivenMiles, milesPerGallon, dollarsPerGallon) * 10);

        System.out.printf("\nGas cost for 50 miles = %.2f ", drivingCost(drivenMiles, milesPerGallon, dollarsPerGallon) * 50);

        System.out.printf("\nGas cost for 400 miles = %.2f\n", drivingCost(drivenMiles, milesPerGallon, dollarsPerGallon) * 400);

    }

}


Output

Enter miles per gallon: 20
Enter dollars per gallon: 3.1599

Gas cost for 10 miles = 1.58
Gas cost for 50 miles = 7.90
Gas cost for 400 miles = 63.20

Add a comment
Know the answer?
Add Answer to:
Write a method drivingCost() with input parameters drivenMiles, milesPerGallon, and dollarsPerGallo
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Driving cost - methods

    3.14 LAB: Driving cost - methodsWrite 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.Output each...

  • Write a function DrivingCost with input parameters drivenMiles, milesPerGallon, and dollarsPerGallon

    Write a function 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 function is called with 5020.03 .1599, the function returns 7.89975 .Define that function 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 function three times.Output each floating-point value with two...

  • Python 3 please. 6.27 (Functions) HW: Driving cost - functions Write a function DrivingCost with input...

    Python 3 please. 6.27 (Functions) HW: Driving cost - functions Write a function DrivingCost with input parameters drivenMiles, milesPerGallon, and dollarsPerGallon, that returns the dollar cost to drive those miles. Ex: If the function is called with 50 20.0 3.1599, the function returns 7.89975. 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...

  • PLEASE WRITE IN C++ 2.25 LAB: Driving costs Driving is expensive. Write a program with a...

    PLEASE WRITE IN C++ 2.25 LAB: Driving costs Driving is expensive. Write a program with a car's miles/gallon and gas dollars/gallon (both doubles) as input, and output the gas cost for 10 miles, 50 miles, and 400 miles. Output each floating-point value with two digits after the decimal point, which can be achieved by executing cout << fixed << setprecision(2); once before all other cout statements. Ex: If the input is: 20.0 3.1599 the output is: 1.58 7.90 63.20 Note:...

  • I'm having trouble with the following Python homework assignment question in ZyBooks: 2.14 HW: Driving costs...

    I'm having trouble with the following Python homework assignment question in ZyBooks: 2.14 HW: Driving costs Driving is expensive. Write a program with a car's miles/gallon and gas dollars/gallon (both floats) as input, and output the gas cost for 10 miles, 50 miles, and 400 miles. Ex: If the input is: 20.0 3.1599 Then the output is: 1.579950 7.899750 63.198000 Any assistance is greatly appreciated. Thanks!

  • A jiffy

    3.13 LAB: A jiffyA “jiffy” is the scientific name for 1/100th of a second. Given an input number of seconds, output the number of "jiffies."Output each floating-point value with two digits after the decimal point, which can be achieved as follows:System.out.printf("%.2f", yourValue);Ex: If the input is:15the output is:1500.00Your program must define and call a method:public static double secondsToJiffies(double userSeconds)

  • LOGIC TUI Computer Program CHALLENGE ACTIVITY 2.9.2: Code basics See Coral: Code basics for solution help....

    LOGIC TUI Computer Program CHALLENGE ACTIVITY 2.9.2: Code basics See Coral: Code basics for solution help. Jump to level 1 Write code that outputs: A3 G7 TOUS: Home 3.6. Variables/Assignments: Driving costs My library > PRG 211: Algorithms & Logic for Computer Programming home > 6: Variables/Assignments: Driving costs zyBooks catalog He Start Driving is expensive. Write a program with a car's miles/gallon and gas dollars/gallon (both floats) as input, and output the gas cost for 10 miles, 50 miles,...

  • Write program PSEUDOCODE that prompts the user for their name and then will compute a car's...

    Write program PSEUDOCODE that prompts the user for their name and then will compute a car's miles per gallon given input of miles driven and gallons of gas used. It should also convert those given numbers into metric and show kilometers driven, liters used, and kilometers per liter. All with well formatted output. Submit: C++ Programming Warm-Up Exercise PSEUDOCODE for the algorithm that compute's a car's miles per gallon given input of miles and convert these numbers into metric and...

  • Drivers are concerned with the mileage their automobiles get. One driver has kept track of several...

    Drivers are concerned with the mileage their automobiles get. One driver has kept track of several tankfuls of gasoline by recording the miles driven and gallons used for each tankful. Develop a C# app that will input the miles driven and gallons used (both as integers) for each tankful. The app should calculate and display the miles per gallon obtained for each tankful and display the combined miles per gallon obtained for all tankfuls up to this point. All averaging...

  • Write a method MaxMagnitude with two integer input parameters that returns the largest magnitude value.

    6.12 LAB: Max magnitudeWrite a method MaxMagnitude with two integer input parameters that returns the largest magnitude value. Use the method in a program that takes two integer inputs, and outputs the largest magnitude value.Ex: If the inputs are 5 7 , the method returns:7Ex: If the inputs are -8 -2, the method returns:-8Note: The method does not just return the largest value, which for -8-2 would be -2. Though not necessary, you may use the absolute-value built-in math method.Your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT