In this task, you are being asked to write a given word problem as a program.
Write a program that asks the user to input (using a Scanner object):
- The number of gallons of fuel in the tank
- The fuel efficiency in miles per gallon
- The price of fuel per gallon
Code:
import java.util.Scanner;public class Lab4Task6{public static void main(String[] args) {Scanner Ahsan = new Scanner(System.in);double noOfGalons, fuelEfficiency, price;int hundredmile = 100;System.out.print("Enter the no of Gallons of gas in tank: ");noOfGalons = Ahsan.nextDouble();System.out.print("Enter the Fuel Efficiency: ");fuelEfficiency = Ahsan.nextDouble();System.out.print("Enter the Price of Fuel per gallon: ");price = Ahsan.nextDouble();double CostPerHundredmile = (hundredmile / fuelEfficiency ) * price;System.out.println("Cost of per 100 mile is: " + CostPerHundredmile);double distance = noOfGalons * fuelEfficiency;System.out.println("Total distance cover by car is: " + distance + "miles");}}