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 list = new ArrayList(); list.add(car); list.add(bus); } }

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!