## CSci 2101 Data Structures: Lab 10

For this lab you will be working with the Vehicle, Car, Airplane example here.

### Problem 1

Write a program to do the following:
• create an array of vehicles, store at least 5 different vehicles in the array (cars, airplanes, and vehicles).
• write aloop to print out the array using `print` method.
• write a loop to find the number of vehicles in the array that cover 100 miles in less than 1 hour (use the method `time`).
Do not modify Vehicle, Car, or Airplane classes.

### Problem 2: `instanceof` operator

Operator `instanceof` is used to check if an object is an instance of a given class. For instance, this code
``````
Vehicle bike = new Vehicle("Bicycle",1,20.0);
Airplane myplane = new Airplane("Cessna",2,500,5000);
boolean b1 = bike instanceof Airplane;
boolean b2 = myplane instanceof Airplane;
``````
makes b1 = false and b2 = true.
Experiment with `instanceof` operator:
• is a car an instance of a vehicle?
• is a car an instance of an airplane?
• is a car an instance of an object?
When can you use this operator?

Write your obsevations on the Wiki page.

### Problem 3: `instanceof` operator

Write a method getMaxAltitude in the airplane class. Test it to make sure it compiles and runs. Then write a loop in the testing program that goes through the array of vehicles, checks (using `instanceof` operator) if the vehicle is an airplane, and if it is, prints its maximum altitude.

### Problem 4: generic programming

For this problem see examples here
• Change the Vehicle class so that it implements Comparable interface, where vehicles are compared by max speed. I.e. your class has to be defined like this:
``````
public class Vehicle implements Comparable {
// keep the old code

public int compareTo(Object o) {
Vehicle v = (Vehicle) o;
return (int) (maxSpeed - v.maxSpeed);
}
}
``````
Ignore the warning
```Note: Test.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
```
when you compile the program.

In your testing program write a method that takes an array of Comparable objects and another Comparable and returns the number of elements in the array that are smaller than the object.

Test it on parameters of type Vehicle (i.e. pass an array of vehicles and another vehicle as an input) and on parameters of type String (pass an array of strings and one more string).

This is a lab from CSci 2101 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.