Chapter 8
description
Transcript of Chapter 8
![Page 1: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/1.jpg)
Chapter 8
High-Level Programming Languages
![Page 2: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/2.jpg)
2
Chapter Goals
• Describe the translation process and distinguish between assembly, compilation, interpretation, and execution
• Name four distinct programming paradigms and name a language characteristic of each
• Describe the following constructs: stream input and output, selection, looping, and subprograms
![Page 3: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/3.jpg)
3
Chapter Goals
• Construct Boolean expressions and describe how they are used to alter the flow of control of an algorithm
• Define the concepts of a data type and strong typing
• Explain the concept of a parameter and distinguish between value and reference parameters
![Page 4: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/4.jpg)
4
Chapter Goals
• Describe two composite data-structuring mechanisms
• Name, describe, and give examples of the three essential ingredients of an object-oriented language
![Page 5: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/5.jpg)
5
Compilers
High-level language
A language that provides a richer (more English like) set of instructions
Compiler
A program that translates a high-level language program into machine code
![Page 6: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/6.jpg)
6
Compilers
Figure 8.1 Compilation process
How does this differ fromthe assembly process?
![Page 7: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/7.jpg)
7
Interpreters
Interpreter
A translating program that translates and executes the statements in sequence
– Assembler or compiler produce machine code as output, which is then executed in a separate step
– An interpreter translates a statement and then immediately executes the statement
– Interpreters can be viewed as simulators
![Page 8: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/8.jpg)
8
Java
• Introduced in 1996 and became instantly popular
• Portability was of primary importance
• Java is compiled into a standard machine language called Bytecode
• A software interpreter called the JVM (Java Virtual Machine) takes the Bytecode program and executes it
![Page 9: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/9.jpg)
9
Portability
PortabilityThe ability of a program to be run on different machines
Compiler portabilityA program in a standardized language can be compiled and run on any machine that has the appropriate compiler
Bytecode portabilityA program translated into Bytecode can be run on any machine that has a JVM
Do you understand the difference?
![Page 10: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/10.jpg)
10
Portability
Figure 8.2 Portability provided by standardized languages versus interpretation by Bytecode
![Page 11: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/11.jpg)
11
Portability
Figure 8.2 Portability provided by standardized languages versus interpretation by Bytecode
![Page 12: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/12.jpg)
12
Programming Language Paradigms
Imperative or procedural model– Program describes the processing– FORTRAN, COBOL, BASIC, C, Pascal,
Ada, and C++
Functional model– Program is written terms of functions, return
an answer.– LISP, Scheme (a derivative of LISP), and ML
![Page 13: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/13.jpg)
13
Programming Language Paradigms
Logic model– Program consists of facts about objects and rules that
question the relationships among facts– PROLOG
Object-oriented model– Program consists of a set of objects and the
interactions among the objects– Smalltalk and Simula – C++ is as an imperative language with some object-
oriented features– Java is an object-oriented language with some
imperative features
![Page 14: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/14.jpg)
14
Programming Language Paradigms
We examine procedural and object-oriented languages in the rest of this chapter by looking at the functionality provided in these languages
We give examples in different languages to show how syntax used to provide the functionality
![Page 15: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/15.jpg)
15
Functionality of Imperative Languages
Sequence
Executing statements in sequence until an instruction is encountered that changes this sequencing
Selection
Deciding which action to take
Iteration (looping)
Repeating an action
Do these concepts sound familiar?Let's review them.
![Page 16: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/16.jpg)
16
Boolean Expressions
Boolean expression
A sequence of identifiers, separated by compatible operators, that evaluates to true or false
A Boolean expression can be
– A Boolean variable
– An arithmetic expression followed by a relational operator followed by an arithmetic expression
– A Boolean expression followed by a Boolean operator followed by a Boolean expression
![Page 17: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/17.jpg)
17
Boolean Expressions
Variable
A location in memory that is referenced by an identifier that contains a data value
Thus, a Boolean variable is a location in memory that can contain either true or false
![Page 18: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/18.jpg)
18
Boolean Expressions
Remember the relational operators?List them!
![Page 19: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/19.jpg)
19
Strong Typing
Data type
A description of the set of values and the basic set of operations that can be applied to values of the type
Strong typing
The requirement that only a value of the proper type can be stored into a variable
vs. what?
![Page 20: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/20.jpg)
20
Data Types
Integer numbers
Real numbers
Characters
Boolean values
Strings
![Page 21: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/21.jpg)
21
Integers
What determines the range of an integer value?
Is the range of an integer value the same in all languages?
What operations can be applied to integers?
![Page 22: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/22.jpg)
22
Reals
How are real values like integer values?
How do real values differ from integer values?
![Page 23: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/23.jpg)
23
CharactersDo you remember
ASCII?
Extended ASCII?
UNICODE?
How many characters in Extended ASCII?
How many characters in UNICODE?
What does a relational operator between two characters mean?
Why are we discussing these again?
![Page 24: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/24.jpg)
24
Boolean and Strings
What values can a Boolean variable be?
For what are Boolean expressions used?
What is a string?
What operations can be applied to strings?
![Page 25: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/25.jpg)
25
Declarations
Declaration A statement that associates an identifier with a variable, an action, or some other entity within the language that can be given a name; the programmer can refer to that item by name
Reserved word A word in a language that has special meaning
Case-sensitive Uppercase and lowercase letters are considered the same
![Page 26: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/26.jpg)
Declaration Example
![Page 27: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/27.jpg)
27
Assignment statement
Assignment statement
An action statement (not a declaration) that says to evaluate the expression on the right-hand side of the symbol and store that value into the place named on the left-hand side
Named constant
A location in memory, referenced by an identifier, that contains a data value that cannot be changed
![Page 28: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/28.jpg)
28
Input/Output Structures
Pseudocode algorithms used the expressions Read or Get and Write or Print
High-level languages view input data as a stream of characters divided into lines
Key to the processing
The data type determines how characters are to be converted to a bit pattern (input) and how a bit pattern is to be converted to characters (output)
![Page 29: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/29.jpg)
29
Input/Output Structures
Read name, age, hourlyWage
name is a string; age is an integer; hourlyWage is a real
The data must be a string, an integer, and areal in that order.
![Page 30: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/30.jpg)
30
Control Structures
Control structure
An instruction that determines the order in which other instructions in a program are executed
Can you name the ones we defined in the functionality of pseudocode?
![Page 31: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/31.jpg)
31
Selection Statements
Figure 8.3 Flow of control of if statement
![Page 32: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/32.jpg)
32
Selection Statements
The if statement allows the program to test the state of the program variables using a Boolean expression
![Page 33: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/33.jpg)
33
Blocks
Notethe symbolsused toindicateblocksin eachlanguage
![Page 34: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/34.jpg)
34
Cascading Ifs
If (temperature > 90)Write "Texas weather: wear shorts"
Else If (temperature > 50)Write "A little chilly: wear a light jacket"
Else If (temperature > 32)Write "Philadelphia weather: wear a heavy coat"
ElseWrite "Stay inside"
Why does this work correctly?
![Page 35: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/35.jpg)
35
Looping Statements
Figure 8.4 Flow of control of while statement
![Page 36: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/36.jpg)
36
Looping Statements
A count-controlled loop
Set sum to 0Set count to 1While (count <= limit)
Read numberSet sum to sum + numberIncrement count
Write "Sum is " + sum
Why is itcalled acount-controlledloop?
![Page 37: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/37.jpg)
37
Looping Statements
![Page 38: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/38.jpg)
38
Looping Statements
An event-controlled loop
Set sum to 0Set allPositive to trueWhile (allPositive)
Read numberIf (number > 0)
Set sum to sum + numberElse
Set allPositive to falseWrite "Sum is " + sum
Why is it called anevent-conrolledloop? What is theevent?
![Page 39: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/39.jpg)
39
Subprogram Statements
We can give a section of code a name and use that name as a statement in another part of the program
When the name is encountered, the processing in the other part of the program halts while the named code is executed
![Page 40: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/40.jpg)
40
Subprogram Statements
What if the subprogram needs data from the calling unit?
Parameters
Identifiers listed in parentheses beside the subprogram declaration; sometimes called formal parameters
Arguments
Identifiers listed in parentheses on the subprogram call; sometimes called actual parameters
![Page 41: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/41.jpg)
41
Subprogram Statements
Figure 8.5 Subprogram flow of control
![Page 42: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/42.jpg)
42
Subprogram Statements
Figure 8.5 Subprogram flow of control
![Page 43: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/43.jpg)
43
Subprogram Statements
Value parameter
A parameter that expects a copy of its argument to be passed by the calling unit
Reference parameter
A parameter that expects the address of its argument to be passed by the calling unit
![Page 44: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/44.jpg)
44
Subprogram Statements
Think of arguments as being placed on a message board
![Page 45: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/45.jpg)
45
Subprogram Statements
![Page 46: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/46.jpg)
46
Recursion
Recursion
The ability of a subprogram to call itself
Base case
The case to which we have an answer
General case
The case that expresses the solution in terms of a call to itself with a smaller version of the problem
![Page 47: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/47.jpg)
47
Recursion
For example, the factorial of a number is defined as the number times the product of all the numbers between itself and 0:
N! = N * (N 1)!
Base case
Factorial(0) = 1 (0! is 1)
General Case
Factorial(N) = N * Factorial(N-1)
![Page 48: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/48.jpg)
48
Asynchronous Processing
Asynchronous processing
Not synchronized with the program's action
– Clicking has become a major form of input to the computer
– Mouse clicking is not within the sequence of the program
– A user can click a mouse at any time during the execution of a program
![Page 49: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/49.jpg)
49
Composite Data Types
Records
A named heterogeneous collection of items in which individual items are accessed by name
Arrays
A named homogeneous collection of items in which an individual item is accessed by its position (index) within the collection
![Page 50: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/50.jpg)
50
Composite Data Types
Declare Record
![Page 51: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/51.jpg)
51
Composite Data Types
Declare record variableUse record variable
![Page 52: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/52.jpg)
52
Composite Data Types
Figure 8.8 Array variable tenThings accessed from 0..9
An Array
![Page 53: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/53.jpg)
53
Composite Data Types
Declare Array
![Page 54: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/54.jpg)
54
Composite Data Types
Access an Array
![Page 55: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/55.jpg)
55
Functionality of Object-Oriented Languages
Remember the definitions from Chapter 6?
Encapsulate
Object class
Object in problem-solving phase
Object in implementation phase
Class in implementation phase
Instantiate
Don't worry!
We'll reviewthese concepts
![Page 56: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/56.jpg)
56
Review
Object class (problem-solving phase)
A description of a group of objects with similar properties and behaviors
Object (problem-solving phase)
An entity or thing that is relevant in the context of a problem
Instantiate
Create an object from a class
![Page 57: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/57.jpg)
57
Review
Encapsulation
A language feature that enforces information hiding (second definition)
Class (implementation phase)
A language construct that is a pattern for an object and provides a mechanism for encapsulating the properties and actions of the object class
Object (implementation phase)
An instance of a class
![Page 58: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/58.jpg)
58
Review
Rememberthepattern?
Fromproblemto generaldescriptionto classdefinition toprogram
![Page 59: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/59.jpg)
59
Inheritance and Polymorphism
Inheritance
A construct that fosters reuse by allowing an application to take an already-tested class and derive a class from it that inherits the properties the application needs
Polymorphism
The ability of a language to have duplicate method names in an inheritance hierarchy and to apply the method that is appropriate for the object to which the method is applied
![Page 60: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/60.jpg)
60
Inheritance and Polymorphism
Inheritance and polymorphism work together
How?
They combine to allow the programmer to build useful hierarchies of classes that can be put into a library to be reused in different applications
![Page 61: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/61.jpg)
61
Ethical Issues
Open-Source Software Development
What is open-source software?
What is proprietary software?
What are the arguments against
open-source software?
What are the arguments for open-
source software?
![Page 62: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/62.jpg)
62
Who am I?
I am best known for structured programming.Can you defineit?I am also knownfor my wit. Canyou recall someof my witty sayings?
![Page 63: Chapter 8](https://reader035.fdocuments.net/reader035/viewer/2022062500/56815097550346895dbe93c2/html5/thumbnails/63.jpg)
63
Do you know?
Was Bytecode the first interpreter for programcode?
What does the word "paradigm" mean? Howhas its meaning changed over time?
How many definitions can you think of for "bow"?