Spaces:
Running
Running
TurkuBasicOOPinJava
/
Week 4: Writing classes
/13A Objects from own classes [i.e. INTERACT w 2 classes OR create COPY of object]
| Objects formed from their own classes behave like any other objects. | |
| That is, objects can be stored in variables and data structures, and their methods can be called in the same way | |
| as the methods of objects created from classes that come with Java. | |
| ------------------- | |
| Objects as method parameters and return values | |
| Objects created from their own classes behave as method parameters and | |
| return values in the same way as other objects. | |
| Methods are passed a reference to a creature, so a method can (as a side effect) modify the creatures it receives as parameters. | |
| For example, the class Student and an example of a method that prints student data: | |
| class Student { | |
| // ATTRIBUTES | |
| private String name; | |
| private String studentId; | |
| private int studyPoints; | |
| //CONSTRUCTOR | |
| public Student(String name, String studentId, int studyPoints) { | |
| this.name = name; | |
| this.studentId = studentId; | |
| this.studyPoints = studyPoints; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public String getStudentId() { | |
| return studentId; | |
| } | |
| public int getStudyPoints() { | |
| return this.studyPoints; | |
| } | |
| public void setStudyPoints(int studyPoints) { | |
| if (studyPoints >= 0) { | |
| this.studyPoints = studyPoints; | |
| } | |
| } | |
| public void addPoints(int completionPoints) { | |
| if (completionPoints >= 0) { | |
| this.studyPoints += completionPoints; | |
| } | |
| } | |
| } | |
| public class TestClass { | |
| public static void main(String[] args) { | |
| Student sally = new Student("Sally Student", "12345", 154); | |
| printStudent(sally); | |
| } | |
| public static void printStudent(Student student) { | |
| System.out.println("Name: " + student.getName()); | |
| System.out.println("St.id: " + student.getStudentId()); | |
| System.out.println("Study points: " + student.getStudyPoints()); | |
| } | |
| } | |
| TestClass outputs: | |
| Name: Sally Student | |
| St.id: 12345 | |
| Study points: 154 | |
| Next, let's define the class Course: | |
| class Course { | |
| private String identifier; | |
| private int studyPoints; | |
| public Course(String identifier, int studyPoints) { | |
| this.identifier = identifier; | |
| this.studyPoints = studyPoints; | |
| } | |
| public String getIdentifier() { | |
| return identifier; | |
| } | |
| public int getStudyPoints() { | |
| return studyPoints; | |
| } | |
| } | |
| ...and then a method that takes the course and the student as parameters. | |
| The method adds the credits of the course to the credits of the student: | |
| public class TestClass { | |
| public static void main(String[] args) { | |
| Student samuel = new Student("Samuel Student", "12345", 154); | |
| Course oop = new Course("OOP", 5); | |
| addResult(samuel, oop); | |
| System.out.println(samuel.getStudyPoints()); | |
| } | |
| // NEW METHOD HERE | |
| public static void addResult(Student student, Course course) { | |
| student.addPoints(course.getStudyPoints()); | |
| } | |
| } | |
| Program outputs: | |
| 159 | |
| Of course, the method can also return a new object. | |
| The following example creates a new student object by copying the name and student number from the object given as a parameter | |
| public class TestClass { | |
| public static void main(String[] args) { | |
| Student samantha = new Student("Samantha Student", "12345", 154); | |
| Student samantha2 = copy(samantha); | |
| System.out.println(samantha2.getName()); | |
| System.out.println(samantha2.getStudentId()); | |
| System.out.println(samantha2.getStudyPoints()); | |
| } | |
| // FROM INPUT 'student' object | |
| // create a 'copy/new student' object | |
| // retrieving the input's information | |
| public static Student copy(Student student) { | |
| Student copy = new Student(student.getName(), | |
| student.getStudentId(), 0); | |
| return copy; | |
| } | |
| } | |
| Program outputs: | |
| Samantha Student | |
| 12345 | |
| 0 | |