Lesson 0

Introduction to Object-Oriented Programming

In the first lesson we will strengthen the knowledge from the PPJ course. We will focus on those elements that are crucial to create something interesting during GUI course.

Task 0
What do you remember about Java?
Task 1

Without modifying the main method, make the code below work.

Hint: Subject class can have access to students' set (e.g. an array). Adding too many students to the set may throw an exception.


public class Main {
    public static void main(String args[]) {
        
        Person p1 = new Person("John", 50); //name, age
        Student s1 = new Student("Johnny", 20, 2); //name, age, semester
        Person p3 = (Person)s1;
	    Student s2 = new Student("Anna", 21, 4);
        
        p1.sayHelloTo(p3); // Hi Johnny!
        p3.sayHelloTo(p1); // Good morning, John!
	    s1.sayHelloTo(s2); // Yo, Anna!
        s1.sayHelloTo(p1); // Good morning, John!
        
        Subject s = new Subject("GUI");
        s.setTeacher(p1);
        
        try {
            s.addStudent(s1);
		    s.addStudent(s2);
        } catch(TooManyStudentsException e) {
            e.printStackTrace();
        }
        
        System.out.println(s); //GUI, teacher: John, students: Johnny - 2 sem, Anna - 4 sem
        
    }
}