Last active
December 17, 2015 03:09
-
-
Save haraldschilly/5541502 to your computer and use it in GitHub Desktop.
.equals() mit hashCode() und JavaDoc in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Das ist eine Klasse für Komplexe Zahlen, | |
* welche die {@link Double} Klasse verwendet. | |
* | |
* <ul> | |
* <li>{@link Complex#add(complexnumbers.Complex) addiert komplexe zahlen}</li> | |
* </ul> | |
* | |
* @author Harald Schilly | |
*/ | |
public class Complex { | |
/** realteil */ | |
private double real; | |
/** imaginärteil */ | |
private double imag; | |
public Complex(double real, double imag) { | |
this.real = real; | |
this.imag = imag; | |
} | |
public Complex(double real) { | |
this(real, 0.0); | |
} | |
public Complex() { this(0,0); } | |
/** | |
* Addiert zu dieser {@link Complex komplexe Zahl} eine andere | |
*. | |
* @param other die andere Zahl | |
* @return gibt eine <b>NEUE</b> komplexe Zahl zurück | |
*/ | |
public Complex add(Complex other) { | |
double r = this.real + other.real; | |
double i = this.imag + other.imag; | |
return new Complex(r, i); | |
} | |
/** | |
* Skaliert diese komplexe Zahl | |
*/ | |
public void scale(double s) { | |
this.real *= s; | |
this.imag *= s; | |
} | |
/** | |
* Der <a href="http://en.wikipedia.org/wiki/Hashcode">Hashcode</a> | |
* eines Objektes muss für zwei Objekte, deren {@link #equals(Object) Vergleichstest} | |
* <code>true</code> ergibt, derselbe sein! | |
*/ | |
@Override | |
public int hashCode() { | |
int hr = Double.valueOf(real).hashCode(); | |
int hi = Double.valueOf(imag).hashCode(); | |
return hr * 31 + hi; | |
} | |
/** | |
* Gleichzeitstest für andere Komplexe Zahlen Objekte und Doubles | |
*/ | |
@Override | |
public boolean equals(Object other) { | |
if (this == other) { | |
return true; | |
} | |
if (other instanceof Complex){ | |
Complex co = (Complex) other; | |
return real == co.real && imag == co.imag; | |
} | |
if (other instanceof Double) { | |
Double d = (Double) other; | |
return d.equals(real) && imag == 0; | |
} | |
return false; | |
} | |
@Override | |
public String toString() { | |
return real + "+" + imag + "i"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ComplexTest { | |
public static void main(String[] args) throws ParseException { | |
Complex c1 = new Complex(1.5, -.5); | |
Complex c2 = new Complex(1.5, -.5); | |
Complex c3 = c1.add(c2); | |
System.out.println("c1: " + c1); | |
System.out.println("c2: " + c2); | |
System.out.println("c1 + c2: " + c3); | |
Complex c4 = new Complex(); | |
System.out.println("c4: " + c4); | |
System.out.println(" == :" + (c1 == c2)); | |
System.out.println("equals: " + c1.equals(c2)); | |
System.out.println("hashcode(c1): " + c1.hashCode()); | |
System.out.println("hashcode(c2): " + c2.hashCode()); | |
Double d = new Double(3); | |
Complex e = new Complex(3, 0); | |
System.out.println("equals: " + e.equals(d)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment