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: 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: 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 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.