Advertisement
timdebuyst

Student.java

Mar 7th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. package hfdst3;
  2.  
  3. interface CallBackInterface { // added
  4.     char getGrade();
  5.     int getEncryptedNaam();
  6. }
  7.  
  8. public class Student {
  9.     protected String naam;
  10.     protected int score;
  11.     public Student(String naam, int score){
  12.         this.naam = naam;
  13.         this.score = score;
  14.     }
  15.     public String getNaam(){
  16.         return naam;
  17.     }
  18.     public int getScore(){
  19.         return score;
  20.     }
  21.     /*
  22.     public char getGrade(){
  23.         return score >= 12 ? 'A' : score < 10 ? 'C' : 'B';
  24.     }
  25.     public int getEncryptedNaam(){
  26.         int e = 0;
  27.         for(int i=0;i<naam.length();i++){
  28.             e += (byte)(naam.charAt(i))*10;
  29.         }
  30.         return e;
  31.     }
  32.     */
  33.     // // // //
  34.     public CallBackInterface getCallBackReferentie() {
  35.         return new CallBackInterface() {
  36.             public char getGrade() {
  37.                 return score >= 12 ? 'A' : score < 10 ? 'C' : 'B';
  38.             }
  39.             public int getEncryptedNaam(){
  40.                 int e = 0;
  41.                 for(int i=0;i<naam.length();i++){
  42.                     e += (byte)(naam.charAt(i))*10;
  43.                 }
  44.                 return e;
  45.             }
  46.         };
  47.     }
  48. }
  49.  
  50. class ScoreLijst { // Student -> CallBackInterface
  51.     protected CallBackInterface[] student;
  52.     protected String vak;
  53.     public ScoreLijst(CallBackInterface[] student,String vak) {
  54.         this.student = student;
  55.         this.vak = vak;
  56.     }
  57.     public String toString(){
  58.         String r = this.vak+"\n";
  59.         for(CallBackInterface i:student){
  60.             r += i.getEncryptedNaam() + ":" + i.getGrade() +"\n";
  61.         }
  62.         return r;
  63.     }
  64. }
  65.  
  66. class Test {
  67.     public static void main(String[] args){
  68.         Student an = new Student("An", 9);
  69.         Student birgit = new Student("Birgit", 16);
  70.         Student carl = new Student("Carl", 6);
  71.         Student dorien = new Student("Dorien", 11);
  72.         // changed:
  73.         ScoreLijst l = new ScoreLijst(new CallBackInterface[]{an.getCallBackReferentie(), birgit.getCallBackReferentie(), carl.getCallBackReferentie(), dorien.getCallBackReferentie()}, "Python");
  74.         System.out.println(l);
  75.        
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement