public class Car2Demo { public static void main(String[] args) { Car2 ford = new Car2("Ford", 20, 10, 500); System.out.println(ford); ford.drive(100); System.out.println("Is empty? " + ford.isEmpty()); System.exit(0); } } class Car { private String name; private double gallons; private double mpg; //miles per gallon public Car(String name, double gallons, double mpg) { //constructor if (gallons < 0) { System.err.println("gallons " + gallons + " can't be negative"); System.exit(1); } if (mpg < 0) { System.err.println("mpg " + mpg + " can't be negative"); System.exit(1); } this.name = name; this.gallons = gallons; this.mpg = mpg; } public String toString() { return name + " has " + gallons + " gallons, gets " + mpg + " mpg, could go " + gallons * mpg + " more miles"; } public boolean isEmpty() { return gallons <= 0; //should never be negative } public void drive(double miles) { //how many miles we want to go //How many miles will we go? double will = Math.min(miles, gallons * mpg); gallons -= will / mpg; } //Siphon gas from the other Car into this one. //g is the number of gallons we want to siphon. public void siphon(Car other, double g) { //How many gallons will we siphon? double will = Math.min(g, other.gallons); gallons += will; other.gallons -= will; } } class Car2 extends Car { private double _price; //can't have field and method with same name public Car2(String name, double gallons, double mpg, double _price) { super(name, gallons, mpg); this._price = _price; } public String toString() { return super.toString() + ", price == " + _price; } public double price() { return _price; } }