Car-Pool Savings Calculator in java

Code Blah
2 min readFeb 14, 2019

--

(Car-Pool Savings Calculator) Research several car-pooling websites. Create an application that calculates your daily driving cost, so that you can estimate how much money could be saved by car pooling, which also has other advantages such as reducing carbon emissions and reducing traffic congestion. The application should input the following information and display the user’s cost per day of driving to work: a) Total miles driven per day. b) Cost per gallon of gasoline. c) Average miles per gallon. d) Parking fees per day.

e) Tolls per day.

/*
* Filename: CarPoolSavingsCalc.java
*
* Description: Exercise 2.35 - Car-Pool Savings Calculator
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* =====================================================================================
*/
import java.util.Scanner;

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

double milesPerDay, costPerGallon, milesPerGallon;
double parkingFees, tollsPerDay, dailyDrivingCost;

milesPerDay = requestInput("Enter total miles driven per day: ", sc);
costPerGallon = requestInput("Enter cost per gallon for gasoline: ", sc);
milesPerGallon = requestInput("Enter average miles per gallon: ", sc);
parkingFees = requestInput("Enter parking fees per day: ", sc);
tollsPerDay = requestInput("Enter tolls per day: ", sc);

// calculate daily driving cost
dailyDrivingCost = ((milesPerDay / milesPerGallon) * costPerGallon) + parkingFees + tollsPerDay;

System.out.printf("Daily driving cost = %.2f\n", dailyDrivingCost);
}
// space saving in main()
private static double requestInput(String s, Scanner input){
System.out.print(s);
return input.nextDouble();
}
}

Method 2 : Car-Pool Savings Calculator in java

/**
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* Exercise 2.35 - Car-Pool Savings Calculator
* This Program Calculates Your Daily Driving Cost
*
*/

import java.util.Scanner;

public class Ex02_35 {
public static void main (String [] args) {

Scanner input = new Scanner (System.in);

int totalMiles;
int gasolineCost;
int milesPerGallon;
int parkingFees;
int tolls;
int dailyDrivingCost;

System.out.println ("This Application Calculates Your Daily Driving Cost");

System.out.println (); //displays a blank line

System.out.print ("Enter Total Miles Driven Per Day: ");
totalMiles = input.nextInt();
System.out.print ("Enter Cost Per Gallon Of Gasoline: ");
gasolineCost = input.nextInt();
System.out.print ("Enter Average Miles Per Gallon: ");
milesPerGallon = input.nextInt();
System.out.print ("Enter Parking Fees Per Day: ");
parkingFees = input.nextInt();
System.out.print ("Enter Tolls Per Day: ");
tolls = input.nextInt();

dailyDrivingCost = (totalMiles / milesPerGallon) * gasolineCost + parkingFees + tolls;

System.out.println("\n");
System.out.printf ("Your Daily Driving Cost Is: %d\n", dailyDrivingCost);

}
}

Originally published at codeblah.com on February 14, 2019.

--

--

Code Blah

Software Engineer and Architect, Teacher, Writer,Android Developer.|Coding | Programming | Blogger