is expensive. Write a program with a car's miles/gallon and gas dollars/gallon (both doubles) as input, and output the gas cost les, 75 miles, and 500 miles. ut each floating-point value with two digits after the decimal point, which can be achieved as follows: tem.out.printf("%.2f", yourValue); e output ends with a newline.

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
icon
Related questions
icon
Concept explainers
Question

Java

Code a program that outputs miles/gallon and gas dollars/gallon.

### 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 20 miles, 75 miles, and 500 miles.

Output each floating-point value with two digits after the decimal point, which can be achieved as follows:

```java
System.out.printf("%.2f", yourValue);
```

The output ends with a newline.

#### Example
If the input is:
```
20.0 3.1599
```
the output is:
```
3.16 11.85 79.00
```

Note: Real per-mile cost would also include maintenance and depreciation.

#### Graphical Explanation

There is a section for "LAB ACTIVITY" indicating the current activity (2.20.1: LAB: Driving costs) and a scripting environment showing the beginning of the Java code:

```java
import java.util.Scanner;

public class LabProgram {
    public static void main(String[] args) {
        ...
    }
}
```

In this scenario, you would need to complete writing the Java program to perform the specified calculations.

---

This tabulation helps students to not only understand the theoretical aspects of programming but also gives them practical exposure to coding and handling real-world problems.
Transcribed Image Text:### 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 20 miles, 75 miles, and 500 miles. Output each floating-point value with two digits after the decimal point, which can be achieved as follows: ```java System.out.printf("%.2f", yourValue); ``` The output ends with a newline. #### Example If the input is: ``` 20.0 3.1599 ``` the output is: ``` 3.16 11.85 79.00 ``` Note: Real per-mile cost would also include maintenance and depreciation. #### Graphical Explanation There is a section for "LAB ACTIVITY" indicating the current activity (2.20.1: LAB: Driving costs) and a scripting environment showing the beginning of the Java code: ```java import java.util.Scanner; public class LabProgram { public static void main(String[] args) { ... } } ``` In this scenario, you would need to complete writing the Java program to perform the specified calculations. --- This tabulation helps students to not only understand the theoretical aspects of programming but also gives them practical exposure to coding and handling real-world problems.
### LAB Activity: Driving Costs

#### Lab Activity 2.20.1: LAB: Driving Costs

In this activity, you will be working on calculating driving costs using a Java program. Below is the initial code provided for you to start with:

```java
import java.util.Scanner;

public class LabProgram {
    public static void main(String[] args) {
        // Your code will go here
    }
}
```

- The program begins with importing the `Scanner` class from the `java.util` package, which will allow you to take input from the user.

- The `LabProgram` class is defined as `public`, making it accessible from outside its package. Inside this class, there is a `main` method, which serves as the entry point of the Java application.

- The body of the `main` method is currently empty, and you will need to add the logic to calculate the driving costs.

### Instructions:

1. **Import the Scanner Class**: This allows you to read input from standard input (keyboard). This has already been done for you in the template.
   
2. **Initialize the Scanner**:
    ```java
    Scanner scnr = new Scanner(System.in);
    ```

3. **Read user inputs** for the following:
    - Cost of gas per gallon
    - Miles per gallon the car gets
    - Driving distance in miles

4. **Calculate the cost of driving** for the entered distance:
    - Use the formula `TotalCost = (Distance / MilesPerGallon) * CostPerGallon`

5. **Print the result** in a user-friendly format.

### Example Code Addition:
Here's an example to guide you:

```java
import java.util.Scanner;

public class LabProgram {
    public static void main(String[] args) {
        
        Scanner scnr = new Scanner(System.in);

        // Get inputs
        System.out.println("Enter cost per gallon:");
        double costPerGallon = scnr.nextDouble();

        System.out.println("Enter miles per gallon:");
        double milesPerGallon = scnr.nextDouble();

        System.out.println("Enter distance in miles:");
        double distance = scnr.nextDouble();

        // Calculate total cost
        double totalCost = (distance / milesPerGallon) * costPerGallon;

        // Print the result
        System.out.printf("Cost of driving %.2f miles: $%.2f\n", distance,
Transcribed Image Text:### LAB Activity: Driving Costs #### Lab Activity 2.20.1: LAB: Driving Costs In this activity, you will be working on calculating driving costs using a Java program. Below is the initial code provided for you to start with: ```java import java.util.Scanner; public class LabProgram { public static void main(String[] args) { // Your code will go here } } ``` - The program begins with importing the `Scanner` class from the `java.util` package, which will allow you to take input from the user. - The `LabProgram` class is defined as `public`, making it accessible from outside its package. Inside this class, there is a `main` method, which serves as the entry point of the Java application. - The body of the `main` method is currently empty, and you will need to add the logic to calculate the driving costs. ### Instructions: 1. **Import the Scanner Class**: This allows you to read input from standard input (keyboard). This has already been done for you in the template. 2. **Initialize the Scanner**: ```java Scanner scnr = new Scanner(System.in); ``` 3. **Read user inputs** for the following: - Cost of gas per gallon - Miles per gallon the car gets - Driving distance in miles 4. **Calculate the cost of driving** for the entered distance: - Use the formula `TotalCost = (Distance / MilesPerGallon) * CostPerGallon` 5. **Print the result** in a user-friendly format. ### Example Code Addition: Here's an example to guide you: ```java import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); // Get inputs System.out.println("Enter cost per gallon:"); double costPerGallon = scnr.nextDouble(); System.out.println("Enter miles per gallon:"); double milesPerGallon = scnr.nextDouble(); System.out.println("Enter distance in miles:"); double distance = scnr.nextDouble(); // Calculate total cost double totalCost = (distance / milesPerGallon) * costPerGallon; // Print the result System.out.printf("Cost of driving %.2f miles: $%.2f\n", distance,
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Operators
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.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
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)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education