hw7
Class Vehicle

java.lang.Object
  |
  +--hw7.Vehicle
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Comparable
Direct Known Subclasses:
LocatedVehicle

public class Vehicle
extends java.lang.Object
implements java.lang.Comparable, java.lang.Cloneable

This class provides an implementation for a basic vehicle that has a vehicle identification number and the ability to sort based on that number. As implemented here, the class is immutable. That is, once an object is created, the vin cannot be changed. However, the vin is declared to be protected, rather than private, so if you need a mutable Vehicle (as search key for a Map for example) you can extend this class and define a setVIN method there.


Field Summary
protected  int vin
          the vehicle identification number
 
Constructor Summary
Vehicle(int id)
          Create a new Vehicle with the given vehicle identification number.
 
Method Summary
 java.lang.Object clone()
          Create a copy of this Vehicle object.
 int compareTo(java.lang.Object o)
          Compares this object with the specified object for order, based on the vehicle identification number.
 boolean equals(java.lang.Object obj)
          Indicates whether some other object is "equal to" this one, based on a comparison of vehicle id numbers.
 int getVIN()
          Return the vehicle id number for this Vehicle.
 int hashCode()
          Returns a hash code value for this Vehicle, namely the vehicle identification number.
static void main(java.lang.String[] arg)
          Test harness for this class.
 java.lang.String toString()
          Provide a String representation of this Vehicle.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

vin

protected int vin
the vehicle identification number
Constructor Detail

Vehicle

public Vehicle(int id)
Create a new Vehicle with the given vehicle identification number.
Parameters:
vin - the vehicle identification number
Method Detail

getVIN

public int getVIN()
Return the vehicle id number for this Vehicle.
Returns:
the vehicle id number

toString

public java.lang.String toString()
Provide a String representation of this Vehicle.
Overrides:
toString in class java.lang.Object
Returns:
a String describing this Vehicle

compareTo

public int compareTo(java.lang.Object o)
Compares this object with the specified object for order, based on the vehicle identification number.
Specified by:
compareTo in interface java.lang.Comparable
Parameters:
o - the other Vehicle object
Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

equals

public boolean equals(java.lang.Object obj)
Indicates whether some other object is "equal to" this one, based on a comparison of vehicle id numbers. This is implemented to remain consistent with compareTo.
Overrides:
equals in class java.lang.Object
Parameters:
obj - the other Vehicle object
Returns:
true if equal, false if not

hashCode

public int hashCode()
Returns a hash code value for this Vehicle, namely the vehicle identification number. This is implemented to remain consistent with equals(), because if two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
Overrides:
hashCode in class java.lang.Object
Returns:
a hash code value for this object

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Create a copy of this Vehicle object. A shallow copy is okay here, because the only instance variable is a primitive type.
Overrides:
clone in class java.lang.Object
Returns:
a clone of this Vehicle instance
Throws:
java.lang.CloneNotSupportedException - but this should never happen

main

public static void main(java.lang.String[] arg)
Test harness for this class.