/**
   Klasse Auto (11)
   @author Benedikt Großer, Holger Arndt
   @version 14.04.2003
*/
public class Auto implements Cloneable
{
  // Klassenttribute
  static int anzahl = 0;

  // Objektattribute
  double tachoStand;
  
  //Konstruktoren
  public Auto()
  {
    this.tachoStand = 100;
    anzahl = anzahl + 1;
  }

  public Auto(double tachoStand)
  {
    if (tachoStand >= 0)
      this.tachoStand = tachoStand;
    else
      this.tachoStand = 100;
    anzahl = anzahl + 1;
  }

  // Destruktor
  protected void finalize() { anzahl = anzahl - 1; }

  // Klassenmethoden
  public static int anzahlAutos() { return anzahl; }

  // Objektmethoden
  public void fahren(double distanz) { tachoStand += distanz; }
 
  public double leseTachoStand() { return tachoStand; }

  public void ausgeben()
  { System.out.println("Tachostand = " + tachoStand + " km"); }
  
  public void abschleppen(Auto auto, double distanz)
  {
    this.fahren(distanz);
    auto.fahren(distanz);
  }

 // ueberlagert die Methode equals() von Object
  public boolean equals(Object obj)
  {
    if (obj != null && obj instanceof Auto)
      return this.tachoStand == ((Auto)obj).tachoStand;
    return false;
  }

  // ueberlagert die Methode clone() von Object
  public Object clone()
  {
    return new Auto(this.tachoStand);
  }

  // ueberlagert die Methode toString() von Object
  public String toString()
  {
    return "Instanz von Auto, tachoStand = " + tachoStand;
  }

  // Methode main zum Testen
  public static void main(String argv[])
  {
    Auto meinPKW = new Auto();
    Auto bmw = new Auto();
    Auto zweiCV;

    // Identitaet und Aequivalenz mit == und equals()
    meinPKW.fahren(100);
    bmw.fahren(100);
    System.out.println(bmw == meinPKW);      // false
    System.out.println(bmw.equals(meinPKW)); // true
    bmw.fahren(100);
    System.out.println(bmw.equals(meinPKW)); // false
    System.out.println(bmw.equals("Hallo")); // false
    zweiCV = (Auto)bmw.clone();              // clone erfordert Cast!
    System.out.println(bmw==zweiCV);         // false
    System.out.println(bmw.equals(zweiCV));  // true
    zweiCV = null;
    System.out.println(bmw.equals(zweiCV));  // false

    // Test toString()
    System.out.println(bmw);             // Instanz von Auto, tachoStand = 300.0
    System.out.println(new Object());       // gibt z.B. java.lang.Object@df6ccd
  }
} // ende: public class Auto
