Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke, David Auer

11th Edition

B003Y7CIBU, 978-0132302678

More Books

Students also viewed these Databases questions

Question

What do Dimensions represent in OLAP Cubes?

Answered: 1 week ago