print
method.
time
).
instanceof
operatorinstanceof
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. instanceof
operator:
Write your obsevations on the Wiki page.
instanceof
operatorinstanceof
operator) if the vehicle is an airplane, and
if it is, prints its maximum altitude.
public class Vehicle implements Comparable {
// keep the old code
// add compareTo method:
public int compareTo(Object o) {
Vehicle v = (Vehicle) o;
return (int) (maxSpeed - v.maxSpeed);
}
}
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).