Presentation on theme: "A workshop for IT teachers"— Presentation transcript:
1A workshop for IT teachers IT is gr8!A workshop for IT teachers2015
2Preparing for programming concepts Data typesArrays (lists)OOPClasses and objectsAttributes and behaviour of objectsData transfer to and from an object
3Programming concepts: Data types All the operators work with numbers.All the slots are round.There is a difference between integer and real values.Drag one operator block to the script area in Scratch. Enter numbers. Click on the block. The result of the operation will appear in a pop-up. In this way calculations with different data types can be emphasized without having to write a complete program.Round is a function.It receives one argument.It returns an integer.
4Programming concepts: Data types All the operators work with text (strings).All the slots which can accept text are rectangular.The slot which contains a number (3) is round.34251 is treated as a string because it was entered as the argument of the length function.Use programming terminology, e.g.:The join function has two parameters. In this example it receives the words Hi and Jenny as arguments. It returns a string.
5Programming concepts: Data types When you compare values you create a condition.The result of a condition is a Boolean value.Boolean values fit into slots with pointed sides.Explain to learners that programming languages do not treat strings in the same way. Scratch do not consider upper and lower case letters to be different. (It is not case sensitive.) Other languages (such as Delphi), use the ASCII value of a character to decide if one letter is ‘larger’ than another one.
6Programming concepts: Arrays Let learners write the same proram in Delphi and Scratch. In this way they see that it is only the syntax which is different.
7The two blocks are actually event handlers The two blocks are actually event handlers. The list (array) had to be declared with stage scope, because both event handlers need access to it.
8In Delphi we draw a visual representation of an array (list of values) In Delphi we draw a visual representation of an array (list of values). In Scratch the list can be displayed on the Stage.
9Programming concepts: OOP Re-use of code (code stored in a unit)Passing data to pre-written codeAn object has attributes (data) and behaviour (methods)Data encapsulationData can only be passed to, and obtained from an object using methods.An object’s methods act on its own data.
10Why OOP?Delphi is an OOP language. It has been developed using objects. You have already used objects. You are going to learn how to create your own objects.
11Where have you used objects? Buttons, Panels, Edits (all are components – a special type of object).These components have been created according to a ‘blueprint’ or ‘recipe’ called a class.
12You have used static components, and instantiated components dynamically Dynamic components (instantiated while program is running)MyPanel := TPanel.Create(frmPicture);MyLabel := TLabel.Create(MyPanel);MyImage := TImage.Create(MyPanel);
13ClassesThe definitions (blueprint / recipe) for components are called classes and they are stored in units.
14How to instantiate and use an object based on a class Use the unit where the class has been declared.Declare a variable based on the class.Instantiate the object.Call methods from the object.Free the object.Guidelines to remember all the steps involved when an object is instantiated. In the beginning it helps learners if they write down the steps and refer to it while working.
15Grade 11 Chapter 13 Activity 3.1 Create a visible object (component) based on a class supplied by Delphi. The name of the unit (ComCtrls) which contains the class definitions of components is added to the uses clause by Delphi. It is still step 1.Grade 11Chapter 13Activity 3.1
16Delphi also provided other classes which can be used to instantiate objects. We want to create our own classes and instantiate objects based on these classes. So we need to know how an object is structured . . .The purpose of the lesson is to learn to create your own class. Therefore we need to know how an object is structured.
17What does an object look like? All objects have attributes (describing what the object looks like) andbehaviour (indicating what the object can do).For example . . .
18The various attributes and the behaviour have been indicated in a class definition (a recipe or blueprint) which is stored in a unit. For example the TEdit class definition is stored in the StdCtrls unit.
19Components are visible objects Components are visible objects. You can also use objects which are not visible at run-time of the program, for example a TStringList.
20The TStringList class definition is stored in the Classes unit. A StringList hasAttributesText (an array/list of strings)Count (an integer indicating the number of strings in the list)The TStringList class definition is stored in the Classes unit.andBehaviourAddStringsDeleteExchangeSortIndexOfSaveToFileLoadFromFile
21How to instantiate and use an object based on a TStringList class Create an invisible object based on a class supplied by Delphi. The name of the unit (Classes) which contains the class definitions of components is added to the uses clause by Delphi. It is still step 1.Grade 11Chapter 13Activity 5
22Benefits of using the TStringList class Learners can see the benefit of using classes.They experience the concept of data encapsulation.They want to create their own classes or collaborate and exchange classes.
23Passing data to a subroutine Re-confirming conceptsPassing data to a subroutineFunction Power(Base: Extended; Exponent: Extended): Extended; rAns := POWER(StrToInt(edtBase.Text), StrToInt(edtPower.Text));Remind learners that they wrote programs before where they had to pass data to a separate entity (pre-written code). When using objects, they have to specify the name of the object before calling a method. E.g.MyTextData.Add(‘Hallo’); similar to redOut.Lines.Add(‘Hallo’);
24Passing data to an object Re-confirming conceptsPassing data to an object
25Write your own class and instantiate an object When you write many programs, and more complicated ones, it may be necessary to create your own classes, so you can create the same objects in different programs, or so you can do more complicated programming in a separate entity.We are going to use an uncomplicated object to begin with: A waiter as an object.
26A Waiter hasAttributesNameNumber of hours the waiter workedRate the waiter earns per hourThe TWaiter class definition will be stored in the clsWaiter_u unit. (You choose the name of the unit yourself.)andNow you create your own class, based on the structure of a class provided by Delphi.BehaviourProvide his/her nameProvide the number of hours workedProvide the rate per hourCalculate and provide total wage
27How to instantiate and use an object based on a TWaiter class The same 5 steps. Since the class is not provide by Delphi, you have to include the name of the unit where the class is stored in the uses clause.Grade 12Chapter 1Activity 1.2
29An object contains attributes and behaviour Re-confirming conceptsA Form is an object (component).While you are building a new Form, you are adding attributes (components and variables with class scope).You are adding behaviour (event handlers and methods – functions and procedures).
30AttributesBehaviourWhile you were adding components and creating event handler and methods, Delphi entered the necessary statements in the Form’s class declaration.
31frmMain frmHideCache frmSwop frmSearch Prepare for concepts In Grade 11 learners use multiple forms. This is an example of a strategy followed in a learner’s Geo cache PAT.On the main Form the user had to log in using a username. On the other forms, the username is needed to perform certain operations. Even though learners do not know how to create classes, you can teach them this method to transfer the username to another form (an object).Write a ‘set’ method for the form which should receive the username (frmHideCache). This corresponds to the mutator methods they will learn about when they write their own classes. To the learners it is clear why the Set method is needed. Once they are in Grade 12, you can refer back to this technique when explaining the Set method for their own classes.
32Use OOP principles Methods are part of the class of the Form. Methods work with attributes (data) of the Form.Once learners write their own classes, show them how it corresponds to a Form’s class.
33Do NOT use procedural principles Subroutines - not methods.If variables are declared in the implementation section of the unit of the Form, the variable has unit scope, not class scope. The program will work, but it does not follow OOP principles. This may confuse learners, and will not contribute to their understanding of writing their own classes.Subroutines work with a variable which has unit scope.