Tower defense (OOP4Fun Book)

4. Variables and expressions

6h 20min
  • TS 4.1. Introduction to Variables and Data Types in the Greenfoot Environment
  • TS 4.2. Introduction to Operators and Expressions in the Greenfoot Environment
  • TS 4.3. Introduction to Constructors in the Greenfoot Environment
  • TS 4.4. Introduction to Attributes in the Greenfoot Environment
  • TS 4.5. Introduction to Constructor Overloading in the Greenfoot Environment

Understanding the basic principles of object-oriented programming (40%)
Understanding the basics of algorithmisation (30%)
Understanding the syntax of the Java programming language (20%)
The ability of creating own programs with the use of OOP (10%)
TS 4.1. Introduction to Variables and Data Types in the Greenfoot Environment
Discussion
1 Introduction

In the introduction section context related to the previous sessions is established. The teacher introduces the term variable.


10 min
Acquisition
2 Variable identification

§  Teacher introduces the term variable,

§  Students can be asked to research and identify variables for their game,

§  The variables can be discussed by the teacher and peers,

§  In this scenario, variable type can be omitted (or discussed in general).


5 min
Acquisition
3 Data types

o   Teacher introduces the term data type,

o   Examples from real-word can be discussed (e.g., integer numbers can be related to number of currently present students, decimal numbers can be related to a product price, text type can be related to instant messaging text, etc.),

o   Data types are considered in the context of the Greenfoot Environment and Java programing language,

o   Detailed discussion related to variable types required for the game.


15 min
Practice
4 Declaration of variables

o   Data types are considered in the context of the Greenfoot Environment and Java programing language,

o   Teacher should explain the difference between declaration and initialization of variables

o   Declaration of Game-required variables.

o   Additional examples can be considered. For example, if act() method is considered, variable for displaying text can be declared.


10 min
Practice
5 Initialization of variables
  • o   Based on previously presented data types, their data values and data ranges are introduced,
  • o   Data values and data ranges are considered in the context of the Greenfoot Environment and Java programing language,
  • o   Teacher should explain the difference between declaration and initialization of variables
  • o   Initialization of game-required variables.
  • o   Additional examples can be considered. For example, if act() method is considered, variable for displaying text can be initialized.

5 min
TS 4.2. Introduction to Operators and Expressions in the Greenfoot Environment
Discussion
1 Operators

The teacher introduces the term operator. The teacher should make this concept more relatable to students by using real-life examples (e.g., buying products at the market). Afterwards, the teacher introduces various operator types.


15 min
Discussion
2 Arithmetic operators and expressions

Teacher can explain operators already known from other courses (e.g., math and math arithmetic operators).These operators are considered in the context of the Greenfoot Environment and Java programing language,Teacher discusses various terms: operator, operand, operator precedence.Additional examples can be considered. Additional example may include defining local variables to retrieve and manipulate an entity's x-position and y-position, thereby changing its position by increasing the variable's values.


10 min
Discussion
3 Boolean operators

Teacher can explain operators already known from other courses (e.g., math and math Boolean operators). These operators are considered in the context of the Greenfoot Environment and Java programing language. The teacher discusses various terms: operator, operand, operator precedence.Additional examples may include defining local variables to check if an entity's x-position is equal to its y-position, using a boolean operator to determine if the entity is on a diagonal.


15 min
Discussion
4 Relational operators

Teacher can explain operators already known from other courses (e.g., math and math relational operators),These operators are considered in the context of the Greenfoot Environment and Java programing language. Teacher discusses various terms: operator, operand, operator precedence.Additional examples may include defining local variables to check if one entity's y-position is below another's, using relational operators to determine positional relationships between entities.


10 min
Discussion
5 Boolean expressions

The teacher can explain Boolean expressions in the context of previously presented operators. These expressions are considered in the context of the Greenfoot Environment and Java programing language. Teacher discusses operator, operand, and operator precedence in the context of boolean expressions. Additional examples may be considered. For example, boolean expressions can be used to verify that entity’s position is inside defined arena’s dimension of the game.


10 min
Discussion
6 Object expression

Teacher can explain object expression in the context of object-oriented design. These expressions are considered in the context of the Greenfoot Environment and Java programming language. The teacher discusses operator, operand, operator precedence, and class casting in the context of object expressions.Additional examples can be explored. For instance, comparing references of two object entities to check if they overlap.


5 min
Discussion
7 Reference variable and its null value

Teacher can explain reference variables in the context of object-oriented design. These reference variables are considered in the context of the Greenfoot Environment and Java programing language.The teacher should explain null reference value.


15 min
Practice
8 Task 4.1

Teacher should discuss Enemy's act() method. Teacher explains how to use local variables in the code, for example to use “rotation” variable. Teacher should describe difference between this.rotation and rotation. Teacher should describe getOneIntersectingObject(_cls_) method behavior. It is used and instance is stored in proper local variable (class casting is required). If there is no intersection object it returns null value. Based on the performed boolean evaluation, an appropriate acting is performed (i.e., rotating or turning). The results are discussed by the teacher and peers.


15 min
TS 4.3. Introduction to Constructors in the Greenfoot Environment
Discussion
1 Basic concepts of constructors

The teacher introduces the term constructor within the context of Class and Object concepts in Object-Oriented Programming (OOP). Constructors are used to initialize a concrete instance (i.e., an object) of a class.


10 min
Practice
2 Code explanation

 teacher should discuss constructors within the context of Class and Object OOP concepts: constructors are used to initialize concrete instances of a class. In addition, constructors are always invoked and can be defined either implicitly or explicitly. There are default constructors (which are implicitly defined) as well as parameterized and non-parameterized constructors (which are explicitly defined by a programmer). The differences between parameterized and non-parameterized constructors should also be discussed. To make this concept more relatable to students, the teacher should use real-life examples.


20 min
Production
3 Task: Rename class MyWord to Arena

The previously defined class MyWorld should be renamed. In this context, a new name should be chosen, specifically Arena. Additionally, the constructor of the class should also be renamed from MyWorld() to Arena().


5 min
Production
4 Task: Create layout of Arena

In this activity, a custom layout for Arena should be created. The custom layout should be provided within the constructor of the Arena class:one instance of Enemy, one instance of Orb, and at least one instance of Direction should be added. After declaring and initializing the variables, properties should be assigned by invoking the appropriate methods. Finally, these objects should be incorporated into the arena by invoking the addObject(Actor) method.


30 min
TS 4.4. Introduction to Attributes in the Greenfoot Environment
Practice
1 Task: Movement-related problem and solution

The teacher should explain that the Enemy is currently moving two cells at once, which causes issues with its movement. To address this, the speed of the Enemy can be modeled differently. The Enemy instance will now always move one cell at a time. Additionally, a new attribute called moveDelay can be defined, which will cause the Enemy instance to move only after a certain number of act() method calls have passed.


30 min
Discussion
2 Attributes

The teacher introduces the concept of attributes within the context of Class and Object concepts in Object-Oriented Programming (OOP).


10 min
Acquisition
3 Parameters of constructors

The teacher introduces the concept of parameters of the constructorin context of Class and Object concepts in Object-Oriented Programming (OOP).


10 min
Production
4 Task: Enemy.moveDelay

A new attribute named moveDelay of type int will be added to the Enemy class. A parameterized constructor will also be defined to initialize this attribute, with the attribute being set to the value provided by the parameter. The code in the Arena class will be adjusted accordingly.


20 min
Practice
5 Task: Movement of enemies respecting delay

The act() method of the Enemy class will be updated so that the Enemy moves only after the specified number of moveDelay calls of the method. Additionally, a new attribute called nextMoveCounter of type int will be introduced and initialized to 0. The act() method will be modified to call this.move(1) only when nextMoveCounter reaches 0. After the movement, nextMoveCounter will be reset to the value of moveDelay. If the Enemy instance cannot move because nextMoveCounter has not yet reached 0, nextMoveCounter will be decreased by 1.


30 min
TS 4.5. Introduction to Constructor Overloading in the Greenfoot Environment
Discussion
1 Basic concepts of constructor overloading

The concepts of constructors overloading are discussed.


5 min
Production
2 Task: Parametric constructor of class Direction

In this session, a parameterized constructor is defined for the Direction class with a single parameter, rotation, of type int. Within the constructor body, the created instance should be rotated based on the value of this parameter. The code in the Arena class should be updated accordingly.


25 min
Practice
3 Task: Overload constructors in class Direction

In this session an overloaded constructor is defined in the Direction class. A non-parameterized constructor is added, and within its body, the parameterized constructor is invoked with the argument rotation set to 0. The code in the Arena class should be updated accordingly, using the non-parameterized version of the Direction class constructor where possible.


25 min
Discussion
4 Theory revision

A review of the previously discussed concepts was conducted during this session.


20 min