First of all I am sorry if don't know how to explain my question properly and if the title is not fully correct or not clear enough. This is my first stackoverflow post. I will try to explain my beginner question with example:
I have following Student class:
public class Student {
private String name;
private int matrikelnummer;
public static int STUDENT_COUNT;
public Student(String name, int matrikelnummer){
this.name = name;
this.matrikelnummer = matrikelnummer;
STUDENT_COUNT++;
}
public String toString() {
return "Student Nr.: " +matrikelnummer+" Name: "+name;
}
}
and another class Tutorium:
public class Tutorium {
private int raumNr;
private String tutor;
private String fach;
private Student[] teilnehmer;
public Tutorium(int kapazitaet, int raumNr, String tutor, String fach) {
this.raumNr = raumNr;
this.tutor = tutor;
this.fach = fach;
teilnehmer = new Student[kapazitaet];
}
public String toString() {
if (teilnehmer == null) {
return "Noch keine Teilnehmer";
}
String out = "Tutorium " + fach + " Bei " + tutor + " in " + raumNr + " " + "\n";
return out + --- ; //here i want to print all array element from teilnehmer
}
}
For e.g if have following main:
public class TutoriumsVerwaltung {
public static void main(String[] args) {
//Studenten und Tutorien erzeugen.
Student[] studs = {new Student("Tim", 333333),
new Student("Anna", 444444),
new Student("Lisa", 345987),
new Student("Karl", 336292),
new Student("Elisabeth", 999999)};
Tutorium[] tuts = {new Tutorium(5, 6057, "Tobias", "PPR-J"),
new Tutorium(2, 6057, "Roland", "PPR-C"),
new Tutorium(9, 6051, "Max", "PPR-J")};
//Infos ueber Tutorien und Studenten ausgeben.
System.out.println("Es gibt "+Student.STUDENT_COUNT+" Studenten.");
for (Student student : studs)
System.out.println(student);
for (Tutorium tutorium : tuts)
System.out.println(tutorium);
System.out.println("");
I want toString() method from Tutorium to print in following format:
Tutorium PPR−J bei Tobias in Raum 6057
Teilnehmer:
Student Nr.: 333333 Name: Tim
Student Nr.: 444444 Name: Anna
Student Nr.: 345987 Name: Lisa
I have tried creating getters/setters (maybe not correctly?) but without success. Would someone care explaining me my problem and solution to it.