diff --git a/exercise1/Human.java b/exercise1/Human.java index 4b9b45e..bad58c5 100644 --- a/exercise1/Human.java +++ b/exercise1/Human.java @@ -1,23 +1,22 @@ -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; - - public String getFullName() { - return fullName; - } - - public void setFullName(String fullName) { - this.fullName = fullName; - } - - public void sayMyName() { - System.out.println("im a human!"); - } - - public void staticPrint() { - System.out.println("this function should always print this string in all subclasses"); - } -} +abstract class Human { + String fullName; + + Human(String name) { + fullName = name; + } + + static void staticPrint() { + System.out.println("Cannot be overridden."); + } + + abstract void sayMyName(); + + public String getFullName() { + return fullName; + } + + public void setFullName(String fullName) { + this.fullName = fullName; + } + +} diff --git a/exercise1/Main.java b/exercise1/Main.java index ad47473..cae5ef0 100644 --- a/exercise1/Main.java +++ b/exercise1/Main.java @@ -1,18 +1,14 @@ -package exercise1; - -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) { - - } -} +public class Main { + public static void main(String[] args) { + Student student = new Student("John Doe", "123456", "Computer Science", "XYZ University"); + student.sayMyName(); + + Professor professor = new Professor("Jane Smith", "Mathematics", 10, "Calculus"); + professor.sayMyName(); + + System.out.println(student instanceof Human); // Should print true + System.out.println(professor instanceof Human); // Should print true + } +} + +// q9: \ No newline at end of file diff --git a/exercise1/Professor.java b/exercise1/Professor.java index 7c3b522..4c9eae2 100644 --- a/exercise1/Professor.java +++ b/exercise1/Professor.java @@ -1,8 +1,16 @@ -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 { -} +class Professor extends Human { + String professorFaculty; + int numberOfCourse; + String professorSpecialty; + + Professor(String fullName, String faculty, int courses, String specialty) { + super(fullName); + professorFaculty = faculty; + numberOfCourse = courses; + professorSpecialty = specialty; + } + + void sayMyName() { + System.out.println("Full Name: " + fullName + ", Faculty: " + professorFaculty); + } +} diff --git a/exercise1/Student.java b/exercise1/Student.java index 0f8f606..f60879e 100644 --- a/exercise1/Student.java +++ b/exercise1/Student.java @@ -1,9 +1,16 @@ -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 { -} +class Student extends Human { + String studentNumber; + String majorName; + String universityName; + + Student(String fullName, String number, String major, String uni) { + super(fullName); + studentNumber = number; + majorName = major; + universityName = uni; + } + + void sayMyName() { + System.out.println("Full Name: " + fullName); + } +} diff --git a/exercise1/q9.txt b/exercise1/q9.txt new file mode 100644 index 0000000..ef97435 --- /dev/null +++ b/exercise1/q9.txt @@ -0,0 +1 @@ +با این مثال‌ها، می‌بینیم که حتی اگر Student و Professor نمونه‌های Human باشند (مطابق نمونه‌چک)، به خاطر خطای ClassCastException، نمی‌توان آنها را جایگزین هم کرد. این نشان‌دهنده‌ی اهمیت چندشکلی و استفاده درست از انواع در برنامه‌نویسی شی‌گرا است. \ No newline at end of file