Question: Using the Java code for Vehicle.java, Car.java and Airplane.java create a tester, VehicleTester.java, that creates an ArrayList of 7 Vehicles. Make some of them Vehicles,
Using the Java code for Vehicle.java, Car.java and Airplane.java create a tester, VehicleTester.java, that creates an ArrayList of 7 Vehicles. Make some of them Vehicles, and some of them Cars, and some of them Airplanes. Simple hard-coding is fine. In the tester, use polymorphism to call the printInfo for all of the vehicles in the ArrayList.
Vehicle.java import java.util.*; public class Vehicle { public String name; public int price; public double currentSpeed; public Vehicle(String name_, int price_) { this.name=name_; this.price=price_; this.currentSpeed=0.0; } public String getName() { return this.name; } public void setName(String name_) { this.name=name_; } public int getPrice() { return this.price; } public void setPrice(int price_) { this.price=price_; } public double getCurrentSpeed() { return this.currentSpeed; } public void accelerate(double acceleration) { this.currentSpeed=currentSpeed+acceleration; } public void printInfo() { System.out.println("Name: "+getName()); System.out.println("Price: $"+getPrice()); System.out.println("Current Speed; "+getCurrentSpeed()); } }
Car.java import java.util.*; public final class Car extends Vehicle { private String make; private String model; public Car( String name_, String make_, String model_, int price_) { super(name_, price_); this.make=make_; this.model=model_; } public String getMake() { return this.make; } public void setMake(String make_) { this.make=make_; } public String getModel() { return this.model; } public void setModel(String model_) { this.model=model_; } public void printInfo() { System.out.println("Name: "+getName()); System.out.println("Make: "+getMake()); System.out.println("Model; "+getModel()); System.out.println("Price: $"+getPrice()); System.out.println("Current Speed; "+getCurrentSpeed()); } }
Airplane.java import java.util.*; public final class Airplane extends Vehicle { private double currentAltitude; public Airplane(String name_, int price_, double currentAltitude_) { super(name_, price_); this.currentAltitude=currentAltitude_; } public void climb(double height) { this.currentAltitude=currentAltitude+height; } public void printInfo() { System.out.println("Name: "+getName()); System.out.println("Price: $"+getPrice()); System.out.println("Current altitude; "+currentAltitude); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
