Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Drive
- {
- public static double DrivingCost(double drivenMiles, double milesPerGallon, double dollarsPerGallon)
- {
- // calculate gallons of ful used
- double gallons = drivenMiles / milesPerGallon;
- // calculate the cost
- double dollar = gallons * dollarsPerGallon;
- return dollar;
- }
- public static void main(String[] args)
- {
- // create a Scanner object
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter the car\'s miles/gallon : ");
- // get user input
- double milesPerGallon = sc.nextDouble();
- System.out.print("Enter the car\'s gas dollars/gallon : ");
- // get user input
- double dollarsPerGallon = sc.nextDouble();
- System.out.println("Cost for 10 miles : $" + DrivingCost(10 , milesPerGallon , dollarsPerGallon));
- System.out.println("Cost for 50 miles : $" + DrivingCost(50 , milesPerGallon , dollarsPerGallon));
- System.out.println("Cost for 400 miles : $" + DrivingCost(400 , milesPerGallon , dollarsPerGallon));
- }
- }
- Sample Output
- https://media.cheggcdn.com/media%2Fd20%2Fd20e4f7d-d491-470d-9be0-bbad8b8ced8f%2Fphp8H4GPT.png
Advertisement
Add Comment
Please, Sign In to add comment