jaredec18

Untitled

Sep 15th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Drive
  4.  
  5. {
  6.  
  7. public static double DrivingCost(double drivenMiles, double milesPerGallon, double dollarsPerGallon)
  8.  
  9. {
  10.  
  11. // calculate gallons of ful used
  12.  
  13. double gallons = drivenMiles / milesPerGallon;
  14.  
  15.  
  16.  
  17. // calculate the cost
  18.  
  19. double dollar = gallons * dollarsPerGallon;
  20.  
  21.  
  22.  
  23. return dollar;
  24.  
  25. }
  26.  
  27.  
  28.  
  29. public static void main(String[] args)
  30.  
  31. {
  32.  
  33. // create a Scanner object
  34.  
  35. Scanner sc = new Scanner(System.in);
  36.  
  37.  
  38.  
  39. System.out.print("Enter the car\'s miles/gallon : ");
  40.  
  41.  
  42.  
  43. // get user input
  44.  
  45. double milesPerGallon = sc.nextDouble();
  46.  
  47.  
  48.  
  49. System.out.print("Enter the car\'s gas dollars/gallon : ");
  50.  
  51.  
  52.  
  53. // get user input
  54.  
  55. double dollarsPerGallon = sc.nextDouble();
  56.  
  57.  
  58.  
  59. System.out.println("Cost for 10 miles : $" + DrivingCost(10 , milesPerGallon , dollarsPerGallon));
  60.  
  61. System.out.println("Cost for 50 miles : $" + DrivingCost(50 , milesPerGallon , dollarsPerGallon));
  62.  
  63. System.out.println("Cost for 400 miles : $" + DrivingCost(400 , milesPerGallon , dollarsPerGallon));
  64.  
  65. }
  66.  
  67. }
  68.  
  69. Sample Output
  70. https://media.cheggcdn.com/media%2Fd20%2Fd20e4f7d-d491-470d-9be0-bbad8b8ced8f%2Fphp8H4GPT.png
Advertisement
Add Comment
Please, Sign In to add comment