Quellcode durchsuchen

(feat) Implement Exercise 3 - Student and Lecturer child classes

main
Rob Hallam vor 1 Jahr
Ursprung
Commit
22e9c99217
1 geänderte Dateien mit 52 neuen und 0 gelöschten Zeilen
  1. +52
    -0
      week23.java

+ 52
- 0
week23.java Datei anzeigen

@@ -163,4 +163,56 @@ class DemoBankAccount {
}
}

class Student extends Person {
private double gpa;

public double getGpa() {
return gpa;
}

public void setGpa(double gpa) {
this.gpa = gpa;
}

@Override
public String toString() {
return "Student [gpa=" + gpa + ", name=" + this.getName() + "]";
// Ah, we can't access private members of parent classes from the child class
}
}

class Lecturer extends Person {
private double salary;

public double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}

@Override
public String toString() {
return "Lecturer [salary=" + salary + ", name=" + this.getName() + "]";
}
}

/**
* Exercise 3.1, 3.2, 3.3 - implement Student and Lcturer classes
*
*/
class DemoStudentLecturer {
public static void main(String[] args) {
Student adrian = new Student();
adrian.setName("Adrian");
adrian.setGpa(3.5);

Lecturer trefusis = new Lecturer();
trefusis.setName("Donald");
trefusis.setSalary(24199);

System.out.println(String.format("At a particular Oxbridge college, we have two people"));
System.out.println(String.format("%s\t\t&\t\t%s", adrian, trefusis));
}
}

Laden…
Abbrechen
Speichern