Created
December 16, 2013 18:16
-
-
Save RavuAlHemio/7991661 to your computer and use it in GitHub Desktop.
Lösung von Tutorium 05-ods-gleichheit, Beispiel 03-deep
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 Haus { | |
private Wohnung[] wohnungen; | |
public Haus() { | |
wohnungen = new Wohnung[0]; | |
} | |
public Haus(Haus other) { | |
wohnungen = new Wohnung[other.wohnungen.length]; | |
for (int i = 0; i < wohnungen.length; ++i) { | |
wohnungen[i] = new Wohnung(other.wohnungen[i]); | |
} | |
} | |
public void addWohnung(Wohnung w) { | |
wohnungen = Arrays.copyOf(wohnungen, wohnungen.length + 1); | |
wohnungen[wohnungen.length - 1] = w; | |
} | |
public String toString() { | |
String s = "Haus:\n"; | |
for (int i = 0; wohnungen != null && i < wohnungen.length; ++i) { | |
s += " - " + wohnungen[i] + "\n"; | |
} | |
return s; | |
} | |
public boolean equals(Object o) { | |
if (this == o) { | |
return true; | |
} | |
if (! (o instanceof Haus)) { | |
return false; | |
} | |
Haus other = (Haus)o; | |
return Arrays.equals(this.wohnungen, other.wohnungen); | |
} | |
} |
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) { | |
Wohnung jakobsWohnung; | |
Wohnung klein = new Wohnung(3, 28); | |
Wohnung mittel = new Wohnung(3, 36); | |
Wohnung gross = new Wohnung(5, 84); | |
Haus favoritenstr = new Haus(); | |
favoritenstr.addWohnung(new Wohnung(klein)); | |
favoritenstr.addWohnung(new Wohnung(klein)); | |
favoritenstr.addWohnung(new Wohnung(klein)); | |
favoritenstr.addWohnung(new Wohnung(klein)); | |
favoritenstr.addWohnung(new Wohnung(klein)); | |
favoritenstr.addWohnung(new Wohnung(mittel)); | |
favoritenstr.addWohnung(jakobsWohnung = new Wohnung(mittel)); | |
favoritenstr.addWohnung(new Wohnung(mittel)); | |
favoritenstr.addWohnung(new Wohnung(mittel)); | |
favoritenstr.addWohnung(new Wohnung(gross)); | |
Haus gusshausstr = new Haus(favoritenstr); | |
System.out.println("Favoritenstraße XX" | |
+ (favoritenstr.equals(gusshausstr) ? " == " : " != ") | |
+ "Gußhausstraße YY"); | |
jakobsWohnung.setZimmeranzahl(4); | |
System.out.print("Favoritenstraße "); | |
System.out.println(favoritenstr); | |
System.out.print("Gußhausstraße "); | |
System.out.println(gusshausstr); | |
System.out.println("Favoritenstraße XX" | |
+ (favoritenstr.equals(gusshausstr) ? " == " : " != ") | |
+ "Gußhausstraße YY"); | |
} | |
} |
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 (other.zimmeranzahl == this.zimmeranzahl | |
&& other.quadratmeter == this.quadratmeter); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment