Commit bf41c9af authored by matthias's avatar matthias
Browse files

added Übung "Lok_Waggon" in OOP3_4

parent a67a4467
package lok_waggon;
public class Lok {
private int lokNummer;
private int zugKraft = 60_000;
public Wagon naechsterWagon;
public Lok(int lokNummer) {
this.lokNummer = lokNummer;
}
@Override
public Lok clone() {
Lok lok = new Lok(lokNummer);
if (naechsterWagon != null) {
lok.naechsterWagon = naechsterWagon.clone();
}
return lok;
}
@Override
public String toString() {
return "Lok-Nummer: " + lokNummer;
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof Wagon )){
return false;
}
Lok lok = (Lok) obj;
if (lokNummer != lokNummer) {
return false;
} else return true;
}
}
package lok_waggon;
public class Programm {
final static int WAGONS = 5;
public static void main(String[] args) {
Lok lok1 = new Lok(1);
lok1.naechsterWagon = new Wagon(1, 500, 1000);
Wagon wagon = lok1.naechsterWagon;
for (int i = 2; i <= WAGONS; i++) {
wagon.naechsterWagon = new Wagon(i, 500, 1000);
wagon = wagon.naechsterWagon;
}
printZug(lok1);
Lok lok2 = lok1.clone();
printZug(lok2);
wagon = lok1.naechsterWagon;
while (wagon.getWagonNummer() != 4) {
wagon = wagon.naechsterWagon;
}
wagon.naechsterWagon = null;
printZug(lok1);
printZug(lok2);
System.out.println("Vergleich von Lok#1 und Lok#2 ist: " + lok1.equals(lok2));
Lok lok3 = lok1.clone();
System.out.println("Vergleich von Lok#3 und Lok#1 ist: " + lok3.equals(lok1));
}
private static void printZug(Lok lok) {
System.out.print(lok + " <=> ");
Wagon wagon = lok.naechsterWagon;
while (wagon != null) {
System.out.print(wagon + " <=> ");
wagon = wagon.naechsterWagon;
}
System.out.println();
}
}
package lok_waggon;
public class Wagon implements Cloneable {
private int wagonNummer;
private int maximalLast = 5000;
private int beladung;
public Wagon naechsterWagon;
public int getWagonNummer() {
return wagonNummer;
}
public Wagon(int wagonNummer, int maximalLast, int beladung) {
this.wagonNummer = wagonNummer;
this.maximalLast = maximalLast;
this.beladung = beladung;
naechsterWagon = null;
}
@Override
public Wagon clone() {
Wagon wagon = new Wagon(wagonNummer, maximalLast, beladung);
if (naechsterWagon != null) {
wagon.naechsterWagon = wagon.clone();
}
return naechsterWagon;
}
@Override
public String toString() {
return "Wagon-Nummer: " + wagonNummer;
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof Wagon)) {
return false;
}
Wagon wagon = (Wagon) obj;
if (wagonNummer != wagonNummer) {
return false;
} else return true;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment