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

Respuesta :

Answer:

The java program for the given scenario is as follows.

import java.util.Scanner;

import java.lang.*;

public class Test

{

   //variables to hold miles per gallon and cost per gallon

   static double miles_gallon, dollars_gallon;

   public static void main(String[] args)

   {

       Scanner sc = new Scanner(System.in);

       System.out.print("Enter the car's mileage in miles per gallon: ");

       miles_gallon=sc.nextDouble();

       System.out.print("Enter the cost of gas in dollars per gallon: ");

       dollars_gallon=sc.nextDouble();

       System.out.printf("The cost of gas for 10 miles is $%.2f\n", drivingCost(10, miles_gallon, dollars_gallon));

       System.out.printf("The cost of gas for 50 miles is $%.2f\n", drivingCost(50, miles_gallon, dollars_gallon));

       System.out.printf("The cost of gas for 400 miles is $%.2f\n", drivingCost(400, miles_gallon, dollars_gallon));

   }

   //method to compute cost of gas

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

   {

       double cost = ((drivenMiles/milesPerGallon)*dollarsPerGallon);

       return cost;

   }

}

OUTPUT

Enter the car's mileage in miles per gallon: 12

Enter the cost of gas in dollars per gallon: 23

The cost of gas for 10 miles is $19.17

The cost of gas for 50 miles is $95.83

The cost of gas for 400 miles is $766.67

Explanation:

1. Two double variables to hold user input for miles per gallon and cost of gas per gallon, are declared.

2. The variables are declared static, and at the class level (outside main()).

3. Inside main(), an object of Scanner class is created.

4. User input is taken for miles per gallon and cost of gas per gallon.

5. Next, the method, drivingCost() is called. This method takes three parameters - miles per gallon, cost of gas per gallon and miles driven. Based on these parameters and the given formula, the cost of gas is computed.

6. This value is then displayed to the user.

7. The method, drivingCost(), is called three times for 10 miles, 50 miles and 400 miles being driven.

8. The whole code is written inside a class since java is a purely object-oriented language.

9. The object of the class is not created since only a single class is used.

10. The program is saved as Test.java.

In this exercise we have to use the knowledge in computational language in JAVA to describe a code that best suits, so we have:

The code can be found in the attached image.

To make it simpler we can write this code as:

import java.util.Scanner;

import java.lang.*;

public class Test

{

  //variables to hold miles per gallon and cost per gallon

  static double miles_gallon, dollars_gallon;

  public static void main(String[] args)

  {

      Scanner sc = new Scanner(System.in);

      System.out.print("Enter the car's mileage in miles per gallon: ");

      miles_gallon=sc.nextDouble();

      System.out.print("Enter the cost of gas in dollars per gallon: ");

      dollars_gallon=sc.nextDouble();

      System.out.printf("The cost of gas for 10 miles is $%.2f\n", drivingCost(10, miles_gallon, dollars_gallon));

      System.out.printf("The cost of gas for 50 miles is $%.2f\n", drivingCost(50, miles_gallon, dollars_gallon));

      System.out.printf("The cost of gas for 400 miles is $%.2f\n", drivingCost(400, miles_gallon, dollars_gallon));

  }

  //method to compute cost of gas

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

  {

      double cost = ((drivenMiles/milesPerGallon)*dollarsPerGallon);

      return cost;

  }

}

See more about JAVA at brainly.com/question/19705654

Ver imagen lhmarianateixeira
Q&A Education