diff --git a/exercise1/Human.java b/exercise1/Human.java index 4b9b45e..1646eb6 100644 --- a/exercise1/Human.java +++ b/exercise1/Human.java @@ -17,7 +17,7 @@ public void sayMyName() { System.out.println("im a human!"); } - public void staticPrint() { + public final void staticPrint() { 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..3968092 100644 --- a/exercise1/Main.java +++ b/exercise1/Main.java @@ -1,18 +1,27 @@ 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) { + Student student = new Student(); + Professor professor = new Professor(); + + String result1 = (student instanceof Human)? "student an instance of Human" : "Student is not an instance of Human."; + System.out.println(result1); + String result2 = (professor instanceof Human)? "professor an instance of Human" : "professor is not an instance of Human."; + System.out.println(result2); + + Human human1 = new Student("negin Ahmadi", 11111, "major", "university"); + Human human2 = new Professor("name_one", "Specialty", "faculty", 2); + human1.sayMyName(); + human2.sayMyName(); + /* 9 +کد بالا نمونه ای از چند شکلی در برنامه نویسی هست + هر 2 کلاس از کلاس Human ارث بری کرده اند برای همین میتوان + یک شی از کلاس Human ساخت که با کلاس هایی که از ان ارث برده اند مقدار دهی میشود + اما متد های ان با توجه به کلاس مربوط مجدد نوشته شده می شوند برای همین + sayMyName + خروجی های متفاوتی بر اساس چیزی که در کلاس های ارث برده شده نوشته شده نشان میدهد + */ } } diff --git a/exercise1/Professor.java b/exercise1/Professor.java index 7c3b522..525507d 100644 --- a/exercise1/Professor.java +++ b/exercise1/Professor.java @@ -1,8 +1,45 @@ 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 numberOfCourses; + + public Professor(String fullName, String professorSpecialty, String professorFaculty, int numberOfCourses) { + setFullName(fullName); + setProfessorSpecialty(professorSpecialty); + setProfessorFaculty(professorFaculty); + setNumberOfCourses(numberOfCourses); + } + + public Professor(){} + + 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 getNumberOfCourses() { + return numberOfCourses; + } + + public void setNumberOfCourses(int numberOfCourses) { + this.numberOfCourses = numberOfCourses; + } + + @Override + public void sayMyName() { + System.out.println(getFullName() + " : " + getProfessorFaculty()); + } } diff --git a/exercise1/Student.java b/exercise1/Student.java index 0f8f606..4403b5f 100644 --- a/exercise1/Student.java +++ b/exercise1/Student.java @@ -1,9 +1,45 @@ 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 Student(String fullName, int studentNumber, String majorName, String universityName) { + setFullName(fullName); + setStudentNumber(studentNumber); + setUniversityName(universityName); + setMajorName(majorName); + } + + public 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()); + } } diff --git a/exercise1/negin.pdf b/exercise1/negin.pdf new file mode 100644 index 0000000..50a3170 Binary files /dev/null and b/exercise1/negin.pdf differ