0% found this document useful (0 votes)
16 views

lecture 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

lecture 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Principles of Object Oriented

Programming (CS2012)
Lecture 1
Programming
• Is challenging
• Could be fun
• Could be stressful
• Comes in many forms
• Is an essential knowledge pillar
Have you Done Any Programming?
Let’s Start a Simple Activity
• Simulate your own game
• Read the hand out and suggest a suitable
name for the game
• Identify the entities in this simulation
• Fill the character sheet for your warrior
How can We Implement this in
Python?
• I.E., only using what you learnt at first
semester
• Let’s consider warriors only
name = ‘Frodo’
category = ‘warrior’
type = ‘normal’
Immortality = False
inventory = {‘stick’:1, ‘binoculars’:0}
How can We Implement this in
Python?
playerNames = [‘Frodo’, ‘Samwise’, ‘Meriadoc‘]
categories = [‘warrior’, ‘warrior’, ‘warrrior’]
playerTypes = [‘normal’, ‘super’, ‘normal’]
playerInventories = [{‘stick’:1, ‘binoculars’:0},
{‘stick’:1, ‘binoculars’:1}, {‘stick’:1,
‘binoculars’:0}]
Object Oriented Programming
• Objects can be used effectively to represent real-
world entities
– Warriors, monsters, trees, binoculars…..

• As another example, an object might represent a


particular employee in a company

• Each employee object handles the processing and


data management related to that employee

• Think of some other examples


Object Oriented Programming
• An object has:

– state - descriptive characteristics

– behaviors - what it can do (or what can be done to it)

• The state of a bank account includes its account number and


its current balance

• The behaviors associated with a bank account include the


ability to make deposits and withdrawals

• Note that the behavior of an object might change its state


Classes
• An object is defined by a class
• A class is the blueprint of an object
• Multiple objects can be created from the
same class
Objects vs Classes
A class An object
(the concept) (the realization)

Bank John’s Bank Account


Account Balance: $5,257

Bill’s Bank Account


Balance: $1,245,069
Multiple objects
from the same class
Mary’s Bank Account
Balance: $16,833
Attributes
• Contain current state of an object
• Attributes can be classified as simple or
complex.
• Simple attribute can be a primitive type such
as integer, string, etc., which takes on literal
values.
• Complex attribute can contain collections
and/or references.
Methods and Messages
Method: Defines behavior of an object.
Message: Request from one object to another
asking second object to execute one of its
methods.
Exercise
• Think of an object
– What is the class it belongs to?
– What are its attributes?
– What are its methods?
Identifying Objects/Classes
• Look for nouns in the problem description
– Could be misleading sometimes
• Look for interactions
• How to identify behavior?
– Look for verbs
When a teacher is conducting a class, he or she will show
lecture slides on the projector, write explanations on the
whiteboard and verbally discuss study material. The teacher
will also ask questions from the class and answer student
questions.
Object Oriented Programming is
OOP languages
• Python
• C++
• Java
• C#
Basic Java Program
public class MyFirstJavaProgram {
Student aStudent;

public static void main(String []args) {


aStudent = new Student(0131234);
int student_id_number = aStudent. getStudentIdNumber();
}
}
public class Student{
int student_id_number;
public Student(int student_id_number)
{
this.student_id_number = student_id_number;
}
public int getStudentIdNumber()
{
return student_id_number;
}
}

You might also like