Top Banner
© Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel . cech @ uhk . cz explorer.uhk.cz/pc/www
37

© Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove [email protected] explorer.uhk.cz /pc/www.

Mar 31, 2015

Download

Documents

Francesca Buzby
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: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

© Tomáš Kozel, Pavel Čech

Programming I

Pavel Čech Faculty of Informatics and Management

University Hradec [email protected]

explorer.uhk.cz/pc/www

Page 2: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

2

© Tomáš Kozel, Pavel Čech

Content

Introduction into programming

Object oriented modeling Object oriented programming Programming in Java

Page 3: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

3

© Tomáš Kozel, Pavel Čech

Objectives Introduce students to OOP in Java

Apply apply knowledge acquired in ALGD

Use advanced tools for application development

Page 4: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

4

© Tomáš Kozel, Pavel Čech

ResourcesMcConnell:Rapid Development – Microsoft

Press 1996McConnell:Code Complete – Microsoft

Press 1994

Electronic publications The Java Tutorial, Java SDK

Documentation - www.javasoft.com Thinking in Java - www.bruceeckel.com, 3.

edition

Page 5: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

5

© Tomáš Kozel, Pavel Čech

Tools Java 2 SDK – basic package for

programming in Java Eclipse – IDE for programming in JAVA ArgoUML – CASE Tool for object

oriented modeling in UML

These tools can be downloaded from> http://iris.uhk.cz/kozel/software (uid: student, pwd: prox)

All tools a free

Page 6: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

6

© Tomáš Kozel, Pavel Čech

Terminlogy Program = sequence of commands

describing some action Process = program being executed

(running) program Processor – device capable of

executing a program Data – certain kind of objects , that are

appropriately transformed by the program

Page 7: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

7

© Tomáš Kozel, Pavel Čech

Terminology (cont.) Source code – programming code written

in a certain programming language Target code – binary code, that is created

by compilation of source code – is executable

Byte code(Java) – intermediate code that is create by compilation but it is run by runtime environment (JRE, JVM)

Library – group of files that contain subroutines and classes and that can be used in our program

Page 8: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

8

© Tomáš Kozel, Pavel Čech

Terminology (cont.2) Data type – determines type and the size of

value in variables. Indirectly determines the set of operations that can be applied to the value.

Variable – “named” block of memory in RAM for storing values. The size of the memory block depends on the data type of the value.

Constant – the same as variable but it is not allowed to changed the value (it is read only). The constant can be assigned value only once and that can be only read.

Page 9: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

9

© Tomáš Kozel, Pavel Čech

Programming language the means of communication between

computer and the programmer the means of describing an algorithm needs to be understandable for both sides

Taxonomy Lower level programming lang. Higher level …

Page 10: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

10

© Tomáš Kozel, Pavel Čech

Types of prog. lang. Structured

Separation of data and operations Older by still commonly used In many cases faster and efficient C, Pascal, Basic,…

Object oriented Modern Easily maintainable The code can be re-used Eiffel, Smalltalk, Java, ...

Hybrid Not strictly object oriented i.e. can also be structured (C+

+, Object Pascal - Delphi, Visual Basic ...)

Page 11: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

11

© Tomáš Kozel, Pavel Čech

Compiler

=special kind of programs for conversion of source code into machine code

Types Interpreter Compiler

Page 12: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

12

© Tomáš Kozel, Pavel Čech

Interpretersourcecode

outputdata

Computer

Interpreter

inputdata

Page 13: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

13

© Tomáš Kozel, Pavel Čech

Compiler

developer’scomputer

source codeinput data

output data

user’s computer

compiler

target code

Page 14: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

14

© Tomáš Kozel, Pavel Čech

The process of compilationEditor

CompilerLinker

(links program)

OBJ coderelative code

 

Library

Target code

Source code

Debugger

Page 15: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

15

© Tomáš Kozel, Pavel Čech

Running program in JavaCompilation environment

Run-time environment

Source code (.java)

Java compiler

Byte code(.class)

Local or network

transmission of byte

code

Class Loader+

Verifier of byte code

Java libraries

JavaInterpreter

Just-In-Timecompiler

Runtime system

OS

HW

JavaVirtualMachine

Page 16: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

16

© Tomáš Kozel, Pavel Čech

What is object …

Objects real world - entities that can be

found everywhere

software – program entities that follow the rules of real world ones. By combination of sw. object we create object oriented programs (systems).

Page 17: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

17

© Tomáš Kozel, Pavel Čech

Real world objects

Have a name (object identity)

Have properties (state)

Do something (behavior)

Page 18: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

18

© Tomáš Kozel, Pavel Čech

Where can we find objects? Thinking about a situation We can write some notes with description

and processes in that situation. Noun can be objects. Verbs can be operations (behavior) of

objects Objects that are not important when

considering a given problem can be left out

Page 19: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

19

© Tomáš Kozel, Pavel Čech

Object oriented programs(Alan Kay)

1. Everything is object. Objects stores data and we can ask for some services (performing an operation).

2. Program is a group of objects that comunicates (send messages = ask for services).

3. Each object has its own memory space and can consists of other objects. Complex objects can be divided into other objects.

4. Every object belongs to a class. 5. Every object of the same class has to

understand the same messages and perform the same operations. Objects can belong into more classes through inheritance.

Page 20: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

20

© Tomáš Kozel, Pavel Čech

Why use objects in programs?

It is modern … They’re closer to

reality Readable source

code Advanced tools and

languages Reusable Easily maintainable

Page 21: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

21

© Tomáš Kozel, Pavel Čech

What objects can today?

Behave Have

properties Inherit from

ancestors Adapt Communicate …

Page 22: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

22

© Tomáš Kozel, Pavel Čech

Terminology revisited

Object always something particular with identity called also instance (Trabant, Audi A6, ...).

Class type of objects, a group in which a set of objects belong. Describes general features common to all objects (instances) in a class (Car).

Page 23: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

23

© Tomáš Kozel, Pavel Čech

UML .... tool ArgoUML

Unified Modeling Language Graphical language for object

modelingClass ObjectMyFord:CarCar

Color

HP

Go()

Brake()

Car attributes(data)

methods(operations)

Page 24: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

24

© Tomáš Kozel, Pavel Čech

Software objects

have name (identity), state, behavior

name is determined by identifier, state is described by attributes, behaviour is realized by methods

Page 25: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

25

© Tomáš Kozel, Pavel Čech

Basic OOP principles

Abstraction Encapsulation Inheritance Polymorphisms

• Communication Communication ((messagesmessages))

• AssociationAssociation• AggregationAggregation

((compositioncomposition))

Page 26: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

26

© Tomáš Kozel, Pavel Čech

Abstraction= separation of important and unimportant

aspects based on a given problemExample: Calculator from the point of

view of the studentImportant aspects:

Range and precision of numbers Number and types of operations Can be kept secret during the test

Unimportant: Number of chips on a system board Type of processor Exact algorithm of every operation

Page 27: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

27

© Tomáš Kozel, Pavel Čech

Encapsulation= data and operation forms a atomic whole that

cannot be separatedData (properties, state) of object and operations

are dependent and influence each other.Not all features of objects are propagated to

outside (Information hidding)Data – attribures of an objectOperation – methods of an object

Example: An angry man (state) is more noisy (behavior) than the calm one.

Page 28: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

28

© Tomáš Kozel, Pavel Čech

Information hidingEvery object can have private elements (attributes, methods) than cannot be seen or influence from the outside of the object (Information hiding). These elements are hidden behind so called public interface .Example: state and properties of a man‘s intestines

Publicinterface

Private state

„Doughnut“ diagram

Page 29: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

29

© Tomáš Kozel, Pavel Čech

Communication (messaging) Objects communicate by sending

messages. As a consequence of receiving a

message some operation is performed

(message passing = method invocation)

Page 30: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

30

© Tomáš Kozel, Pavel Čech

Association= represents a general binary relation

between two classes Each class in association has its role For each object with a role we can determine

multiplicity (cardinality) of that relationExample:Class Student is in association with the class

Faculty. The student „studies“ and the faculty is „studied by“.

Multiplicity: Many students (n or *) can study only one faculty.

Page 31: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

31

© Tomáš Kozel, Pavel Čech

Aggregation= special kind of association that

represents the relation „part of“Object can contain other objects. Such

objects are created by aggregating other objects. If the use of part objects is exclusive in aggregation we talk about composition. Example:

Composition: TV x ChipAggregation: Student x Lab

Page 32: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

32

© Tomáš Kozel, Pavel Čech

Association in UML

FacultyStudentstuduje 1..* 1

Association

Lab Student

TV Chip

Aggregation

Composition

Page 33: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

33

© Tomáš Kozel, Pavel Čech

Inheritance= ability of objects to inherit

attributes and behavior from ancestors (it is so called object evolution)

ie. that attributes and behavior can be further extended and modified

Example: Parent -> Children Machine -> Vehicle -> Car

Page 34: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

34

© Tomáš Kozel, Pavel Čech

Inheritance in UMLMachine

Vehicle

Car Truck

Page 35: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

35

© Tomáš Kozel, Pavel Čech

Class attributes

Attributes are common must be present for all objects of a particular class

State of object is given by values of attributes

Sometimes called data, properties

Example: Color, Brand, horse power

Page 36: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

36

© Tomáš Kozel, Pavel Čech

Class methods

Specify behavior Sometimes called operation or

services It is custom to hide attributes and

use so called access method to set or retrieve values

Access methods Selectors – reading/retrieving values Modifiers – setting/writing values

Page 37: © Tomáš Kozel, Pavel Čech Programming I Pavel Čech Faculty of Informatics and Management University Hradec Kralove pavel.cech@uhk.cz explorer.uhk.cz /pc/www.

37

© Tomáš Kozel, Pavel Čech

Information hiding

Rule: None of the attributes should be directly accessible from the outside world

->(Black Box)

Attributes

Method

Method

Method

Method