Spaces:
Running
Running
INTERACT w 2 classes OR create COPY of object
Browse files
Week 4: Writing classes/13A Objects from own classes [i.e. INTERACT w 2 classes OR create COPY of object]
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Objects formed from their own classes behave like any other objects.
|
| 2 |
+
That is, objects can be stored in variables and data structures, and their methods can be called in the same way
|
| 3 |
+
as the methods of objects created from classes that come with Java.
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
-------------------
|
| 7 |
+
|
| 8 |
+
Objects as method parameters and return values
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
Objects created from their own classes behave as method parameters and
|
| 12 |
+
return values in the same way as other objects.
|
| 13 |
+
Methods are passed a reference to a creature, so a method can (as a side effect) modify the creatures it receives as parameters.
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
For example, the class Student and an example of a method that prints student data:
|
| 17 |
+
|
| 18 |
+
class Student {
|
| 19 |
+
// ATTRIBUTES
|
| 20 |
+
private String name;
|
| 21 |
+
private String studentId;
|
| 22 |
+
private int studyPoints;
|
| 23 |
+
|
| 24 |
+
//CONSTRUCTOR
|
| 25 |
+
public Student(String name, String studentId, int studyPoints) {
|
| 26 |
+
this.name = name;
|
| 27 |
+
this.studentId = studentId;
|
| 28 |
+
this.studyPoints = studyPoints;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
public String getName() {
|
| 32 |
+
return name;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
public String getStudentId() {
|
| 36 |
+
return studentId;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
public int getStudyPoints() {
|
| 40 |
+
return this.studyPoints;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
public void setStudyPoints(int studyPoints) {
|
| 44 |
+
if (studyPoints >= 0) {
|
| 45 |
+
this.studyPoints = studyPoints;
|
| 46 |
+
}
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
public void addPoints(int completionPoints) {
|
| 50 |
+
if (completionPoints >= 0) {
|
| 51 |
+
this.studyPoints += completionPoints;
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
public class TestClass {
|
| 59 |
+
public static void main(String[] args) {
|
| 60 |
+
Student sally = new Student("Sally Student", "12345", 154);
|
| 61 |
+
printStudent(sally);
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
public static void printStudent(Student student) {
|
| 65 |
+
System.out.println("Name: " + student.getName());
|
| 66 |
+
System.out.println("St.id: " + student.getStudentId());
|
| 67 |
+
System.out.println("Study points: " + student.getStudyPoints());
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
TestClass outputs:
|
| 72 |
+
Name: Sally Student
|
| 73 |
+
St.id: 12345
|
| 74 |
+
Study points: 154
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
Next, let's define the class Course:
|
| 81 |
+
class Course {
|
| 82 |
+
private String identifier;
|
| 83 |
+
private int studyPoints;
|
| 84 |
+
|
| 85 |
+
public Course(String identifier, int studyPoints) {
|
| 86 |
+
this.identifier = identifier;
|
| 87 |
+
this.studyPoints = studyPoints;
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
public String getIdentifier() {
|
| 91 |
+
return identifier;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
public int getStudyPoints() {
|
| 95 |
+
return studyPoints;
|
| 96 |
+
}
|
| 97 |
+
}
|
| 98 |
+
...and then a method that takes the course and the student as parameters.
|
| 99 |
+
|
| 100 |
+
The method adds the credits of the course to the credits of the student:
|
| 101 |
+
public class TestClass {
|
| 102 |
+
public static void main(String[] args) {
|
| 103 |
+
Student samuel = new Student("Samuel Student", "12345", 154);
|
| 104 |
+
Course oop = new Course("OOP", 5);
|
| 105 |
+
|
| 106 |
+
addResult(samuel, oop);
|
| 107 |
+
System.out.println(samuel.getStudyPoints());
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
// NEW METHOD HERE
|
| 111 |
+
public static void addResult(Student student, Course course) {
|
| 112 |
+
student.addPoints(course.getStudyPoints());
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
Program outputs:
|
| 117 |
+
159
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
Of course, the method can also return a new object.
|
| 127 |
+
The following example creates a new student object by copying the name and student number from the object given as a parameter
|
| 128 |
+
|
| 129 |
+
public class TestClass {
|
| 130 |
+
public static void main(String[] args) {
|
| 131 |
+
Student samantha = new Student("Samantha Student", "12345", 154);
|
| 132 |
+
Student samantha2 = copy(samantha);
|
| 133 |
+
|
| 134 |
+
System.out.println(samantha2.getName());
|
| 135 |
+
System.out.println(samantha2.getStudentId());
|
| 136 |
+
System.out.println(samantha2.getStudyPoints());
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
// FROM INPUT 'student' object
|
| 140 |
+
// create a 'copy/new student' object
|
| 141 |
+
// retrieving the input's information
|
| 142 |
+
public static Student copy(Student student) {
|
| 143 |
+
Student copy = new Student(student.getName(),
|
| 144 |
+
student.getStudentId(), 0);
|
| 145 |
+
return copy;
|
| 146 |
+
}
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
Program outputs:
|
| 150 |
+
Samantha Student
|
| 151 |
+
12345
|
| 152 |
+
0
|
| 153 |
+
|
| 154 |
+
|