web viewoop can basically be summoned up by: data before action. class. it’s the blueprint,...

20

Click here to load reader

Upload: lamtram

Post on 06-Feb-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Web viewOOP can basically be summoned up by: data before action. Class. It’s the blueprint, the definition, the description (describes what something is)

Object-Oriented Programming: Introduction

Focuses on the objects involved in a calculation For example you have a car:

Properties Make Model Color Year Price

Methods Start Drive Park

Events On_Start On_Parked On_Brake

Descriptive items are called properties. Actions you can perform are called methods (described using verbs usually) Things can that happen are called events – known as methods. OOP can basically be summoned up by: data before action.

Class

It’s the blueprint, the definition, the description (describes what something is) You use the class in order to represent an aspect of the code (eg: a restaurant,

review, user) They can also represent visual part of the program (eg: textbox, button, window) Classes can also represent invisible things (eg: date, time zone, video player)

What does a class define?

ATTRIBUTES (its characteristics) BEHAVIOR (what can it do?)Name WalkHeight RunWeight JumpGender Speak

Age Sleep

The attributes are the data, the variables. The behavior is what can you do, you functions. Attributes Properties Behavior Methods

The class is describing these in abstract; for example in the above information, it says that the person has a name, however, it does not tell us what the name is. The reasons for this is that the class is only the description of something

The class is the idea behind of something you want to create.

1

Page 2: Web viewOOP can basically be summoned up by: data before action. Class. It’s the blueprint, the definition, the description (describes what something is)

Object

The object is created from the class. One class can create multiple objects.

Encapsulation

This is the idea that classes are self-contained units They represent both the data and the code that works on that data We take our properties and our methods and we encapsulate them (box them up)

OOP: Syllabus notes

*OBJECTS AS A PROGRAMMING CONCEPT*

1.1 Outline the general nature of an object

An OBJECT contains:

1. VARIABLES = PROPERTIES 2. METHODS = BEHAVIORS

1.2 Distinguish between an object (definition, template or class) and instantiation

In Java, an object is an instance of a class. We can make lots of objects from the same class. Each object requires some memory to store its details. Each object has its own copy of its variables as well as its own copy of the class

methods. But, if the methods or variable are static, then there is only one copy of the method or

variable, and all the objects share the same copy.

1.3 Construct unified modelling language (UML) diagrams to represent object designs

UML diagrams show:

1. Class name2. Variable names3. Method signatures4. Dependencies

OOP Design Tutorial

UML Tutorial

UML is used to create software blueprints.

2

Page 3: Web viewOOP can basically be summoned up by: data before action. Class. It’s the blueprint, the definition, the description (describes what something is)

UML is a graphical way of describing software systems There are two types of UML; Sketching and Blueprints In UML Sketching you do not need to be specific – you just need to mark down the

main ideas, so you know what you need to accomplish in the creation of this system. UML Platform Independent Model: independent from any type of technology or any

specific language UML Platform Specific Model: specific to an execution environment or a chosen

programming language

1.4 Interpret UML diagrams

Library Diagram –

3

Page 4: Web viewOOP can basically be summoned up by: data before action. Class. It’s the blueprint, the definition, the description (describes what something is)

Shopping Diagram –

1.5 Describe the process of decomposition into several related objects

OOP Design Notes

1.6 Describe the relationships between objects for a given problem

Aggregation, Association, Composition Video

Example using busses:

- Dependency = a bus USES roads (depends on)- Aggregation = a bus HAS seats (contains)- Inheritance = a bus IS A vehicle (extends), and has an engine like all vehicles.

An example using students:

- Dependency = a student USES a school- Aggregation = a student HAS hands- Inheritance = a student IS A person, and hence has a name

1.7 Outline the need to reduce dependencies between objects in a given problem

It is desirable for objects (classes) to be “self-contained” This is partially achieved through encapsulation – that means putting all the data and

methods inside the object. If we depend on another class to provide useful methods, then we must be sure to

have the class/object available whenever we use our object. For example, if we have an Employee Object containing name, phone etc about the

employee, the program will need to save the data in a disk file. If we include the save method inside the Employee Object, then it is simpler then it is simpler to use than

4

Page 5: Web viewOOP can basically be summoned up by: data before action. Class. It’s the blueprint, the definition, the description (describes what something is)

expecting some external methods to take care of saving data. If some other programmer changes the save method that is somewhere else, we cannot be sure that it will work well.

This is related to choosing where responsibilities are placed in a computer system.

1.8 Construct related objects for a given problem.

Scenario: a school student & staff database has to be developed with personal details of every person in the school. Because students and teachers both have common properties, a master class is useful from which both groups will inherit some of their properties.

Object people: this object stores the general details of the people in the school, like gender, age, phone number and home address.

Object students: this object inherits all the properties of the people class and adds some more properties specific to students, like grade, marks for subjects and days missed.

Object teachers: this object inherits all the properties of the people class and also some more properties specific to teachers, like homeroom, days sick, salary and subjects teached.

In this scenario, only objects of teachers and students would be instantiated, but because they inherit from the people class, they will possess properties of this class. People is now a superclass while teachers and students are a subclass.

1.9 Explain the need for different data types to represent data items.

Integer: whole numbers (Java int) Real: floating point, decimals (Java double or float) String: text data, characters (Java String) Boolean: true/false (Java Boolean) It is actually possible to store numeric values in String variables, but this is

inefficient. Adding up to 1 million numbers would then require 1 million conversions from String to double.

1.9 Describe how data items can be passed to and from actions as parameters

Pass-By-Value means that the value is copied from the main program into the parameter(s). It the value of the parameter is change inside the method, that change does NOT affect the original values in the main program.

A Pass-By-Value (or reference) parameter does NOT have its value copied into the parameter, but rather the memory location (reference/pointer) is copied. That means the method CAN change the values in the variables in the main program.

5

Page 6: Web viewOOP can basically be summoned up by: data before action. Class. It’s the blueprint, the definition, the description (describes what something is)

*FEATURES OF OOP*

2.1 Define the term encapsulation

Encapsulation: data and actions are limited to the object in which they are defined.

This is accomplished by:

- Putting all DATA (properties/variables) and all METHODS inside the object/class- Using PRIVATE variables, accessed through public GET- and SET- methods

2.2 Define the term inheritance

Inheritance: a parent object holds common data and actions for a group of related child objects.

This involves:

- Making an original object/class- EXTENDING the class to create a new object/class with more properties and

methods- During inheritance, the new class can override the old properties and methods in the

original class

2.3 Define the term polymorphism

Polymorphism: actions have the same name but different parameter lists and processes.

- There can be several methods (different versions) with the same name. - If these are in the same class, then they must have different signatures (parameter

lists), so that the compiler can tell which version to use. - If the various versions are in different classes, then they could have the same

parameter lists and will be selected by the compiler according to the class currently in use.

2.4 Explain the advantages of encapsulation

Encapsulation ensures that:

1. Variables (properties) are not accidently changed by another part of the program.2. The correct methods are used, rather than accidently using another method with the

same name that is outside the class.

All these are particularly important in a team-programming project, where various parts of a computer system are created by many different people. Although good communication and documentation could prevent many accidents, encapsulation enforces restrictions at the compiler level.

6

Page 7: Web viewOOP can basically be summoned up by: data before action. Class. It’s the blueprint, the definition, the description (describes what something is)

2.5 Explain the advantages of inheritance

- This is especially useful in team-programming projects- It means that a parent (basic) object could be changed (improved) and those

changes would immediately be available in the child (extended) objects.

For example, if a better sorting algorithm is created in the parent object, it can immediately be used by the child objects. The compiler makes the improvements automatically, without the programmers passing around copies of the new sorting method.

2.6 Explain the advantages of polymorphism

For example, an action in a child object may choose to override actions of the parent object. This allows an external program to use the same action on a family of objects without knowing the implementation detail.

Consider a school database containing these objects:

- Student extends person (adding homeroom, bus route etc)- Teacher extends person (adding room, subject etc)

Now an END_OF_YEAR method can be constructed that sends an email to each student reminding them to turn in all their books.

A different END_OF_YEAR method for a teacher might send an email reminding them about important work they should do during the vacation (no work for the students)

It is possible to make a loop that goes through all the person objects, sending the appropriate email by using the END_OF_YEAR method in that object.

7

Page 8: Web viewOOP can basically be summoned up by: data before action. Class. It’s the blueprint, the definition, the description (describes what something is)

2.7 Describe the advantages of libraries of objects

For example, sorts and other complex algorithms and processes do not have to be “re-invented”.

A very simple example is all the GUI control Objects available from the javax.swing library.

We don't want every programmer to write their own code to draw a jButton and all its associated methods (like resize).

This library has been built, debugged and documented through great time and expense by the system programmers at SUN.

Another example would be a 3rd-party library that draws 2D and 3D mathematical graphs.  No need for every mathematics programmer to write all this code again.  

For example, the algorithm to draw a straight line from one point to another, choosing exactly the correct pixels, is a complex and difficult problem.

2.8 Describe the disadvantages of OOP

Increased complexity for small problems Unsuited for some kind of problems

2.9 Discuss the use of programming teams

Compared to working alone, programming teams can have many advantages:

1. People can work on many parts of a program at the same time, reducing time needed

2. Code can be cross-checked to avoid mistakes3. Because people don’t see other’s information, module dependencies are reduced

due to information hiding4. Expertise can be concentrated in a narrow field

2.10 Explain the advantages of modularity in program development

Modules can be tested and debugged separately Time reduced as work can be done on many modules simultaneously Modules can be easily changed, so easier maintenance Internal structure of modules can be changed without having to worry about the other

modules easier code optimism

*PROGRAM DEVELOPMENT*

3.1 Define the terms: class, identifier, primitive, instance variable, parameter variable, and local variable

Class: an abstract model for objects. It defines the properties and the methods of an object. It is like a data type for an object. For example, in Java strings are not fundamental variables like integers.

Identifier: a pointer that explicitly identifies an object. Generally it is the variable name.

8

Page 9: Web viewOOP can basically be summoned up by: data before action. Class. It’s the blueprint, the definition, the description (describes what something is)

Primitive: primitive data types are the ones involving the most basic entities in informatics: numbers. Other data types like arrays are abstract because they combine many primitive data types.

Instance variable: a variable in a class from which every instantiated object gets its own copy. If a class called people would have an instance variable called age, every object of this class would have its own variable called age.

Parameter variable: a variable that is passed along to function that is called to perform operations with. In Java it is the argument passed in the brackets with the function caller.

Local variable: a variable declared inside a function. This variable is only known and accessible by the function it is declared inside.

3.2 Define the terms: method, accessor, mutator, constructor, signature and return variable

Method: a method is a set of rules defining operations that are executed

Accessor: a method used to return values of a private variable of an object

Mutator: a mutator is a method used to control changes to the private variables of an object. Setter and accessor methods are mutator methods

Constructor: the method called when an object is instantiated

Signature: defines the input and outputs of a method. In some programming languages it even defines the errors thrown by the function.

Return value: specifies the data type that a function returns to its caller.

3.3 Define the terms: private, protected, public, extends and static

Private: a constructor that hides variables and methods from being accessed from outside the class

Protected: a member access modifier. A protected is only accessible within its own or derived class instances

Public: member can be accessed by any method that references its class

Extends: this keyword is used to state from which class the current class inherits its methods and properties

Static: declares a static member of a class that will be the same for all members

3.4 Describe the uses of the primitive data types and the reference class string

9

Page 10: Web viewOOP can basically be summoned up by: data before action. Class. It’s the blueprint, the definition, the description (describes what something is)

In examination questions the primitive types will be limited to int, long, double, char and Boolean.

Primitive data types are most commonly used to store simple values. They are also used as the building blocks of the more complex abstract data types.

3.5 Construct code examples to implement assessment statements

10

Page 11: Web viewOOP can basically be summoned up by: data before action. Class. It’s the blueprint, the definition, the description (describes what something is)

3.6 Construct code examples related to selection statements

3.7 Construct code examples related to repetition statements

3.8 Construct code examples related to static arrays

11

Page 12: Web viewOOP can basically be summoned up by: data before action. Class. It’s the blueprint, the definition, the description (describes what something is)

3.9 Discuss the features of modern programming languages that enable internalization

Use of common character sets among many platforms and languages, like UNICODE

Platform independent high level languages enable code to run on many platforms

3.10 Discuss the ethical and moral obligations of programmers

Adequate testing of products to prevent possibilities of commercial or more other damage

Acknowledging the work of other programmers (plagiarism) Open Source movement

*HL EXTENSION – ADVANCED PROGRAM DEVELOPMENT*

4.1 Define the term recursion

Recursion: a method that invokes (calls) itself. That means the method will contain its own name as one of the commands.

4.2 Describe the application of recursive algorithms

A TREE is the standard example of a need for recursion. At each node in the TREE, the nodes below it form a SUB-TREE. Then the algorithm

starts over at the current node and recurses through the subtree. Afterward, it must RETURN to the parent node and complete the previous copy of the algorithm.

In the real world of business programming, most tasks can be completed ITERA-TIVELY (using loops), and they don't require recursion.  

LISTS are LINEAR structures.   2D arrays are effectively LINEAR, although they don't appear that way at first. But we

can TRAVERSE a 2D array using NESTED LOOPS. Most standard business problems can be treated effectively as a 2D array (as used

in Excel). IF we need to TRAVERSE an array by following NEIGHBORS (e.g. in MineSweeper

to count neighbors) then a RECURSIVE method may still be required.  Such prob-lems cannot be programmed with nested loops.

4.3 Construct algorithms that use recursion

This is limited to a method that returns no more than one result and contains either one or two recursive calls (a link between computational thinking program design) [can be practiced through Tree Traversals in order, pre order and post order]

4.4 Trace recursive algorithms

12

Page 13: Web viewOOP can basically be summoned up by: data before action. Class. It’s the blueprint, the definition, the description (describes what something is)

The TRACE usually looks like a tree, with each new recursive call creating a new ode in the tree.

4.5 Define the term object reference

A Linked-List or a TREE contain NODE objects that point to other NODE objects so the pointers (next, leftChild, rightChild) will be self-referential

Eg: class TreeNode{  String data;  TreeNode leftChild;  TreeNode rightChild;}

4.6 Construct algorithms that use reference mechanism

In exams, this is only going to involve single pointers, not binary trees.

4.7 Identify the features of the abstract data type (ADT) list

An ADT (Abstract Data Type) "hides" it's complexity inside the object/class. The programmer should be able to use the ADT successfully without knowing exactly

HOW it does what it does. Specific examples are: LinkedList and ArrayList (same as Vector).

4.8 Describe applications of lists

Queue - waiting lines, printer queue (server)Stack - return values from methods, undo lists in applications,           history in a browserThese can be implemented in an ArrayList, by adding and removing from the appropriate end(s)

4.9 Construct algorithms using a static implementation of a list

A STATIC implementation of a Linked-List means it is stored in an Array, together with ap-propriate "pointers" like FIRST and LAST. Then there are actually no "links", but similar fea-tures must be provided, e.g.:- addAtTail- addAtHead- insert- delete- list (show all items)- isEmpty (checkEmpty) and isFull (checkFull)4.10 Construct list algorithms using object references

13

Page 14: Web viewOOP can basically be summoned up by: data before action. Class. It’s the blueprint, the definition, the description (describes what something is)

This is a DYNAMIC implementation, with a NODE class and a LinkedList ADT, containing appropriate methods for adding, inserting and deleting NODES at specific locations.

4.11 Construct algorithms using the standard library collections included in JETS

The classes are ArrayList and LinkedList.

e.g. use these as ADTs.  Don't need to do any programming inside of these - just use the standard features.

4.12 Trace algorithms using the implementations described in assessment statements

In examination questions, definitions of ArrayList and LinkedList methods will be given where necessary.

4.13 Explain the advantages of using library collections

Specifically for ArrayList and LinkedList – 1. It is very easy to make a programming mistake when dealing with references (point-

ers).  2. Libraries have been thoroughly debugged and are probably more reliable than the

code that normal programmers could write. This also saves lots of time, and is sensi-ble when implementing STANDARD algorithms, like searching and sorting.

4.14 Outline the features of ADT’s stack, queue and binary tree

Stack = LIFO, using PUSH and POP

Queue = FIFO, using ENQUEUE and DEQUEUE

Binary Tree = using LEFTCHILD, RIGHTCHILD and traversals: in order, pre order and post order.

Store and retrieve unique "keys" - like words for a spell-check algorithm

4.15 Explain the importance of style and naming conventions in codeTwo main reasons for good style:(1) Enables someone else to read and understand your    code more easily and successfully(2) Enables you or anyone else to make changes and    improvements later

Good style includes:- Clear and consistent NAMING CONVENTION- Clear and consistent SPELLING (like camelCaps)- Clear and consistent INDENTATION- Lots of COMMENTS

14