import java.io.PrintWriter; import java.io.IOException; import java.util.Scanner; class Car { int odo; String make; String model; double galsRemaining; // gallons double gasMileage; // mpg double sizeOfTank; // gallons int year; // year of manufacture static int numCars; // Persist an existing Car object to a file. void persist(PrintWriter pw) { pw.println(this.make + " " + this.model); pw.println(this.galsRemaining); pw.println(this.gasMileage); pw.println(this.sizeOfTank); pw.println(this.year); pw.println(this.odo); pw.flush(); } // Hydrate a new Car object from the file passed as an argument. Car(Scanner s) throws NoCarException { String makeModelLine = s.nextLine(); if (makeModelLine.equals("Done with cars.")) { throw new NoCarException(); } String[] mmlparts = makeModelLine.split(" "); this.make = mmlparts[0]; this.model = mmlparts[1]; this.galsRemaining = Double.parseDouble(s.nextLine()); this.gasMileage = Double.parseDouble(s.nextLine()); this.sizeOfTank = Double.parseDouble(s.nextLine()); this.year = Integer.parseInt(s.nextLine()); this.odo = Integer.parseInt(s.nextLine()); } Car(String make, String model, int year) { this.make = make; this.model = model; this.year = year; this.odo = 0; numCars++; if (make.equals("Chevy") || make.equals("GM")) { this.sizeOfTank = 40; } else { this.sizeOfTank = 15; } if (make.equals("Chevy") && model.equals("Malibu")) { this.gasMileage = 4; } else { this.gasMileage = 28; } this.galsRemaining = this.sizeOfTank / 2; } public String getMake() { return this.make; } String getModel() { return this.model; } void setGalsRemaining(double newGals) { this.galsRemaining = newGals; } int getYearsOld() { return 2025 - this.year; } void fillUp() { this.galsRemaining = this.sizeOfTank; } double getTankPerc() { return this.galsRemaining / this.sizeOfTank * 100; } void drive(double miles) throws Exception { double gallonsUsed = miles / this.gasMileage; if (gallonsUsed > this.galsRemaining) { throw new Exception("NOT ENOUGH GAS!!!"); } this.odo = this.odo + (int) miles; this.galsRemaining = this.galsRemaining - gallonsUsed; } public boolean equals(Car c) { if (this.make.equals(c.make) && this.model.equals(c.model)) { return true; } else { return false; } } public String toString() { return "a " + this.year + " " + this.make + " " + this.model + " with " + this.galsRemaining + " gallons in its tank and " + this.odo + " miles on its odometer"; } }