Simple Project use of classes, copy constructor and aggregation JAVA by Ravinder Nath Rajotiya - April 20, 20210 Share on Facebook Share Send email Mail Print Print A simple project Following Classes: 1. Student Class 2. Subject Class 3. ExamPaper Class Aggregation example: when you have field in one class that are also the object in another class, there comes a concept what is called aggregation. We can access those object members from this class object using aggregation concept. See code in main method for aggregation: System.out.println(paper.getSubject().getSubjectCode()); //similarily if you want to extract only the student number, see how you can System.out.println(“Student number:”+paper.getStudent().getStudentNumber()); Let us see the complete project involving all the above classes in intellij package com.example; public class classAndObjects { public static void main(String[] args) { Subject subject1 = new Subject (“ETEE 310”, “MP&MC”); Student std1=new Student(“Ravinder”, 1234); Exampaper paper = new Exampaper(std1, subject1, 100); //System.out.println(paper); //we can extract more information from paper object, say we want to know the subject code. //See the following code carefully //this is use of aggregation in classes System.out.println(paper.getSubject().getSubjectCode()); //similarily if you want to extract only the student number, see how you can System.out.println(“Student number:”+paper.getStudent().getStudentNumber()); /* Subject subject2= new Subject(“ETEE 310”, “MP&MC”); Student std1=new Student(“Ravinder”, 1234); Student std2=new Student(“Rajotiya”, 1235); if(subject1.equal(subject2)) System.out.println(“They are same”); System.out.println(subject1); System.out.println(std1); // this will print package , class and the object memory address and this we donot want. //we have to override the toString default String method so that it can be printed in readable format. //create toString method in class Subject that explains what is happening to */ } } // package com.example; public class Subject { private String subjectCode; private String subjectName; public Subject (String subjectCode, String subjectName) { this.subjectCode = subjectCode; this.subjectName = subjectName; } //copy constructor public Subject(Subject subject) { this.subjectCode = subject.subjectCode; this.subjectName = subject.subjectName; } public boolean equal(Subject object) {/* if(this.subjectCode.equals(object.subjectCode) && (this.subjectName.equals(object.subjectName))) { return true; } else { return false; } */ //we can replace everything in above comment by simple one line return this.subjectCode.equals(object.subjectCode) && (this.subjectName.equals(object.subjectName)); } public String getSubjectName() { return subjectName; } public void setSubjectCode(String subjectCode) { this.subjectCode = subjectCode; } public void setSubjectName(String subjectName) { this.subjectName = subjectName; } public String getSubjectCode() { return subjectCode; } //is used to explain what is secific happening in object // so we are overriding the class that inherits main class of all classes //so we are trying our own toString method @Override public String toString() { return “Subject Information:\n” + “Subject Code : “+ subjectCode +“\n“+ “Subject Name:” +subjectName; //now run and see your output } } // Student classification package com.example; public class Student { private String studentname; private long studentNumber; public Student(String studentname, long studentNumber) { this.studentname = studentname; this.studentNumber = studentNumber; } //copy constructor public Student (Student obj1) { this.studentNumber= obj1.studentNumber; this.studentname=obj1.studentname; } // equal method public boolean equals(Student obj) { return this.studentNumber== obj.studentNumber && this.studentname.equals(obj.studentname); } public String getStudentname() { return studentname; } public long getStudentNumber() { return studentNumber; } public void setStudentname(String studentname) { this.studentname = studentname; } public void setStudentNumber(long studentNumber) { this.studentNumber = studentNumber; } @Override //override the inbuilt toString so that code is readable public String toString() { return “student information”+“\n“+ “Student num:”+studentNumber +“\n“+ “student name”+studentname; } } Aggrgation: aggregation occurs in JAVA when fields in one class are objects of other class. Public class Exampaper { private Student student; //field of this class but object of Student class private Subject subject; // field of this class but object f Subject class private int marks; } package com.example; public class Exampaper { private Student student; private Subject subject; private int marks; public Exampaper(Student student, Subject subject, int marks) { this.student = student; this.subject = subject; this.marks = marks; } public Student getStudent() { return new Student(student);// instead of passing back my student object pass a copy } public Subject getSubject() { return new Subject(subject); // instead of passing back my subject object pass a copy of it } public int getMarks() { return marks; } public void setStudent(Student student) { this.student = new Student(student); //also this.student, student that is passed in and student object on th of this.student exactly to the same address, so this is a security risk in handling object, so, // for security purpose let us pass a copy of student object rather than setting the value directly //similarily for subject object this.subject = new Subject(subject); this.marks=marks; // as marks are not of object type, so set is directly } public void setSubject(Subject subject) { //this.subject = subject; //not this //instead of passing back(returning) the object itself pass a copy instead this.subject = new Subject(subject); //this way is correct } public void setMarks(int marks) { this.marks = marks; } @Override public String toString() { return “Exam Paper Information ” + “\n” + “student info “+student + “\n“+ “Subject info “+subject + “\n” + “marks info” + marks; } } main method use of aggregation…. see how subject code information is derived through paper objective //similarly, how student number is extracted from paper object.getStudent().getStudentNumber() public class classAndObjects { public static void main(String[] args) { Subject subject1 = new Subject (“ETEE 310”, “MP&MC”); Student std1=new Student(“Ravinder”, 1234); Exampaper paper = new Exampaper(std1, subject1, 100); //System.out.println(paper); //we can extract more information from paper object, say we want to know the subject code. //See the following code carefully //this is use of aggregation in classes System.out.println(paper.getSubject().getSubjectCode()); //similarily if you want to extract only the student number, see how you can System.out.println(“Student number:”+paper.getStudent().getStudentNumber()); } Share on Facebook Share Send email Mail Print Print