Wednesday, February 3, 2016

Student Level

Student Level

Implement a class Student, including the following attributes and methods:
  1. Two public attributes name(string) and score(int).
  2. A constructor expect a name as a parameter.
  3. A method getLevel to get the level(char) of the student.
score - level table:
  • A: score >= 90
  • B: score >= 80 and < 90
  • C: score >= 60 and < 80
  • D: score < 60

Example
Student student = new Student("Zuck");
student.score = 10;
student.getLevel(); // should be 'D'
student.score = 60;

student.getLevel(); // should be 'C'

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class Student {
    /**
     * Declare two public attributes name(string) and score(int).
     */
    // write your code here
    public String name;
    public int score;
    
    /**
     * Declare a constructor expect a name as a parameter.
     */
    // write your code here
    Student(String name) {
        this.name = name;
    }
    
    /**
     * Declare a public method `getLevel` to get the level(char) of this student.
     */
    // write your code here
    public char getLevel() {
        if (this.score >= 90) {
            return 'A';
        }
        else if (this.score >= 80) {
            return 'B';
        }
        else if (this.score >= 60) {
            return 'C';
        }
        else {
            return 'D';
        }
    }
    
}