Tower defense (OOP4Fun Book)

7. Encapsulation

3h 40min
  • TS 7.1. Exploring Encapsulation through Game Development with Greenfoot (Part I)
  • TS 7.2. Exploring Encapsulation through Game Development with Greenfoot (Part II)

Understanding the basic principles of object-oriented programming (50%)
Understanding the basics of algorithmisation (10%)
Understanding the syntax of the Java programming language (20%)
The ability of creating own programs with the use of OOP (20%)
7.1. Exploring Encapsulation through Game Development with Greenfoot (Part I)
Discussion
1 Introduction

The teacher should start the previously developed game and observe how different actors behave. Suggest developing another type of tower that can be manually controlled to remove enemies more easily. The user should be able to control one tower at a time. When the tower is clicked, it should become manually controlled. To indicate which tower is manually controlled, the currently controlled tower should have a different appearance.


5 min
Production
2 Task 7.1

Since students already know how to make a descendant class, let them form teams and create a ManualTower class as a descendant of the Tower class. Students should implement both the constructors and the act method, ensuring that the super constructors are called from these methods. In this part of the class, students will review the material, apply it, and improve their practical knowledge of inheritance.
(Team Collaboration and Coding)


20 min
Production
3 Task 7.2 and 7.3

The teacher should prepare icons for the manually controlled tower. To change the icon of the object programmatically, the teacher should explain to the students how to use the Actor.setImage(String) method. Allow students some time to test this function.

The teacher should discuss with students how to determine whether the tower is manually controlled. Emphasize that it is not only important to change the object's state but also to update its image. Highlight that if a user wants to change the state of a Tower object and only changes the attribute directly, the image will remain the same. This discussion should help students understand the need to change the value of an attribute through a method and to keep attributes private rather than public. Explain to the students that this practice is called encapsulation, where the internal state is hidden, and public methods are used to change that state in a controlled manner.

Allow the students to implement the logic of the function. Let them manually invoke their method and observe changes in the internal state.


30 min
Discussion
4 Discussion

The teacher should point out that the tower's state can be changed only by manually invoking the method. Point out that the mouse could be outside the world, in which case the mouse information will be null. Remind students that the act() method is constantly running during the game and that it should check whether the object has been clicked and only then invoke the changeControl() method. Highlight that the logic for processing the control should be encapsulated inside a separate methodprocessUserControl().


35 min
Practice
5 Code explanation

Consider how to change the actor's state by clicking on the object. To implement this, the GreenFoot.mouseClicked(Object) method should be explained. Also, introduce MouseInfoobject, which can be used for retrieving informations about the mouse position.


25 min
Assessment
6 Task 7.4

After defining the processUserControl() private method, let students implement its logic. When the mouse is clicked, the controlled tower should change. If the tower is manually controlled, it should follow and be directed towards the mouse. Remind them that it is possible for mouse to be outside the world. After grouping students into teams, let them implement the logic of the processUserControl() method.

One or two teams will present their work, and the group will discuss the results along with the teacher.By the end of the session, all students should understand how this method is implemented.

(Team Formation and Project Assignment)


10 min
0
7.2 Exploring Encapsulation through Game Development with Greenfoot (Part II)
Acquisition
1 Flipped Classroom Session

Students should identify the problem with the previously implemented user control and student should investigate how to solve the problem. 


30 min
Discussion
2 Class attributes

Explain what class attributes are: variables that belong to the class itself, rather than instances of the class. Relate this concept to the game scenario discussed earlier, where having a centralized attribute to manage the currently selected tower could solve the issue.


5 min
Production
3 Task 7.6

Add evidence of manually controlled tower.
To track which tower is currently selected in the game, add private static attribute-controlledInstance to the ManualTower class and initialize it to null. Static attribute is related to the whole class, not to an object of a class.Hence, defining a static variable will allow us to determinewhether tower has been selected and, if so, which one, byreferencing  theclass name, without needing to access aspecific object. The teacher should emphasize that there is one controlledInstancefor the whole game. At the beginning, controlledInstance should be initialized to null, as there is no selected tower. Inspect the internal state of class. Here the teacher explains differences between static and non-static attributes. The teacher with studentsdiscussbenefits of using static attributes in games. Teacher should also mention here static methods and discuss with the students where usingstatic methods is beneficial.


5 min
Practice
4 Method of class

Present the concept of class methods that can operate on class-level data.Discuss the need for methods like changeControlledInstance to manage switching the currently controlled tower. Emphasize that these methods can be called without needing an instance of the class. For example, school bell rings for everyone at the same time, it doesn’t matter who you are, on the other hand, checking a student’s homework requires information about that specific student.


10 min
Production
5 Task 7.7

Change of manually controlled tower from centralized place.
Teacher should add method changeControlledInstance to change manually controlled tower. Parameter of the method will be the tower user wants to select. First, it should be checked whether the controlled instance is currently selected. If it is, nothing should change, but if the passed instance is different than we should change currently controlled instance (reference to the currently controlled instance should be changed). Test out the function manually and observe that the icons of the towers don’t change. Point out that only changing the reference of the controlled instance,wouldn’t change the control and that it should be done manually. Add the code which releases the currently controlled instance and, after updating the reference, add code which sets manual control of newly controlled instance. Highlight the need for checking null references which could appear if there is no currently controlled instance and if there is no newly controlled instance (when the parameter is null).


20 min
Production
6 Task 7.8

Invoke change of manually controlled tower.
Manually test out the function whether it works correctly. Afterwards, discuss with the students where should this function be invoked. Method should be invoked inside of Arena’s act() function and inside of processUserControll() function. Lastly, make method ManualTower.changeControl(Boolean) private and observe changes of instance of ManualTower.


15 min
Discussion
7 Theory revision

Summarize the session, highlighting the importance of class attributes and methods in managing game logic efficiently.Encourage students to explore further by applying these concepts in their own programming projects.


10 min