44
Introduction to Computer Science William Hsu Department of Computer Science and Engineering National Taiwan Ocean University

Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

Introduction to Computer Science

William HsuDepartment of Computer Science and EngineeringNational Taiwan Ocean University

Page 2: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

Chapter 6: Programming Languages“ If debugging is the process of removing software bugs, then programming must be the process of putting them in. ”

- Edsger Dijkstra

Page 3: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

› 6.1 Historical Perspective

› 6.2 Traditional Programming Concepts

› 6.3 Procedural Units

› 6.4 Language Implementation

› 6.5 Object Oriented Programming

› 6.6 Programming Concurrent Activities

› 6.7 Declarative Programming

Page 4: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

The Evolution of Programming Paradigms

2014/8/29 4

Page 5: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

A Function for Checkbook Balancing Constructed from Simpler Functions

2014/8/29 5

Page 6: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

The Composition of a Typical Imperative Program or Program Unit

2014/8/29 6

Page 7: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

› Integer: Whole numbers

› Real (float): Numbers with fractions

› Character: Symbols

› Boolean: True/false

Data Types

2014/8/29 7

Page 8: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

float Length, Width;

int Price, Total, Tax;

char Symbol;

int WeightLimit = 100;

Variables and Data types

2014/8/29 8

Page 9: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

• Conceptual shape or arrangement of data• A common data structure is the array

– In C

int Scores[2][9];

– In FORTRAN

INTEGER Scores(2,9)

Data Structure

2014/8/29 9

Page 10: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

A Two-dimensional Array with Two Rows and Nine Columns

2014/8/29 10

Page 11: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

The Conceptual Structure of the Aggregate type Employee

struct {

char Name[25];

int Age;

float SkillRating;

} Employee;

2014/8/29 11

Page 12: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

› In C, C++, C#, JavaZ = X + y;

› In AdaZ := X + y;

› In APL (A Programming Language)Z ← X + y

Assignment Statements

2014/8/29 12

Page 13: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

› Go to statementgoto 40

20 Evade()goto 70

40 if (KryptoniteLevel < LethalDose) then goto 60

goto 2060 RescueDamsel()70 ...

› As a single statementif (KryptoniteLevel < LethalDose):RescueDamsel()

else:Evade()

Control Statements

2014/8/29 13

Page 14: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

› If in Pythonif (condition):

statementAelse:

statementB

› In C, C++, C#, and Javaif (condition) statementA; else statementB;

› In AdaIF condition THEN

statementA; ELSE

statementB;END IF;

Control Statements (continued)

2014/8/29 14

Page 15: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

› While in Pythonwhile (condition):

body

› In C, C++, C#, and Javawhile (condition) {body

}

› In AdaWHILE condition LOOP

body END LOOP;

Control Statements (continued)

2014/8/29 15

Page 16: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

› Switch statement in C, C++, C#, and Javaswitch (variable) {

case 'A': statementA; break;case 'B': statementB; break;case 'C': statementC; break;default: statementD;

}

› In AdaCASE variable IS

WHEN 'A'=> statementA; WHEN 'B'=> statementB; WHEN 'C'=> statementC; WHEN OTHERS=> statementD;

END CASE;

Control Statements (continued)

2014/8/29 16

Page 17: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

The for Loop Structure and its Representation in C++, C#, and Java

2014/8/29 17

Page 18: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

› Explanatory statements within a program

› Helpful when a human reads a program

› Ignored by the compiler

/* This is a comment. */

// This is a comment

<!-- This is a comment -->

% This is a comment in LaTeX

Comments

2014/8/29 18

Page 19: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

› Many terms for this concept:– Subprogram, subroutine, procedure, method,

function.

› Unit begins with the function’s header.

› Local versus Global Variables.

› Formal versus Actual Parameters.

› Passing parameters by value versus reference.

Procedural Units

2014/8/29 19

Page 20: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

The Flow of Control Involving a Function

2014/8/29 20

CallerCallee

Page 21: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

The Function ProjectPopulationWritten in the Programming Language C

2014/8/29 21

Page 22: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

Executing the Function Demo and Passing Parameters by Value

2014/8/29 22

Page 23: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

Executing the Function Demo and Passing Parameters by Reference

2014/8/29 23

Page 24: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

The Fruitful Function CylinderVolumeWritten in the Programming Language C

2014/8/29 24

Page 25: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

The Translation Process

2014/8/29 25

Page 26: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

A Syntax Diagram of Python’s if-then-else Statement

2014/8/29 26

Page 27: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

Syntax Diagrams Describing the Structure of a Simple Algebraic Expression

2014/8/29 27

Page 28: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

The Parse Tree for the String x + y * z Based on the Syntax Diagrams

2014/8/29 28

Page 29: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

Two Distinct Parse Trees for the Statement:if B1 then if B2 then S1 else S2

2014/8/29 29

Page 30: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

An Object-oriented Approach to the Translation Process

2014/8/29 30

Page 31: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

› Object: Active program unit containing both data and procedures.

› Class: A template from which objects are constructed.

› An object is called an instance of the class.

Objects and Classes

2014/8/29 31

Page 32: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

The Structure of a Class Describing a Laser Weapon in a Computer Game

2014/8/29 32

Page 33: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

› Instance Variable: Variable within an object.– Holds information within the object.

› Method: Procedure within an object.– Describes the actions that the object can perform.

› Constructor: Special method used to initialize a new object when it is first constructed.

› Destructors: Special method used to de-initialize a existing object when it is removed.

Components of an Object

2014/8/29 33

Page 34: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

A Class with a Constructor

2014/8/29 34

Page 35: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

› Encapsulation: A way of restricting access to the internal components of an object.– Private – Public– (Protected)– (Friend)

Object Integrity

2014/8/29 35

Page 36: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

Our LaserClass Definition Using Encapsulation as it Would Appear in a Java or C# Program

2014/8/29 36

Page 37: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

› Inheritance: Allows new classes to be defined in terms of previously defined classes.

› Polymorphism: Allows method calls to be interpreted by the object that receives the call.

Additional Object-oriented Concepts

2014/8/29 37

Page 38: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

› Parallel (or concurrent) processing: simultaneous execution of multiple processes.– True concurrent processing requires multiple CPUs.

› Or called cores, threads.– Can be simulated using time-sharing with a single CPU.

Programming Concurrent Activities

2014/8/29 38

Page 39: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

Spawning Threads (Processes)

2014/8/29 39

Page 40: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

› Mutual Exclusion: A method for ensuring that data can be accessed by only one process at a time.

› Monitor: A data item augmented with the ability to control access to itself.

Controlling Access to Data

2014/8/29 40

Page 41: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

› Resolution: Combining two or more statements to produce a new statement (that is a logical consequence of the originals).– Example: (P OR Q) AND (R OR ¬Q)

resolves to (P OR R)

– Resolvent: A new statement deduced by resolution– Clause form: A statement whose elementary components are

connected by the Boolean operation OR.

› Unification: Assigning a value to a variable so that two statements become “compatible.”

Declarative Programming

2014/8/29 41

Page 42: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

Resolving the Statements (P OR Q) and (R OR ¬Q) to Produce (P OR R)

2014/8/29 42

Page 43: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

Resolving the Statements (P OR Q), (ROR ¬Q), ¬R, and ¬P

2014/8/29 43

Page 44: Department of Computer Science and Engineering National ... · › 6.1 Historical Perspective › 6.2 Traditional Programming Concepts › 6.3 Procedural Units › 6.4 Language Implementation

› Fact: A Prolog statement establishing a fact– Consists of a single predicate– Form: predicateName(arguments).

› Example: parent(bill, mary).

› Rule: A Prolog statement establishing a general rule– Form: conclusion :- premise.

› :- means “if”– Example: wise(X) :- old(X).

– Example: faster(X,Z) :- faster(X,Y), faster(Y,Z).

Prolog

2014/8/29 44