class Vehicle { protected int mileage; protected double milesPerGallon; protected String color; protected Vehicle() { mileage = 0; milesPerGallon = 20; color = new String(); } protected Vehicle(int mileage, double milesPerGallon, String color) { this.mileage = mileage; this.milesPerGallon = milesPerGallon; this.color = new String(color); } public String getColor() { return color; } public int getMileage() { return mileage; } public double getMilesPerGallon() { return milesPerGallon; } public void setColor(String color) { this.color = new String(color); } public void setMileage(int mileage) { this.mileage = mileage; } public void setMilesPerGallon(double milesPerGallon) { this.milesPerGallon = milesPerGallon; } public String toString() { return "Color is " + color + "\n" + "Mileage is " + mileage + "\n" + "Miles Per Gallon is " + milesPerGallon + "\n"; } } class Car extends Vehicle { private int numberOfPassengers; public Car() { super(); numberOfPassengers = 4; } public Car(int numberOfPassengers, int mileage, double mpg, String color) { super(mileage, mpg, color); this.numberOfPassengers = numberOfPassengers; } public String toString() { return super.toString() + "Number of Passengers is " + numberOfPassengers + "\n"; } public int getNumberOfPassengers(){ return numberOfPassengers; } public void setNumberOfPassengers(int n){ numberOfPassengers = n; } } class Truck extends Vehicle { private double weightCapacity; public Truck() { super(); weightCapacity = 1000.00D; } public Truck(double weightCapacity, int mileage, double mpg, String color) { super(mileage, mpg, color); this.weightCapacity = weightCapacity; } public double getWeightCapacity() { return weightCapacity; } public void setWeightCapacity(double w) { weightCapacity = w; } public String toString() { return super.toString() + "Weight Capacity is " + weightCapacity + "\n"; } } class ParkingGarage { public Vehicle spaces[]; private int spacesFilled; public ParkingGarage() { spaces = new Vehicle[100]; spacesFilled = 0; } public ParkingGarage(int size) { spaces = new Vehicle[size]; spacesFilled = 0; } public ParkingGarage(Vehicle s[], int sf) { spacesFilled = sf; spaces = s; } public int findVehicle(Vehicle vehicle) { for(int i = 0; i < spacesFilled; i++) { if ( spaces[i] == null ) { continue; } else { if ( (spaces[i] instanceof Car) && (vehicle instanceof Car) ) { Car c1 = (Car) spaces[i]; Car c2 = (Car) vehicle; if (c1.getMileage() == c2.getMileage() && c1.getMilesPerGallon() == c2.getMilesPerGallon() && c1.getColor().equalsIgnoreCase( c2.getColor() ) && c1.getNumberOfPassengers() == c2.getNumberOfPassengers()) return i; } else if ( (spaces[i] instanceof Truck) && (vehicle instanceof Truck) ) { Truck t1 = (Truck) spaces[i]; Truck t2 = (Truck) vehicle; if (t1.getMileage() == t2.getMileage() && t1.getMilesPerGallon() == t2.getMilesPerGallon() && t1.getColor().equalsIgnoreCase( t2.getColor() ) && t1.getWeightCapacity() == t2.getWeightCapacity()) return i; } } } return -1; // Not Found! } public boolean parkVehicle(Vehicle vehicle) { if(spaces.length <= spacesFilled) { System.out.println("No space left!"); return false; } spaces[spacesFilled++] = vehicle; return true; } } class Driver { public static void main(String [] args){ Car c1 = new Car(4,1000,30,"blue"); System.out.println("Car 1 is created "+c1); Car c2 = new Car(4,1000,30,"blue"); System.out.println("Car 2 is created "+c2); Car c3 = new Car(3,10000,25,"white"); System.out.println("Car 3 is created "+c3); Truck t1 = new Truck(2000,25000, 15, "yellow"); System.out.println("Truck 1 is created "+t1); Truck t2 = new Truck(5000,45000, 10, "purple"); System.out.println("Truck 2 is created "+t2); Truck t3 = new Truck(20000,95000, 7, "lily"); System.out.println("Truck 3 is created "+t3); ParkingGarage pg = new ParkingGarage(4); System.out.println("car 1 is parking "+pg.parkVehicle(c1)); System.out.println("truck 1 is parking "+pg.parkVehicle(t1)); System.out.println("car 2 is parking "+pg.parkVehicle(c2)); System.out.println("truck 1 is parking "+pg.parkVehicle(t2)); System.out.println("truck 3 is parking "+pg.parkVehicle(t3)); System.out.println("Is car 1 in the parking garage? "+pg.findVehicle(c1)); System.out.println("Is truck 1 in the parking garage? "+pg.findVehicle(t1)); System.out.println("Is truck 3 in the parking garage? "+pg.findVehicle(t3)); } }