Spaces:
Running
Running
public void setAttributeName(dtype attributeName) { ...<chk> <set value>... }
Browse files
Week 4: Writing classes/09a. Setting Methods
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
The principle of set methods is the same as that of observation methods:
|
| 2 |
+
they allow the client to CHANGE the STATE of the OBJECT.
|
| 3 |
+
OFTEN, setting methods are of the 'void' type, since their purpose is to set the value of an attribute.
|
| 4 |
+
|
| 5 |
+
Of course, the type can be something other than void, for example, a boolean type setting method could
|
| 6 |
+
return true or false depending on whether the given value was allowed or not.
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
Consider the class ExamResult, which has a setting method for the attribute grade added to it:
|
| 11 |
+
Before setting the value of the attribute, it is checked that the given value is within the allowed range.
|
| 12 |
+
|
| 13 |
+
The method is named in the form setAttributeValue.
|
| 14 |
+
The setting is done in a familiar way from the constructor, using the keyword 'this':
|
| 15 |
+
|
| 16 |
+
class ExamResult {
|
| 17 |
+
// PRIVATE ATTRIBUTES
|
| 18 |
+
private String student;
|
| 19 |
+
private int grade;
|
| 20 |
+
|
| 21 |
+
//CONSTRUCTOR
|
| 22 |
+
public ExamResult(String student, int grade) {
|
| 23 |
+
this.student = student;
|
| 24 |
+
this.grade = grade;
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
// GET
|
| 28 |
+
public int getGrade() {
|
| 29 |
+
return grade;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
//SET
|
| 33 |
+
public void setGrade(int grade) {
|
| 34 |
+
if (grade >= 1 && grade <= 5) {
|
| 35 |
+
this.grade = grade;
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
Now the client can set a new value for the rating by calling a method.
|
| 45 |
+
|
| 46 |
+
public class TestClass {
|
| 47 |
+
public static void main(String[] args) {
|
| 48 |
+
ExamResult r = new ExamResult("Ann", 3);
|
| 49 |
+
|
| 50 |
+
// SET/UPDATE ATTRIBUTE HERE
|
| 51 |
+
r.setGrade(5);
|
| 52 |
+
System.out.println(r.getGrade());
|
| 53 |
+
|
| 54 |
+
// VS
|
| 55 |
+
// This is not accepted, so the grade does not change
|
| 56 |
+
r.setGrade(-1);
|
| 57 |
+
System.out.println(r.getGrade());
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
Program outputs:
|
| 61 |
+
5
|
| 62 |
+
5
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
In the Student class, it would be easiest for the customer if points could be added to the regulation
|
| 67 |
+
by one method in addition to the given number
|
| 68 |
+
- often the number of credits increases by a certain value when a course is completed.
|
| 69 |
+
So let's implement methods in the class for both setting and adding credits.
|
| 70 |
+
At the same time, the other missing set and get methods are added to the class.
|
| 71 |
+
|
| 72 |
+
class Student {
|
| 73 |
+
private String name;
|
| 74 |
+
private String studentId;
|
| 75 |
+
private int studyPoints;
|
| 76 |
+
|
| 77 |
+
public Student(String name, String studentId, int studyPoints) {
|
| 78 |
+
this.name = name;
|
| 79 |
+
this.studentId = studentId;
|
| 80 |
+
this.studyPoints = studyPoints;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
public String getName() {
|
| 84 |
+
return name;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
public String getStudentId() {
|
| 88 |
+
return studentId;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
public int getStudyPoints() {
|
| 92 |
+
return this.studyPoints;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
public void setName(String name) {
|
| 96 |
+
this.name = name;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
public void setStudentId(String studentId) {
|
| 100 |
+
this.studentId = studentId;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
public void setStudyPoints(int studyPoints) {
|
| 104 |
+
if (studypoints >= 0) {
|
| 105 |
+
this.studypoitns = studypoints;
|
| 106 |
+
}
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
public void addPoints(int completionPoints) {
|
| 110 |
+
if (completionPoints >= 0) {
|
| 111 |
+
this.studypoints += completionPoints;
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|