From d87e691ba5e2d5379b7e63805b5a6f8fd47509b8 Mon Sep 17 00:00:00 2001 From: mobina sakhavat Date: Sun, 19 May 2024 11:57:43 +0330 Subject: [PATCH] done! --- .idea/.name | 1 + exercise1/Human.java | 4 +--- exercise1/Main.java | 39 +++++++++++++++++++++++++++++++-------- exercise1/Professor.java | 38 +++++++++++++++++++++++++++++++++----- exercise1/Student.java | 37 ++++++++++++++++++++++++++++++++----- 5 files changed, 98 insertions(+), 21 deletions(-) create mode 100644 .idea/.name diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..002da1d --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Main.java \ No newline at end of file diff --git a/exercise1/Human.java b/exercise1/Human.java index 4b9b45e..2d33163 100644 --- a/exercise1/Human.java +++ b/exercise1/Human.java @@ -1,7 +1,5 @@ package exercise1; -// 1.Change the staticPrint() method so that its output cannot be changed in all the classes that extend or implement Human. - public abstract class Human { private String fullName; @@ -17,7 +15,7 @@ public void sayMyName() { System.out.println("im a human!"); } - public void staticPrint() { + public final void staticPrint() { // made it final so it cannot be overridden System.out.println("this function should always print this string in all subclasses"); } } diff --git a/exercise1/Main.java b/exercise1/Main.java index ad47473..881cc35 100644 --- a/exercise1/Main.java +++ b/exercise1/Main.java @@ -3,16 +3,39 @@ import java.util.ArrayList; import java.util.List; -// After completing the Student and Professor classes, create an instance of each one: -// 6. Using instanceof keyword, check if the instances that you have created are really an instance of human class -// 7. Write the following code: Human human = new Student(); What is the output of human.sayMyName() ? -// 8. Now write this: -// Human human = new Professor(); -// What is the output of human.sayMyName() ? -// 9. What can we understand from question 7 and 8? - public class Main { public static void main(String[] args) { + Student student = new Student(); + student.setFullName("John Doe"); + student.setStudentNumber(123); + student.setMajorName("Computer Science"); + student.setUniversityName("Harvard University"); + Professor professor = new Professor(); + professor.setFullName("Jane Smith"); + professor.setProfessorSpecialty("Mathematics"); + professor.setProfessorFaculty("Faculty of Science"); + professor.setNumberOfCourse(5); + + List humans = new ArrayList<>(); + humans.add(student); + humans.add(professor); + + for (Human human : humans) { + if (human instanceof Student) { + System.out.println("This is a Student"); + } else if (human instanceof Professor) { + System.out.println("This is a Professor"); + } + human.sayMyName(); + } + + Human human1 = new Student(); + human1.setFullName("Alice Brown"); + human1.sayMyName(); // Output: Alice Brown + + Human human2 = new Professor(); + human2.setFullName("Bob Johnson"); + human2.sayMyName(); // Output: Bob Johnson from null (since professorFaculty is not set) } } diff --git a/exercise1/Professor.java b/exercise1/Professor.java index 7c3b522..c19c219 100644 --- a/exercise1/Professor.java +++ b/exercise1/Professor.java @@ -1,8 +1,36 @@ package exercise1; -// Implement or extend the human class to make a professor class... -// 4.Add the following attributes to this class with setters and getters: professorSpecialty, professorFaculty, numberOfCourse -// 5.Change the professor class so that when we call the sayMyName() method on an instance of this class, fullName of the professor plus their professorFaculty is printed. - -public class Professor { +public class Professor extends Human { + private String professorSpecialty; + private String professorFaculty; + private int numberOfCourse; + + public String getProfessorSpecialty() { + return professorSpecialty; + } + + public void setProfessorSpecialty(String professorSpecialty) { + this.professorSpecialty = professorSpecialty; + } + + public String getProfessorFaculty() { + return professorFaculty; + } + + public void setProfessorFaculty(String professorFaculty) { + this.professorFaculty = professorFaculty; + } + + public int getNumberOfCourse() { + return numberOfCourse; + } + + public void setNumberOfCourse(int numberOfCourse) { + this.numberOfCourse = numberOfCourse; + } + + @Override + public void sayMyName() { + System.out.println(getFullName() + " from " + getProfessorFaculty()); + } } diff --git a/exercise1/Student.java b/exercise1/Student.java index 0f8f606..8ccc30e 100644 --- a/exercise1/Student.java +++ b/exercise1/Student.java @@ -1,9 +1,36 @@ package exercise1; -// Human is an abstract concept, so we have created an abstract class to represent it... -// Implement or extend the Human class to make a student class. -// 2. Change the student class so that when we call the sayMyName() method on an instance of this class, the fullName attribute of the student is printed. -// 3.Add the following attributes to the student class with setters and getters: studentNumber, majorName, universityName +public class Student extends Human { + private int studentNumber; + private String majorName; + private String universityName; -public class Student { + public int getStudentNumber() { + return studentNumber; + } + + public void setStudentNumber(int studentNumber) { + this.studentNumber = studentNumber; + } + + public String getMajorName() { + return majorName; + } + + public void setMajorName(String majorName) { + this.majorName = majorName; + } + + public String getUniversityName() { + return universityName; + } + + public void setUniversityName(String universityName) { + this.universityName = universityName; + } + + @Override + public void sayMyName() { + System.out.println(getFullName()); + } }