computer science homework
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package tangk1_ica01; import java.io.PrintStream; import static java.lang.System.out; import java.util.Scanner; /** * * @author tangk1 */ public class Tangk1_ICA01 { final static Scanner cin = new Scanner(System.in); final static PrintStream cout = System.out; static int speed; static int year; static String make; static double odometer; static double mpg; static double tankCap; static double fuel; static double fueladded; static double spilledfuel; /** * @param args the command line arguments */ public static void main(String[] args) { init(); runMenu(); } private static void init() { out.print("Make? "); make = cin.next(); out.print("Model Year? "); year = cin.nextInt(); out.print("Miles per gallon? "); mpg = cin.nextDouble(); out.print("Tank capacity? "); tankCap = cin.nextDouble(); fuel = tankCap / 4; // car is delivered with quarter tank of fuel speed = 0; display(); } private static void runMenu() { char cmd; do { out.print("\nA/B/D/R/Q? "); cmd = cin.next().charAt(0); if (cmd == 'A') accel(); else if (cmd == 'B') brake(); else if (cmd == 'D') drive(); else if (cmd == 'R') refuel(); else if (cmd != 'Q') out.println("*** Invalid command ***"); display(); } while (cmd != 'Q'); } private static void display() { out.println("\n" + year + " "+ make + ", speed = " + speed + " mph"); out.println("Odometer = " + odometer + ", Fuel = " + fuel + " gallons"); } private static void accel() { // add code to check that there is fuel before accelerating! // if no fuel, speed must be 0 if(fuel > 0){ speed = speed + 10; } else{ out.println("no fuel"); } } private static void brake() { speed = speed - 10; if (speed < 0) speed = 0; } private static void drive() { // write the code if (speed > 0){ odometer = mpg * fuel; } else{ out.println("accelerate first"); } } private static void refuel() { // write the code if (speed > 0){ out.println("brake first"); } else{ out.print("How much fuel do you want to add?"); fueladded = cin.nextInt(); if (fueladded > fuel){ spilledfuel = fueladded - fuel; out.println("you fuel spilled"+spilledfuel); } } } }