diff --git a/exercise1/Human.java b/exercise1/Human.java index 4b9b45e..1ac5542 100644 --- a/exercise1/Human.java +++ b/exercise1/Human.java @@ -1,6 +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 +16,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..5fce533 100644 --- a/exercise1/Main.java +++ b/exercise1/Main.java @@ -3,16 +3,20 @@ 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 student1 = new Student("Ali Arad", 123456, "Mathematics", "Tehran"); + Professor professor1 = new Professor("sara mohamadi", "Mathematics", "Science", 10); + + System.out.println(student1 instanceof Human); + System.out.println(professor1 instanceof Human); + + Human human = new Student("zahra karimi", 54321, "Chemistry", "london"); + human.sayMyName(); + + human = new Professor("maryam", "Physics", "Engineering", 5); + human.sayMyName(); } } diff --git a/exercise1/Professor.java b/exercise1/Professor.java index 7c3b522..3640a0f 100644 --- a/exercise1/Professor.java +++ b/exercise1/Professor.java @@ -1,8 +1,62 @@ 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); + this.professorSpecialty = professorSpecialty; + this.professorFaculty = professorFaculty; + this.numberOfCourses = numberOfCourses; + } + + public void sayMyName() { + System.out.println(getFullName() + " , " + professorFaculty); + } + + 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; + } + + public static void main(String[] args) { + Professor p = new Professor("", "", "", 0); + + p.staticPrint(); + + p.setFullName("sara mohamadi"); + System.out.println(p.getFullName()); + + p.setProfessorSpecialty("Mathematics"); + System.out.println(p.getProfessorSpecialty()); + + p.setProfessorFaculty("Science"); + System.out.println(p.getProfessorFaculty()); + + p.setNumberOfCourses(10); + System.out.println(p.getNumberOfCourses()); + + p.sayMyName(); + } } diff --git a/exercise1/Student.java b/exercise1/Student.java index 0f8f606..ab7a828 100644 --- a/exercise1/Student.java +++ b/exercise1/Student.java @@ -1,9 +1,62 @@ 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); + this.studentNumber = studentNumber; + this.majorName = majorName; + this.universityName = universityName; + } + + public void sayMyName() { + System.out.println(getFullName()); + } + + 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; + } + + public static void main(String[] args) { + Student s = new Student("", 0, "", ""); + s.sayMyName(); + + + s.staticPrint(); + + s.setFullName("Ali Arad"); + System.out.println(s.getFullName()); + + s.setStudentNumber(123456); + System.out.println(s.getStudentNumber()); + + s.setMajorName("Mathematics"); + System.out.println(s.getMajorName()); + + s.setUniversityName("Tehran"); + System.out.println(s.getUniversityName()); + } } diff --git a/exercise1/q_9.pdf b/exercise1/q_9.pdf new file mode 100644 index 0000000..dc7fad2 Binary files /dev/null and b/exercise1/q_9.pdf differ