Spaces:
Running
Running
this.attr1 = new ArrayList<Class1>();
Browse files
Week 5: Class hierarchies/06. Class gradebook
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write a category 'Gradebook' with the following properties
|
| 2 |
+
Attribute owner of type string
|
| 3 |
+
Attribute grades, whose type is a list. The list must be able to store objects of type Grade (the class can be found in the attached program)
|
| 4 |
+
A constructor that takes the owner as a parameter and sets the value of the attribute. The constructor also initializes the grade list.
|
| 5 |
+
The attributes must be 'protected' so that the client cannot see them, but any inheriting classes can.
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
import java.util.Random;
|
| 9 |
+
import java.lang.reflect.Field;
|
| 10 |
+
import java.util.ArrayList;
|
| 11 |
+
|
| 12 |
+
public class Test{
|
| 13 |
+
public static void main(String[] args){
|
| 14 |
+
final Random r = new Random();
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
System.out.println("Testing class Gradebook...");
|
| 18 |
+
Gradebook gb = new Gradebook("Gary Grader");
|
| 19 |
+
|
| 20 |
+
System.out.println("Object created!");
|
| 21 |
+
|
| 22 |
+
try {
|
| 23 |
+
Field number = gb.getClass().getDeclaredField("owner");
|
| 24 |
+
boolean numberPrivate = number.getModifiers() == 4;
|
| 25 |
+
System.out.println("Owner is protected: " + numberPrivate);
|
| 26 |
+
} catch (Exception e) {
|
| 27 |
+
System.out.println("Attribute owner is not defined");
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
try {
|
| 31 |
+
Field grades = gb.getClass().getDeclaredField("grades");
|
| 32 |
+
boolean asPrivate = grades.getModifiers() == 4;
|
| 33 |
+
System.out.println("Grades is protected: " + asPrivate);
|
| 34 |
+
|
| 35 |
+
ArrayList<Grade> as = gb.grades;
|
| 36 |
+
boolean isInitialized = gb != null;
|
| 37 |
+
System.out.println("Grades is initialized: " + isInitialized);
|
| 38 |
+
} catch (Exception e) {
|
| 39 |
+
System.out.println("Attribute grades is not initialized");
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
class Grade {
|
| 45 |
+
private String student;
|
| 46 |
+
private int grade;
|
| 47 |
+
|
| 48 |
+
public Grade(String student, int grade) {
|
| 49 |
+
this.student = student;
|
| 50 |
+
this.grade = grade;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
public String getStudent() {
|
| 54 |
+
return student;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
public int getGrade() {
|
| 58 |
+
return grade;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
//add
|
| 69 |
+
class Gradebook {
|
| 70 |
+
protected String owner;
|
| 71 |
+
protected ArrayList<Grade> grades;
|
| 72 |
+
|
| 73 |
+
// constructor
|
| 74 |
+
public Gradebook(String owner) {
|
| 75 |
+
this.owner = owner;
|
| 76 |
+
this.grades = new ArrayList<Grade>();
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
Testing class Gradebook...
|
| 83 |
+
Object created!
|
| 84 |
+
Owner is protected: true
|
| 85 |
+
Grades is protected: true
|
| 86 |
+
Grades is initialized: true
|
| 87 |
+
|