Develop a Java application that provides program output in a logical manner and incorporates appropriate data types. Similar to zyBooks examples in Chapter 2, your program should prompt a user to enter a car brand, model, year, starting odometer reading, ending odometer reading, and gallons used. The output should include this information along with the estimated miles per gallon consumed by the vehicle in the format MPG: your calculation. Print each on separate lines with the appropriate labels (example, MPG: 25) Submit your source code and screenshots of the application executing with output in a single document.

Respuesta :

Answer:

A java application was designed to provide a program output in a logical manner and incorporates appropriate data types.

Explanation:

Solution

Program:

import java.util.Scanner;

public class Test {

  public static void main(String[] args) {

      //for taking console input

      Scanner sc = new Scanner(System.in);      

      //varibles for holding the values

      String brand, model;

      int year, startOdo, endOdo;

      double gallons, mpg;        

      //taking user inputs

      System.out.print("Enter Car Brand: "); brand = sc.nextLine();

      System.out.print("Enter Car Model: "); model = sc.nextLine();

      System.out.print("Enter Car Year: "); year = sc.nextInt(); sc.nextLine();

      System.out.print("Enter Starting Odometer reading: "); startOdo = sc.nextInt(); sc.nextLine();

      System.out.print("Enter Ending Odometer reading: "); endOdo = sc.nextInt(); sc.nextLine();

      System.out.print("Enter gallons used: "); gallons = sc.nextDouble(); sc.nextLine();        

      //calculating the mpg by dividing the total distance with fuel used

      mpg = (endOdo - startOdo) / gallons;      

      //showing the output

      System.out.println("\n*********Car Details*********");

      System.out.println("Brand: "+brand);

      System.out.println("Model: "+model);

      System.out.println("Year: "+year);

      System.out.println("Starting Odometer: "+startOdo);

      System.out.println("End Odometer: "+endOdo);

      System.out.println("Gallons Used: "+gallons);

      System.out.printf("MPG: %.2f",mpg);    

      sc.close();

}

}

ACCESS MORE
EDU ACCESS