SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Console Input And Output
Exercise:
Programming Projects
Question:4 | ISBN:9780132830317 | Edition: 5

Question

An automobile is used for commuting purposes. Write a program that takes as input the distance of the commute in miles, the automobile’s fuel consumption rate in miles per gallon, and the price of a gallon of gas. The program should then output the cost of the commute.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

import java.util.Scanner;
public class AutomobileDemo
{

	public static void main(String[] args) 
	{
		double distance, mpg, gasPrice;
		Scanner scn = new Scanner(System.in);
		System.out.print("Enter the distance in miles:");
		distance = scn.nextDouble();
		System.out.print("Enter rate of the car (mpg):");
		mpg = scn.nextDouble();
		System.out.print("Enter the cost of one gallon:");
		gasPrice = scn.nextDouble();
		System.out.println("Cost of the commute:" +(distance / mpg * gasPrice));
	}
}

OUTPUT:

Enter the distance in miles:150
Enter rate of the car (mpg):60
Enter the cost of one gallon:15.5
Cost of the commute:38.75

 

0 0

Discussions

Post the discussion to improve the above solution.