Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The top part is finished : Assignment . The first part answer was excellent. On the bottom is the Extend the lab assignment N- 35.

The top part is finished: Assignment. The first part answer was excellent. On the bottom is the Extend the lab assignment N-35. And answer to the first part is also on the bottom Java, netBean, 8.1 or 8.2 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();

}

This is the part I need to finish, extension of the first part

Extend the lab assignment N-35.

In class Vehicle:

Make name private (not protected).

Add setter setName.

Add getter getName.

Add private variable seats (int) with setter and getter.

Add private variable wheels (int) with setter and getter.

Add class Motorcycle (similar to Car).

Add class Truck.

Add setter for pickup(int seats, int wheels.

Add setter for flatbed(int seats, int wheels).

Add setter for box(int seats, int wheels, int boxsize).

From the run menu, build the project.

Open a cmd window.

Use the java command (2nd to last) (cut/paste) to run your program from the the command line.

ALT-printscreen to capture and upload, code too.

Sample input:

bus fred 66 car barney 2 5 car bambam 2 2 bus wilma 32 bus pebbles 32 motorcyle dino flatbed MrSlate 2 8 24

Answer to first part

package n_35;

import java.util.ArrayList;

/**

*

* @author kpierc02

*/

public class N_35

{

/**

* @param args the command line arguments

*/

public static void main(String[] args)

{

abstract class Vehicle

{

protected String name;

protected int speed;

}

class Bus extends Vehicle

{

int seats;

public Bus(String name)

{

this.name = name;

}

public int getSeats()

{

return seats;

}

public void setSeats(int seats)

{

this.seats = seats;

}

public int getSpeed()

{

return speed;

}

public void setSpeed(int speed)

{

this.speed = speed;

}

@Override

public String toString()

{

return "Bus : " + name;

}

}

class Car extends Vehicle

{

public Car(String name)

{

this.name = name;

}

public void dragCar()

{

this.speed = 0;

}

@Override

public String toString()

{

return "Car : " + name;

}

}

/*public static void main(String[] args) {*/

String data[] = {"bus fred",

"car barney",

"car bambam",

"bus wilma",

"bus pebbles"};

//Car car = new Car("BMW");

//Bus bus = new Bus("MAN");

ArrayList list = new ArrayList();

// list.add(car);

// list.add(bus);

for (int i = 0; i < data.length; i++)

{

//Parse the string and split into type and names

String str[] = data[i].trim().split(" ");

String type = str[0];

String name = str[1];

Vehicle v;

//Initialize object depending on tht type

if (type.equals("car"))

{

v = new Car(name);

} else {

v = new Bus(name);

}

list.add(v);

}

System.out.println("Vehicles are : ");

for (int i = 0; i < list.size(); i++)

{

System.out.println(list.get(i));

}

System.out.println("");

for (int i = 0; i < list.size(); i++)

{

Object obj = list.get(i);

if (obj instanceof Bus)

{

Bus bus = (Bus) obj;

bus.setSeats(80);

System.out.println(bus+" seats are "+bus.getSeats());

} else if (obj instanceof Car)

{

Car car = (Car) obj;

car.dragCar();

System.out.println("Speed of "+car+" is "+car.speed);

} else {

Vehicle vehicle = (Vehicle) obj;

System.out.println(vehicle.toString());

}

}

}

}

run:

Vehicles are :

Bus : fred

Car : barney

Car : bambam

Bus : wilma

Bus : pebbles

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_2

Step: 3

blur-text-image_3

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 Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions