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