Top Banner
IT is gr8! A workshop for IT teachers 2015
33

IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

Dec 15, 2015

Download

Documents

Kaylee Hammonds
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

IT is gr8!A workshop for IT teachers

2015

Page 2: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

Preparing for programming concepts

• Data types• Arrays (lists)• OOP• Classes and objects• Attributes and behaviour of objects• Data transfer to and from an object

Page 3: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

Programming concepts: Data types

All the operators work with numbers.All the slots are round.There is a difference between integer and real values.

Round is a function.It receives one argument.It returns an integer.

Page 4: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

Programming concepts: Data typesAll 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.

Page 5: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

Programming 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.

Page 6: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

Programming concepts: Arrays

Page 7: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.
Page 8: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.
Page 9: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

Programming concepts: OOP

1. Re-use of code (code stored in a unit)2. Passing data to pre-written code3. An object has attributes (data) and

behaviour (methods)4. Data encapsulation

a) Data can only be passed to, and obtained from an object using methods.

b) An object’s methods act on its own data.

Page 10: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

Why 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.

Page 11: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

Where 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.

Page 12: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

You have used static components, and instantiated components dynamically

‘Static’ components

Dynamic components (instantiated while program is running)• MyPanel := TPanel.Create(frmPicture);• MyLabel := TLabel.Create(MyPanel);• MyImage := TImage.Create(MyPanel);

Page 13: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

Classes

The definitions (blueprint / recipe) for components are called classes and they are stored in units.

Page 14: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

How to instantiate and use an object based on a class

1. Use the unit where the class has been declared.

2. Declare a variable based on the class.3. Instantiate the object.4. Call methods from the object.5. Free the object.

Page 15: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

1.

2.

3.

4.

5.

Grade 11Chapter 13Activity 3.1

Page 16: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

Delphi 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 . . .

Page 17: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

What does an object look like?

• All objects have attributes (describing what the object looks like) and • behaviour (indicating what the object can

do).

For example . . .

Page 18: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

The 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.

Page 19: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

Components are visible objects. You can also use objects which are not visible at run-time of the program, for example a TStringList.

Page 20: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

A StringList has

Attributes Text (an array/list of strings) Count (an integer indicating the number

of strings in the list)

and

Behaviour AddStrings Delete Exchange Sort IndexOf SaveToFile LoadFromFile

The TStringList class definition is stored in the Classes unit.

Page 21: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

How to instantiate and use an object based on a TStringList class

1.

2.

3.

4.

5. Grade 11Chapter 13Activity 5

Page 22: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

Benefits 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.

Page 23: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

Passing data to a subroutineFunction Power(Base: Extended; Exponent: Extended): Extended;rAns := POWER(StrToInt(edtBase.Text), StrToInt(edtPower.Text));

Re-confirming concepts

Page 24: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

Passing data to an objectRe-confirming

concepts

Page 25: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

Write 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.

Page 26: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

A Waiter has

Attributes Name Number of hours the waiter

worked Rate the waiter earns per hour

and

Behaviour Provide his/her name Provide the number of hours worked Provide the rate per hour Calculate and provide total wage

The TWaiter class definition will be stored in the clsWaiter_u unit. (You choose the name of the unit yourself.)

Page 27: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

How to instantiate and use an object based on a TWaiter class

1.

2.

3.

4.

5. Grade 12Chapter 1Activity 1.2

Page 28: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.
Page 29: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

An object contains attributes and behaviour

• A 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).

Re-confirming concepts

Page 30: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

BehaviourAtt

ribut

es

Page 31: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

frmMain

frmHideCache frmSwop frmSearch

Prepare for concepts

Page 32: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

Use OOP principlesMethods are part of the class of the Form.

Methods work with attributes (data) of the Form.

Page 33: IT is gr8! A workshop for IT teachers 2015. Preparing for programming concepts Data types Arrays (lists) OOP Classes and objects Attributes and behaviour.

Do NOT use procedural principles

Subroutines work with a variable which has unit scope.

Subroutines - not methods.