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/exercise1/Human.java b/exercise1/Human.java index 4b9b45e..8459ccc 100644 --- a/exercise1/Human.java +++ b/exercise1/Human.java @@ -3,7 +3,7 @@ // 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 fullName; public String getFullName() { return fullName; @@ -17,7 +17,7 @@ public void sayMyName() { System.out.println("im a human!"); } - public void staticPrint() { + public static 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..6a4ecfb 100644 --- a/exercise1/Main.java +++ b/exercise1/Main.java @@ -1,7 +1,5 @@ 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 @@ -13,6 +11,32 @@ public class Main { public static void main(String[] args) { - + //Question6 + Student student = new Student("John Doe"); + Professor professor = new Professor("Dr. Smith"); + if (student instanceof Human) { + System.out.println(true); + } + else { + System.out.println(false); + } + if (professor instanceof Human) { + System.out.println(true); + } + else { + System.out.println(false); + } + //Question7 + /* + Human human = new Student("whatever"); + human.sayMyName(); + */ + //Question8 + /* + Human human = new Professor("whatever"); + human.sayMyName(); + */ + //Question9 + //Taghiri dar yek class tasiri bar digari nadarad. } } diff --git a/exercise1/Professor.java b/exercise1/Professor.java index 7c3b522..c830bbd 100644 --- a/exercise1/Professor.java +++ b/exercise1/Professor.java @@ -4,5 +4,33 @@ // 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 professorFaculty; + private String numberOfCourse; + private String professorSpecialty; + + public Professor(String fullName){ + } + + public String getProfessorFaculty() { + return professorFaculty; + } + public String getNumberOfCourse() { + return numberOfCourse; + } + public String getProfessorSpecialty() { + return professorSpecialty; + } + public void setProfessorFaculty(String professorFaculty) { + this.professorFaculty = professorFaculty; + } + public void setNumberOfCourse(String numberOfCourse) { + this.numberOfCourse = numberOfCourse; + } + public void setProfessorSpecialty(String professorSpecialty) { + this.professorSpecialty = professorSpecialty; + } + public void sayMyName() { + System.out.print(fullName + professorFaculty); + } } diff --git a/exercise1/Student.java b/exercise1/Student.java index 0f8f606..acb1456 100644 --- a/exercise1/Student.java +++ b/exercise1/Student.java @@ -5,5 +5,35 @@ // 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 { +public class Student extends Human { + private String fullName; + private String studentNumber; + private String majorName; + private String universityName; + + public Student(String fullName) { + this.fullName = fullName; + } + public String getStudentNumber() { + return studentNumber; + } + public String getMajorName() { + return majorName; + } + public String getUniversityName() { + return universityName; + } + public void setStudentNumber(String studentNumber) { + this.studentNumber = studentNumber; + } + public void setMajorName(String majorName) { + this.majorName = majorName; + } + public void setUniversityName(String universityName) { + this.universityName = universityName; + } + + public void sayMyName() { + System.out.println(fullName); + } }