Write a method drivingCost() with input parameters milesPerGallon, dollars PerGallon, and miles Driven that returns the dollar cost to drive those miles. All items are of type double. The method called with arguments (20.0, 3.1599, 50.0) returns 7.89975. Define that method in a program whose inputs are the car's miles per gallon and the price of gas in dollars per gallon (both doubles). Output the gas cost for 10 miles, 50 miles, and 400 miles, by calling your driving Cost() 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
Write a method drivingCost() with input parameters milesPerGallon, dollars PerGallon, and miles Driven that returns the dollar cost to drive those miles. All items are of type double. The method called with arguments (20.0, 3.1599, 50.0) returns 7.89975. Define that method in a program whose inputs are the car's miles per gallon and the price of gas in dollars per gallon (both doubles). Output the gas cost for 10 miles, 50 miles, and 400 miles, by calling your driving Cost() 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
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
Java
Write a method drivingCost.
![### Lab Exercise: Driving Cost Calculation using Methods in Java
#### Description:
In this lab exercise, you will write a method `drivingCost()` that takes input parameters `milesPerGallon`, `dollarsPerGallon`, and `milesDriven` to calculate and return the dollar cost to drive those miles. All inputs are of type `double`. For example, the method called with arguments `(20.0, 3.1599, 50.0)` returns `7.89975`.
#### Task:
Define this method in a Java program. The program’s inputs are the car's miles per gallon and the price of gas in dollars per gallon (both as doubles). You will output the gas cost for 10 miles, 50 miles, and 400 miles by calling your `drivingCost()` method three times.
Each floating-point value should be output with two digits after the decimal point. You can achieve this using:
```java
System.out.printf("%.2f", yourValue);
```
Note that the output ends with a newline.
#### Example:
If the input is:
```
20.0 3.1599
```
the output is:
```
1.58 7.90 63.20
```
#### Instructions:
Your program must define and call a method:
```java
public static double drivingCost(double milesPerGallon, double dollarsPerGallon, double milesDriven)
```
Below is the starter code for your lab activity:
```java
import java.util.Scanner;
public class LabProgram {
/* Define your method here */
public static void main(String[] args) {
/* Type your code here. */
}
}
```
---
#### Steps to Complete the Lab:
1. **Method Definition:**
- The `drivingCost` method will calculate the cost based on the formula:
\[
\text{Cost} = \left(\frac{\text{milesDriven}}{\text{milesPerGallon}}\right) \times \text{dollarsPerGallon}
\]
2. **Input Handling:**
- Read the car's miles per gallon and price of gas in dollars per gallon from the user.
- Call the `drivingCost` method for the predefined miles (10, 50, 400).
3. **Output Formatting:**
- Print each result formatted to two decimal places.
#### Complete](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F129bcf88-7726-463b-9834-6ce998bc2339%2F9bea51e0-f945-4775-beae-dd3746a960b3%2Fqubp8v_processed.jpeg&w=3840&q=75)
Transcribed Image Text:### Lab Exercise: Driving Cost Calculation using Methods in Java
#### Description:
In this lab exercise, you will write a method `drivingCost()` that takes input parameters `milesPerGallon`, `dollarsPerGallon`, and `milesDriven` to calculate and return the dollar cost to drive those miles. All inputs are of type `double`. For example, the method called with arguments `(20.0, 3.1599, 50.0)` returns `7.89975`.
#### Task:
Define this method in a Java program. The program’s inputs are the car's miles per gallon and the price of gas in dollars per gallon (both as doubles). You will output the gas cost for 10 miles, 50 miles, and 400 miles by calling your `drivingCost()` method three times.
Each floating-point value should be output with two digits after the decimal point. You can achieve this using:
```java
System.out.printf("%.2f", yourValue);
```
Note that the output ends with a newline.
#### Example:
If the input is:
```
20.0 3.1599
```
the output is:
```
1.58 7.90 63.20
```
#### Instructions:
Your program must define and call a method:
```java
public static double drivingCost(double milesPerGallon, double dollarsPerGallon, double milesDriven)
```
Below is the starter code for your lab activity:
```java
import java.util.Scanner;
public class LabProgram {
/* Define your method here */
public static void main(String[] args) {
/* Type your code here. */
}
}
```
---
#### Steps to Complete the Lab:
1. **Method Definition:**
- The `drivingCost` method will calculate the cost based on the formula:
\[
\text{Cost} = \left(\frac{\text{milesDriven}}{\text{milesPerGallon}}\right) \times \text{dollarsPerGallon}
\]
2. **Input Handling:**
- Read the car's miles per gallon and price of gas in dollars per gallon from the user.
- Call the `drivingCost` method for the predefined miles (10, 50, 400).
3. **Output Formatting:**
- Print each result formatted to two decimal places.
#### Complete
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you

Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON

Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education