Last active
August 29, 2015 13:59
-
-
Save haraldschilly/10636907 to your computer and use it in GitHub Desktop.
Vorlesung und Studierende als Einführungsbeispiel für OOP
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
package oop; | |
import java.util.Arrays; | |
/** | |
* Beispielprogramm zur Einführung in Objektorientiertes Programmieren (OOP) | |
* | |
* 2014-04-14 | |
* | |
* @author Harald Schilly | |
*/ | |
public class Objektorientierung { | |
public static void main(String... args) { | |
Studierender s1 = new Studierender("Alex"); | |
Studierender s2 = new Studierender("Susi"); | |
Studierender s3 = new Studierender("Max"); | |
Studierender[] studenten = new Studierender[] {s1, s2, s3}; | |
Vortragender vortragender = new Vortragender("Prof. Dr. Javaprogimus"); | |
Vorlesung pp = new Vorlesung("Programmierpraktikum", studenten, vortragender); | |
System.out.println(pp); | |
System.out.println("Mit Studierenden: " + | |
Arrays.toString(pp.getStudierende())); | |
System.out.println("Unter der Leitung von " + pp.getVortragenden() + "."); | |
s1.setNote(1); | |
s2.setNote(3); | |
s3.setNote(3); | |
System.out.printf("Notendurchschnitt ist %.2f.\n", pp.notendurchschnit()); | |
System.out.printf("Ist %s besser als %s? => %s\n", s1, s2, s1.besser(s2)); | |
} | |
} |
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
package oop; | |
/** | |
* Basisklasse für {@link Studierender} & co. | |
* | |
* @author Harald Schilly | |
*/ | |
public class Person { | |
protected String name; | |
public Person(String name) { | |
this.name = name; | |
} | |
@Override | |
public String toString() { | |
return name; | |
} | |
} |
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
package oop; | |
/** | |
* Ein Studierender einer Vorlesung. | |
* | |
* @author Harald Schilly | |
*/ | |
public class Studierender extends Person { | |
private String mtrlnr; | |
private int note = 5; | |
public void setMatrikelnummer(String mtrlnr) { | |
// TODO: teste, ob mtrnr korrekt ist | |
this.mtrlnr = mtrlnr; | |
} | |
public int getNote() {return this.note; } | |
public void setNote(int note) { | |
if (note <= 0 || note > 5) { | |
new IllegalArgumentException("note zwischen 1 und 5"); | |
} | |
this.note = note; | |
} | |
boolean besser(Studierender anderer) { | |
return this.note < anderer.note; | |
} | |
public Studierender(String name) { | |
super(name); | |
} | |
@Override | |
public String toString() { | |
return "\"" + name + "\""; | |
} | |
} |
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
package oop; | |
/** | |
* Eine Vorlesung: Hat einen Titel, Studierende und einen Vortragenden. | |
* | |
* @author Harald Schilly | |
*/ | |
public class Vorlesung { | |
private Studierender[] studierende; | |
private Vortragender vortragender; | |
private String titel; | |
public Vorlesung(String titel, | |
Studierender[] studenten, | |
Vortragender vortragender) { | |
this.titel = titel; | |
this.studierende = studenten; | |
this.vortragender = vortragender; | |
} | |
public double notendurchschnit() { | |
int sum = 0; | |
for(Studierender s : studierende) { | |
sum += s.getNote(); | |
} | |
double d = (double) sum / studierende.length; | |
return d; | |
} | |
@Override | |
public String toString() { | |
return "VO \"" + titel + "\""; | |
} | |
public Studierender[] getStudierende() { | |
return studierende; | |
} | |
Vortragender getVortragenden() { | |
return vortragender; | |
} | |
} |
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
package oop; | |
/** | |
* Ein Vortragender einer {@link Vorlesung} | |
* | |
* @author Harald Schilly | |
*/ | |
class Vortragender extends Person { | |
public Vortragender(String name) { | |
super(name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment