diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..fb89d3f --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Human.java \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 07115cd..6f29fee 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/exercise1/Human.java b/exercise1/Human.java index 4b9b45e..dae8469 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 static 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..121a8e6 100644 --- a/exercise1/Main.java +++ b/exercise1/Main.java @@ -1,18 +1,25 @@ 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(); + + if (student instanceof Human) { + System.out.println("student is an instance of Human"); + } + else + System.out.println("student is not an instance of Human"); + if (professor instanceof Human) { + System.out.println("professor is an instance of Human"); + } + else + System.out.println("professor is not an instance of Human"); + + Human human_student = new Student("student_name", 1, "math", "beheshti"); + Human human_professor = new Professor("professor_name", "number theory", "mathematics", 24); + human_student.sayMyName(); + human_professor.sayMyName(); } } diff --git a/exercise1/Professor.java b/exercise1/Professor.java index 7c3b522..eeba303 100644 --- a/exercise1/Professor.java +++ b/exercise1/Professor.java @@ -1,8 +1,45 @@ package exercise1; +public class Professor extends Human { + private String professorSpecialty; + private String professorFaculty; + private int numberOfCourses; -// 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 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()); + } +} \ No newline at end of file diff --git a/exercise1/Student.java b/exercise1/Student.java index 0f8f606..32121ba 100644 --- a/exercise1/Student.java +++ b/exercise1/Student.java @@ -1,9 +1,46 @@ package exercise1; +public class Student extends Human { + private int studentNumber; + private String majorName; + private String universityName; -// 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 Student(String fullName, int studentNumber, String majorName, String universityName) { + setFullName(fullName); + setStudentNumber(studentNumber); + setUniversityName(universityName); + setMajorName(majorName); + } -public class Student { + public Student(){ + } + + + public void setStudentNumber(int studentNumber) { + this.studentNumber = studentNumber; + } + + public int getStudentNumber() { + return studentNumber; + } + + public void setMajorName(String majorName) { + this.majorName = majorName; + } + + public String getMajorName() { + return majorName; + } + + public void setUniversityName(String universityName) { + this.universityName = universityName; + } + + public String getUniversityName() { + return universityName; + } + + @Override + public void sayMyName() { + System.out.println(getFullName()); + } } diff --git a/exercise1/answer.txt b/exercise1/answer.txt new file mode 100644 index 0000000..45c2b16 --- /dev/null +++ b/exercise1/answer.txt @@ -0,0 +1,14 @@ +این نمونه ای از چندشکلی در برنامه نویسی است +از آنجایی که هر دو کلاس زیرشاخه های کلاس +Human +هستند میتوان شی ای از کلاس +Human +ساخت و آن را با کلاس های زیرشاخه +Student +و +Professor +مقداردهی کرد + +به همین علت تابع +sayMyName +مقادیر متفاوتی به ازای هر شی دارد \ No newline at end of file diff --git a/out/production/AP-1/.gitignore b/out/production/AP-1/.gitignore new file mode 100644 index 0000000..524f096 --- /dev/null +++ b/out/production/AP-1/.gitignore @@ -0,0 +1,24 @@ +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +replay_pid* diff --git a/out/production/AP-1/.idea/.gitignore b/out/production/AP-1/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/out/production/AP-1/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/out/production/AP-1/.idea/.name b/out/production/AP-1/.idea/.name new file mode 100644 index 0000000..fb89d3f --- /dev/null +++ b/out/production/AP-1/.idea/.name @@ -0,0 +1 @@ +Human.java \ No newline at end of file diff --git a/out/production/AP-1/.idea/AP-1.iml b/out/production/AP-1/.idea/AP-1.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/out/production/AP-1/.idea/AP-1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/AP-1/.idea/misc.xml b/out/production/AP-1/.idea/misc.xml new file mode 100644 index 0000000..6f29fee --- /dev/null +++ b/out/production/AP-1/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/AP-1/.idea/modules.xml b/out/production/AP-1/.idea/modules.xml new file mode 100644 index 0000000..76c5b34 --- /dev/null +++ b/out/production/AP-1/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/AP-1/.idea/uiDesigner.xml b/out/production/AP-1/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/out/production/AP-1/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/AP-1/.idea/vcs.xml b/out/production/AP-1/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/out/production/AP-1/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/AP-1/README.md b/out/production/AP-1/README.md new file mode 100644 index 0000000..25ceecf --- /dev/null +++ b/out/production/AP-1/README.md @@ -0,0 +1,36 @@ +ترتیب کلاس ها برای کامل کردن: + +1. Human +2. Student +3. Professor +4. Main + + + - کلاس های professor و student از کلاس human ارث بری میکنند. + + +سوال 1: در کلاس Human متود ()staticPrint را طوری تغییر دهید که خروجی آن در کلاس هایی که کلاس Human را پیاده سازی می کنند قابل تغییر نباشد. + +سوال 2: کلاس Student را طوری تغییر دهید که هنگام صدا زدن متود ()sayMyName بر روی آن، مشخصه fullName آن در خروجی چاپ شود. + +سوال 3: مشخصه های زیر را با توابع Setter و Getter مناسب به کلاس Student اضافه کنید. +#### studentNumber, majorName, universityName + +سوال 4: مشخصه های زیر را با توابع Setter و Getter مناسب به کلاس Professor اضافه کنید. +#### professorFaculty, numberOfCourse, professorSpecialty + +سوال 5: کلاس Professor را طوری تغییر دهید که هنگام صدا زدن متود ()sayMyName بر روی آن، مشخصه fullName و professorFaculty در خروجی چاپ شود. + +سوال 6: بعد از کامل کردن کلاس های Student و Professor یک نمونه از هرکدام بسازید و با استفاده از کلمه کلیدی instanceof چک کنید که آیا این دو نمونه واقعا نمونه ای از کلاس Human هستند یا نه. + +سوال 7: کد مقابل را بنویسید. +#### Human human = new Student(); +خروجی ()human.sayMyName چیست؟ + +سوال 8: حالا این کد را بنویسید. +#### Human human = new Professor(); +خروجی ()human.sayMyName چیست؟ + + +سوال 9: +از این دو مثال چه نتیجه ای می توان گرفت؟