Question
java 8.2 netbean please Java allows you to have an ArrayList that contains different objects. Assignment: 1. Get the Vehicle/Bus/Car example working. 2. Add the
java 8.2 netbean please
Java allows you to have an ArrayList that contains different objects.
Assignment:
1. Get the Vehicle/Bus/Car example working. 2. Add the interrogation code down at the bottom to the N-35 main class. 3. Add toString() methods to Bus/Car classes. Call the toString methods from the interrogation code. 4. Loop through the entire ArrayList and print the names of the vehicle. 5. Add the following data from a pre-initialized array of strings (use split to separate the words and add name as name of the appropriate vehicle:
bus fred car barney car bambam bus wilma bus pebbles
public abstract class Vehicle { protected String name; }
public class Bus extends Vehicle { public Bus(String name) { this.name=name; } }
public class Car extends Vehicle { public Car(String name) { this.name=name; } }
public class Main { public static void main(String[] args) { Car car = new Car("BMW"); Bus bus = new Bus("MAN"); ArrayList
And you can retrieve objects from that ArrayList and interrogate them like:
Object obj = list.get(1); if(obj instanceof Bus) { Bus bus = (Bus) obj; bus.busMethod(); } else if(obj instanceof Car) { Car car = (Car) obj; car.carMethod(); } else { Vehicle vehicle = (Vehicle) obj; vehicle.vehicleMethod(); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started