Chapter5_2-Methods

public string getstudentname return studentname

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ypen attributen; ... public void setAttribute1(dataType1 param){ attribute1 = param; } ... public void setAttributen(dataTypen param) { attributen = param; } ... } Page 26 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP public class Course { // Attributes private String studentName; private String courseCode ; ... public String getStudentName() { return studentName; } public String getCourseCode() { return courseCode; } ... public void setStudentName(String val) { studentName = val; } public void setCourseCode(String val) { courseCode = val; } } Page 27 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP public class CourseRegistration { public static void main(String args) { Course course1, course2; //Create and assign values to course1 course1 = new Course( ); course1.setCourseCode(“CSC112“); course1.setStudentName(“Majed AlKebir“); //Create and assign values to course2 course2 = new Course( ); course2.setCourseCode(“CSC107“); course2.setStudentName(“Fahd AlAmri“); System.out.println(course1.getStudentName() + " has the course “ + course1.getCourseCode()); System.out.println(course2.getStudentName() + " has the course “ + course2.getCourseCode()); } } Page 28 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Passing an Object to a Setter Page 29 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Using Setters and sharing the same Object • The same Student object reference is passed to card1 and card2 using setters • Page 30 Since we are actually passing the same object reference, it results in the owner of two LibraryCard objects referring to the same Student object Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Class Constructors • A class is a blueprint or prototype from which objects of the same type are created. • Constructors define the initial state...
View Full Document

Ask a homework question - tutors are online