Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise1 #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions exercise1/Human.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}
}
33 changes: 33 additions & 0 deletions exercise1/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 اشاره دارد و با بازنویسی در کلاس فرزند اجرا میشود
در نتیجه تغییرات در کلاس های فرزند بدون تغیر قسمت های دیگر اجرا میشود
*/
}
}
32 changes: 31 additions & 1 deletion exercise1/Professor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
33 changes: 32 additions & 1 deletion exercise1/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}