import java.io.PrintWriter; import java.io.File; import java.util.Scanner; class Car { private String make, model; private String color; private double sizeOfTank; private double galsRemaining; private double gasMileage; private int odo; private int yearManufactured; private static int totalCars = 0; Car(Scanner s) throws NoMoreCarsException { String firstLine = s.nextLine(); // throw away "Here be a car" if (firstLine.equals("--END--")) { throw new NoMoreCarsException(); } this.make = s.nextLine(); this.model = s.nextLine(); this.color = s.nextLine(); this.sizeOfTank = Double.valueOf(s.nextLine()); } Car(String make, String model, int yearsOld) { this.make = make; this.model = model; this.yearManufactured = 2024 - yearsOld; this.odo = 0; // brand new car!! this.color = "gray"; if (make.equals("Chevy")) { this.sizeOfTank = 100; this.gasMileage = 4; } else { this.sizeOfTank = 20; this.gasMileage = 34; } this.galsRemaining = this.sizeOfTank; totalCars += 1; } void persist(PrintWriter pw) { pw.println("Here be a car"); pw.println(this.make); pw.println(this.model); pw.println(this.color); pw.println(this.sizeOfTank); } int getYearsOld() { return 2024 - this.yearManufactured; } double getGalsRemaining() { return this.galsRemaining; } void fillUp() { this.galsRemaining = this.sizeOfTank; } int getTankPerc() { return (int) ((this.galsRemaining / this.sizeOfTank) * 100); } void drive(int numMiles) throws Exception { double galsConsumed = numMiles / this.gasMileage; if (galsConsumed > this.galsRemaining) { throw new Exception(); } else { this.galsRemaining = this.galsRemaining - galsConsumed; this.odo = this.odo + numMiles; } } String getCarModel() { return this.model; } String getColor() { return this.color; } void setColor(String color) { this.color = color; } public String toString() { return "a " + this.make + " " + this.model + " with " + this.galsRemaining + " gallons in the tank"; } public static void main(String args[]) { Car honda = new Car("Chevy", "Accord",10); Car ferrari = new Car("Ferrari", "F1",0); Car stephensCar = ferrari; System.out.println("The Honda's model is: " + honda.getCarModel()); System.out.println("The Ferrari is colored " + ferrari.getColor()); System.out.println("The Honda is colored " + honda.getColor()); honda.setColor("plaid"); try { PrintWriter pw = new PrintWriter(new File("/home/stephen/lulu/oscar.aww")); honda.persist(pw); pw.flush(); pw.close(); } catch (Exception e) { e.printStackTrace(); } try { honda.drive(125000); } catch (Exception e) { e.printStackTrace(); } ferrari.setColor("red"); System.out.println("The Ferrari is colored " + ferrari.getColor()); System.out.println("The Honda is colored " + honda.getColor()); System.out.println("Stephen's is colored " + stephensCar.getColor()); System.out.println("There are a total of " + totalCars + " cars in the universe"); System.out.println("The Honda is " + honda); System.out.println("The Ferrari is " + ferrari); System.out.println("Stephen's car is " + stephensCar); } }