Question

Driving cost - methods

3.14 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.


2 2
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1
import java.util.Scanner;

public class LabProgram {
   
   public static double drivingCost(double drivenMiles, double milesPerGallon, double dollarsPerGallon) {
      
      double cost;
      
      cost = drivenMiles / milesPerGallon * dollarsPerGallon;
      
      return cost;
      
   }
   
   public static void main(String[] args) {
      
      Scanner scnr = new Scanner(System.in);
      
      double milesPerGallon;
      double dollarsPerGallon;
      double cost;
      
      milesPerGallon = scnr.nextDouble();
      dollarsPerGallon = scnr.nextDouble();
      
      cost = drivingCost(10, milesPerGallon, dollarsPerGallon);
      System.out.printf("%.2f", cost);
      
      cost = drivingCost(50, milesPerGallon, dollarsPerGallon);
      System.out.printf(" %.2f", cost);
      
      cost = drivingCost(400, milesPerGallon, dollarsPerGallon);
      System.out.printf(" %.2f\n", cost);
      
   }
   
}


Add a comment
Know the answer?
Add Answer to:
Driving cost - methods
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
  • Write a method drivingCost() with input parameters drivenMiles, milesPerGallon, and dollarsPerGallo

    6.26 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...

  • 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,...

  • 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...

  • // Enter your name as a comment for program identification // Program assignment TripCost.cpp // Enter...

    // Enter your name as a comment for program identification // Program assignment TripCost.cpp // Enter your class section, and time /* The program TripCost.cpp uses pointers to calculate    the cost of a trip depending on the cost of a gallon    of gas, the number of miles, and the number of miles    per gallon a car gets. */ /* The cost of gas, the number of miles, and number of    miles per gallon is entered. */...

  • JAVA: amortization table: Hi, I have built this code and in one of my methods: printDetailsToConsole...

    JAVA: amortization table: Hi, I have built this code and in one of my methods: printDetailsToConsole I would like it to print the first 3 payments and the last 3 payments if n is larger than 6 payments. Please help! Thank you!! //start of program import java.util.Scanner; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.lang.Math; //345678901234567890123456789012345678901234567890123456789012345678901234567890 /** * Write a description of class MortgageCalculator here. * * @author () * @version () */ public class LoanAmortization {    /* *...

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