Java-Klasse Student: Attribute, Konstruktoren und Methoden erklärt
Java-Beispiel: Klasse Student
Das folgende Beispiel zeigt eine Java-Klasse Student mit Attributen, Konstruktoren, Getter- und Setter-Methoden sowie Methoden zur Berechnung des Notendurchschnitts und des Status.
Klasse Test
Die Klasse Test erstellt ein Objekt der Klasse Student und ruft die Methode Mostrar() auf:
import java.io.*;
public class Test {
static void main() throws IOException {
Student A1;
A1 = new Student("John", true, 3, 2, 5, 43);
A1.Mostrar();
}
}Klasse Student
Die Klasse Student enthält private Attribute für Noten, Name, Alter und Geschlecht.
import java.io.*;
public class Student {
private int nota1, nota2, nota3;
private String name;
private int edad;
private boolean sexo; // Attribute
public Student() { // Standardkonstruktor
this.name = "NN";
this.sexo = false;
this.nota1 = 0;
this.nota2 = 0;
this.nota3 = 0;
this.edad = 2;
}
public Student(String nom, boolean sexo, int n1, int n2, int n3, int eda) { // Konstruktor
this.name = nom;
this.sexo = sexo;
this.nota1 = n1;
this.nota2 = n2;
this.nota3 = n3;
this.edad = eda;
}
public String getName() { return this.name; } // Rückgabe des Namens
public boolean getSexo() { return this.sexo; }
public int getNota1() { return this.nota1; }
public int getNota2() { return this.nota2; }
public int getNota3() { return this.nota3; }
public int getEdad() { return this.edad; } // Zugriffsmethoden
public void setName(String nom) { this.name = nom; }
public void setSexo(boolean sexo) { this.sexo = sexo; }
public void setNota1(int n1) { this.nota1 = n1; }
public void setNota2(int n2) { this.nota2 = n2; }
public void setNota3(int n3) { this.nota3 = n3; }
public void setEdad(int eda) { this.edad = eda; }
// Methoden
public float promedio() {
float pro;
pro = (float) (this.nota3 + this.nota2 + this.nota1) / 3;
return pro;
}
public int mayor() {
int m;
m = 0;
if (this.nota1 > m) m = this.nota1;
if (this.nota2 > m) m = this.nota2;
if (this.nota3 > m) m = this.nota3;
return m; // Pseudocode-ähnlich
}
public void situacion() {
float p = this.promedio(); // Aufruf der Methode promedio
if (p >= 5) {
System.out.println("aprobado");
} else if (p < 3) {
System.out.println("suspenso");
} else {
System.out.println("examen final"); // Prüfungslage
}
}
public String queSexo() {
String sx;
if (this.sexo == true) {
sx = "männlich";
} else {
sx = "weiblich";
}
return sx;
} // Ende Methode queSexo
public void mostrar() {
System.out.println("NAME: " + this.name);
System.out.println("ALTER: " + this.edad);
System.out.println("GESCHLECHT: " + this.queSexo()); // Rückgabe
System.out.println("Durchschnitt: " + this.promedio());
System.out.println("Status:");
this.situacion();
} // Ende Methode mostrar
}Hinweise zur Korrektur
- Groß- und Kleinschreibung der Methoden wurde vereinheitlicht (z. B.
mostrar()). - Die Schreibweise
this.sexowurde konsistent verwendet. - Fehlende Semikolons und Klammern wurden ergänzt.
- Die Methode
getNota2()greift nun korrekt aufnota2zu.