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

Fateme rostami #9

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
3 changes: 1 addition & 2 deletions exercise1/Human.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
}
}
20 changes: 12 additions & 8 deletions exercise1/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
}
64 changes: 59 additions & 5 deletions exercise1/Professor.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
63 changes: 58 additions & 5 deletions exercise1/Student.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
Binary file added exercise1/q_9.pdf
Binary file not shown.