import java.io.PrintWriter; import java.util.Scanner; class Car { int year; String make, model; String review; int odo; double galsRemaining; double sizeOfTank; double gasMileage; static int num = 0; Car(String make, String model) { this.make = make; this.model = model; this.odo = 0; this.year = 2024; if (this.make.equals("Chevy")) { this.sizeOfTank = 50; } else { this.sizeOfTank = 12; } if (this.make.equals("Chevy") && this.model.equals("Monte Carlo")) { this.gasMileage = 3; } else { this.gasMileage = 28; } this.galsRemaining = this.sizeOfTank; num++; } void persist(PrintWriter pw) { pw.println(this.make); pw.println(this.model); pw.println(this.odo); pw.println(this.review); pw.println("END REV"); pw.println(this.year); pw.println(this.galsRemaining); pw.println(this.sizeOfTank); pw.println(this.gasMileage); pw.println("That was a car!"); } Car(Scanner s) throws Exception { this.make = s.nextLine(); if (this.make.equals("That's all the cars!")) { throw new Exception(); } this.model = s.nextLine(); this.odo = Integer.parseInt(s.nextLine()); this.review = ""; String temp = ""; temp = s.nextLine(); while (!temp.equals("END REV")) { this.review += temp + "\n"; temp = s.nextLine(); } this.year = Integer.parseInt(s.nextLine()); this.galsRemaining = Double.parseDouble(s.nextLine()); this.sizeOfTank = Double.parseDouble(s.nextLine()); this.gasMileage = Double.parseDouble(s.nextLine()); s.nextLine(); // throw away dorky message } String getMake() { return this.make; } String getModel() { return this.model; } int getYearsOld() { return 2024 - this.year; } void setYear(int year) { this.year = year; } void drive(int numMiles) throws Exception { double gallonsUsed = numMiles / this.gasMileage; if (gallonsUsed > this.galsRemaining) { throw new Exception(); } this.odo += numMiles; this.galsRemaining = this.galsRemaining - gallonsUsed; } double getTankPerc() { return this.galsRemaining / this.sizeOfTank * 100; } void fillUp() { this.galsRemaining = this.sizeOfTank; } public String toString() { return "a " + this.year + " " + this.make + " " + this.model + " (which has a " + this.getTankPerc() + " % full tank)"; } public static void main(String args[]) { Car stephensCrummyCar = new Car("Honda","CRV"); stephensCrummyCar.setYear(2008); Car raesCrummyCar = stephensCrummyCar; raesCrummyCar.fillUp(); try { stephensCrummyCar.drive(3000); } catch (Exception e) { e.printStackTrace(); } System.out.println("Stephen drives " + stephensCrummyCar); } }