Last active
December 31, 2015 13:09
-
-
Save RavuAlHemio/7991226 to your computer and use it in GitHub Desktop.
Lösung von Tutorium 05-ods-gleichheit, Beispiel 02-compare
This file contains hidden or 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
import java.util.Arrays; | |
public class Main { | |
public static void main(String[] args) { | |
int N = 10; | |
Wohnung[] wohnungen = new Wohnung[N]; | |
Wohnung klein = new Wohnung(3, 28); | |
Wohnung mittel = new Wohnung(3, 36); | |
Wohnung gross = new Wohnung(5, 84); | |
wohnungen[0] = new Wohnung(klein); | |
wohnungen[1] = new Wohnung(klein); | |
wohnungen[2] = new Wohnung(klein); | |
wohnungen[3] = new Wohnung(klein); | |
wohnungen[4] = new Wohnung(klein); | |
wohnungen[5] = new Wohnung(mittel); | |
wohnungen[6] = new Wohnung(mittel); | |
wohnungen[7] = new Wohnung(mittel); | |
wohnungen[8] = new Wohnung(mittel); | |
wohnungen[9] = new Wohnung(gross); | |
Wohnung jakobsWohnung = wohnungen[6]; | |
jakobsWohnung.setZimmeranzahl(4); | |
for (int i = 0; i < N; ++i) { | |
if (wohnungen[i].equals(mittel)) { | |
System.out.print("M: "); | |
} else { | |
System.out.print(" "); | |
} | |
if (wohnungen[i] == jakobsWohnung) { | |
System.out.print("Jakob: "); | |
} else { | |
System.out.print(" "); | |
} | |
System.out.println(wohnungen[i]); | |
} | |
} | |
} |
This file contains hidden or 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 Wohnung { | |
private int zimmeranzahl; | |
private int quadratmeter; | |
public Wohnung(int zimmeranzahl, int quadratmeter) { | |
this.zimmeranzahl = zimmeranzahl; | |
this.quadratmeter = quadratmeter; | |
} | |
public Wohnung(Wohnung other) { | |
this.zimmeranzahl = other.zimmeranzahl; | |
this.quadratmeter = other.quadratmeter; | |
} | |
public void setZimmeranzahl(int zimmeranzahl) { | |
this.zimmeranzahl = zimmeranzahl; | |
} | |
public String toString() { | |
return "" + zimmeranzahl + "-Zimmer-Wohnung mit " + quadratmeter + "m²"; | |
} | |
public boolean equals(Object o) { | |
if (this == o) { | |
return true; | |
} | |
if (o instanceof Wohnung) { | |
Wohnung other = (Wohnung)o; | |
return | |
this.zimmeranzahl == other.zimmeranzahl && | |
this.quadratmeter == other.quadratmeter | |
; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment