From ed28bda9f4b51751886f84f4b1cbaef8a040a0b5 Mon Sep 17 00:00:00 2001 From: Mahsa Date: Sat, 18 May 2024 20:30:29 +0330 Subject: [PATCH] commit 1 --- exercise1/Human.java | 4 ++-- exercise1/Main.java | 33 +++++++++++++++++++++++++++++++++ exercise1/Professor.java | 32 +++++++++++++++++++++++++++++++- exercise1/Student.java | 33 ++++++++++++++++++++++++++++++++- 4 files changed, 98 insertions(+), 4 deletions(-) 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..2758e91 100644 --- a/exercise1/Main.java +++ b/exercise1/Main.java @@ -13,6 +13,39 @@ public class Main { public static void main(String[] args) { + //Q6 + 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); + } + //Q7 + /* + Human human = new Student("whatever"); + human.sayMyName(); + */ + + //Q8 + /* + Human human = new Professor("whatever"); + human.sayMyName(); + */ + + + //Q9 + /* + یک نمونه از کلاس فرزند Student یا Professor به کلاس والد یعنی human اشاره دارد و با بازنویسی در کلاس فرزند اجرا میشود + در نتیجه تغییرات در کلاس های فرزند بدون تغیر قسمت های دیگر اجرا میشود + */ } } diff --git a/exercise1/Professor.java b/exercise1/Professor.java index 7c3b522..9ad2fac 100644 --- a/exercise1/Professor.java +++ b/exercise1/Professor.java @@ -4,5 +4,35 @@ // 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; + } + + @Override + public void sayMyName() { + System.out.print(fullName + professorFaculty); + } } diff --git a/exercise1/Student.java b/exercise1/Student.java index 0f8f606..b6d96b9 100644 --- a/exercise1/Student.java +++ b/exercise1/Student.java @@ -5,5 +5,36 @@ // 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); + } } +