/* Using simply notepad (and without looking on the Internet) create a program that will keep track of a car's mileage, mph, gas in the tank, and the known sum spent on gas. You will create methods to simulate driving a certain distance. The program must calculate the miles driven and the gas left in the tank. The same method should also allow for the user to enter in the price of gas, which will be kept in a variable.
Download this. Open it with BlueJ.
*/
import java.util.Scanner;
public class CarSimulator{ private int mileage = 0; private int miles_per_hour = 0; private int gasTank = 0; private double gasTotal = 0; private double gasPrice = 0; private int miles_per_gallon = 0; public CarSimulator(int mpg, int gas_in_tank) { miles_per_gallon = mpg; gasTank = gas_in_tank; }
public double getGasTotal() { Scanner in = new Scanner(System.in); System.out.print("Please enter the price of gas: "); gasPrice = in.nextDouble(); gasTotal = gasPrice * gasTank; return gasTotal; }
public int distance() { mileage = miles_per_gallon * gasTank; return mileage; }
public int gasLeft() { gasTank = gasTank - (int)mileage/gasTank; return gasTank; }
public static void main(String[] args) { CarSimulator vehicle = new CarSimulator(25, 16); System.out.println("The distance you can travel is " + vehicle.distance() + " miles."); }}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment