Skip to content

Instantly share code, notes, and snippets.

@haraldschilly
Last active December 17, 2015 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haraldschilly/5584669 to your computer and use it in GitHub Desktop.
Save haraldschilly/5584669 to your computer and use it in GitHub Desktop.
Demo Data Structures for Students & Exams

Data Structures Demo in JAVA

Student

Prüfung

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DataStructures {
static List<Student> studenten = new ArrayList<Student>();
static Map<Student, Prüfung> prüfung1 = new HashMap<Student, Prüfung>();
public static void main(String[] args) {
Student s1 = new Student("12345");
s1.setName("Hubert");
System.out.println(s1);
Student s2 = new Student("828282");
s2.setName("Susi");
System.out.println(s2);
studenten.add(s1);
studenten.add(s2);
System.out.println("Studenten: " + studenten);
Prüfung p1 = new Prüfung(10);
Prüfung.Beispiel p1b3 = p1.getBeispiel(3);
p1b3.setPunkte(3);
System.out.println(p1);
System.out.println("summe: " + p1.summe());
Prüfung p2 = new Prüfung(10);
Prüfung.Beispiel p2b4 = p2.getBeispiel(4);
p2.getBeispiel(6).setPunkte(2);
p2b4.setPunkte(4);
System.out.println(p2);
System.out.println("summe: " + p2.summe());
prüfung1.put(s1, p1);
prüfung1.put(s2, p2);
System.out.println("Prüfung1: " + prüfung1);
System.out.println("Ergebnis von " + s2 + " ist " + prüfung1.get(s2));
}
}
public class Prüfung {
private final Beispiel[] ergebnisse;
static class Beispiel {
int punkte;
void setPunkte(int punkte) {
this.punkte = punkte;
}
public String toString() {
return Integer.toString(punkte);
}
}
Beispiel getBeispiel(int i) {
return ergebnisse[i];
}
public Prüfung(int anzBsp) {
this.ergebnisse = new Beispiel[anzBsp];
for (int i = 0; i < anzBsp; i++) {
this.ergebnisse[i] = new Beispiel();
}
}
public int summe() {
int s = 0;
for (Beispiel b : ergebnisse) {
s += b.punkte;
}
return s;
}
public String toString() {
return String.format("%s Punkte", summe());
}
}
public class Student {
private String matrnr;
private String name;
public Student(String mtrnr) {
this.matrnr = mtrnr;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return String.format("%s [%s]", name, matrnr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment