Skip to content

done! #23

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

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
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions exercise1/Human.java
Original file line number Diff line number Diff line change
@@ -1,7 +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 +15,7 @@ public void sayMyName() {
System.out.println("im a human!");
}

public void staticPrint() {
public final void staticPrint() { // made it final so it cannot be overridden
System.out.println("this function should always print this string in all subclasses");
}
}
39 changes: 31 additions & 8 deletions exercise1/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,39 @@
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 student = new Student();
student.setFullName("John Doe");
student.setStudentNumber(123);
student.setMajorName("Computer Science");
student.setUniversityName("Harvard University");

Professor professor = new Professor();
professor.setFullName("Jane Smith");
professor.setProfessorSpecialty("Mathematics");
professor.setProfessorFaculty("Faculty of Science");
professor.setNumberOfCourse(5);

List<Human> humans = new ArrayList<>();
humans.add(student);
humans.add(professor);

for (Human human : humans) {
if (human instanceof Student) {
System.out.println("This is a Student");
} else if (human instanceof Professor) {
System.out.println("This is a Professor");
}
human.sayMyName();
}

Human human1 = new Student();
human1.setFullName("Alice Brown");
human1.sayMyName(); // Output: Alice Brown

Human human2 = new Professor();
human2.setFullName("Bob Johnson");
human2.sayMyName(); // Output: Bob Johnson from null (since professorFaculty is not set)
}
}
38 changes: 33 additions & 5 deletions exercise1/Professor.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
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 numberOfCourse;

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 getNumberOfCourse() {
return numberOfCourse;
}

public void setNumberOfCourse(int numberOfCourse) {
this.numberOfCourse = numberOfCourse;
}

@Override
public void sayMyName() {
System.out.println(getFullName() + " from " + getProfessorFaculty());
}
}
37 changes: 32 additions & 5 deletions exercise1/Student.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
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 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;
}

@Override
public void sayMyName() {
System.out.println(getFullName());
}
}