Chapter 8 High-Level Programming Languages. 2 Compilers High-level language A language that provides a richer (more English like) set of instructions.

Download Chapter 8 High-Level Programming Languages. 2 Compilers High-level language A language that provides a richer (more English like) set of instructions.

Post on 27-Dec-2015

216 views

Category:

Documents

3 download

TRANSCRIPT

  • Slide 1
  • Chapter 8 High-Level Programming Languages
  • Slide 2
  • 2 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
  • Slide 3
  • 3 Compilers Figure 8.1 Compilation process
  • Slide 4
  • 4 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
  • Slide 5
  • 5 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
  • Slide 6
  • 6 Portability The ability of a program to be run on different machines Compiler portability A program in a standardized language can be compiled and run on any machine that has the appropriate compiler Bytecode portability A program translated into Bytecode can be run on any machine that has a JVM
  • Slide 7
  • 7 Portability Figure 8.2 Portability provided by standardized languages versus interpretation by Bytecode
  • Slide 8
  • 8 Portability Figure 8.2 Portability provided by standardized languages versus interpretation by Bytecode
  • Slide 9
  • 9 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 mathematical functions LISP, Scheme (a derivative of LISP), and ML
  • Slide 10
  • 10 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
  • Slide 11
  • 11 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
  • Slide 12
  • 12 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
  • Slide 13
  • 13 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
  • Slide 14
  • 14 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
  • Slide 15
  • 15 Boolean Expressions < >= != or or /= = or ==
  • Slide 16
  • 16 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 (e.g. Integer, real, chars, boolean, strings) Strong typing The requirement that only a value of the proper type can be stored into a variable
  • Slide 17
  • 17 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
  • Slide 18
  • Declaration Example
  • Slide 19
  • 19 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
  • Slide 20
  • 20 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)
  • Slide 21
  • 21 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 a real in that order.
  • Slide 22
  • 22 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?
  • Slide 23
  • 23 Selection Statements Figure 8.3 Flow of control of if statement
  • Slide 24
  • 24 Selection Statements The if statement allows the program to test the state of the program variables using a Boolean expression
  • Slide 25
  • 25 Blocks Note the symbols used to indicate blocks in each language
  • Slide 26 50) Write "A little chilly: wear a light jacket" Else"> 50) Write "A little chilly: wear a light jacket" Else If (temperature > 32) Write "Philadelphia weather: wear a heavy coat" Else Write "Stay inside""> 50) Write "A little chilly: wear a light jacket" Else" title="26 Cascading Ifs If (temperature > 90) Write "Texas weather: wear shorts" Else If (temperature > 50) Write "A little chilly: wear a light jacket" Else">
  • 26 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" Else Write "Stay inside"
  • Slide 27
  • 27 Looping Statements Figure 8.4 Flow of control of while statement
  • Slide 28
  • 28 Looping Statements A count-controlled loop Set sum to 0 Set count to 1 While (count
  • 30 Looping Statements An event-controlled loop Set sum to 0 Set allPositive to true While (allPositive) Read number If (number > 0) Set sum to sum + number Else Set allPositive to false Write "Sum is " + sum Why is it called an event-conrolled loop? What is the event?
  • Slide 31
  • 31 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
  • Slide 32
  • 32 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
  • Slide 33
  • 33 Subprogram Statements Figure 8.5 Subprogram flow of control
  • Slide 34
  • 34 Subprogram Statements Figure 8.5 Subprogram flow of control
  • Slide 35
  • 35 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
  • Slide 36
  • 36 Subprogram Statements Think of arguments as being placed on a message board
  • Slide 37
  • 37 Subprogram Statements
  • Slide 38
  • 38 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
  • Slide 39
  • 39 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)! E.g. 5!=1*2*3*4*5=120 Base case Factorial(0) = 1 (0! is 1) General Case Factorial(N) = N * Factorial(N-1)
  • Slide 40
  • 40 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
  • Slide 41
  • 41 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
  • Slide 42
  • 42 Composite Data Types Declare Record
  • Slide 43
  • 43 Composite Data Types Declare record variable Use record variable
  • Slide 44
  • 44 Composite Data Types Figure 8.8 Array variable tenThings accessed from 0..9 An Array
  • Slide 45
  • 45 Composite Data Types Declare Array
  • Slide 46
  • 46 Composite Data Types Access an Array
  • Slide 47
  • 47 Functionality of Object- Oriented Languages: 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
  • Slide 48
  • 48 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
  • Slide 49
  • 49 Review Remember the pattern? From problem to general description to class definition to program
  • Slide 50
  • 50 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
  • Slide 51
  • 51 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
  • Slide 52
  • Inheritance and Polymorphism e.g. If a Dog is commanded to speak(), it may emit a bark, while if a Pig is asked to speak(), it may respond with an oink. Both inherit speak() from Animal, but their subclass methods override the methods of the superclass, known as overriding polymorphism. Adding a walk method to Animal would give both Pig and Dog objects the same walk method. 52
  • Slide 53
  • Inheritance and Polymorphism To inherit is to derive traits from preceding generations. In the object-oriented programming world, the term is associated with a kind of software reuse. With inheritance, new classes can be derived from existing classes, using the existing classes as building blocks. The new class inherits properties and methods from the base class. The new class can also add its own properties and methods. Inheritance can be understood from the following skeleton example. In this case a parent or a base class called BankAccount is declared as follows: 53
  • Slide 54
  • Inheritance example class BankAccount{ protected: char* Name; char* SSNum; char* AccountNum; float Balance; public: BankAccount (char* name, char* ssnum); void Deposit (float amount, char* accnum); void Withdraw(float amount, char* accnum); void PrintBalance(char* accnum); } 54
  • Slide 55
  • Inheritance example The base class BankAccount has four variables: Name: which stores the name of account holder, SSNum: the account holders social security number, AccountNum: the account number, and Balance: the account balance. The methods Deposit and Withdraw are used to make a deposit and withdrawal from the bank account. The PrintBalance method prints the balance in the account. The BankAccount class in itself is not sufficient to carry out all the transactions on the account. Generally, there are two types of accounts: the checking account, which facilitates day to day transactions, and the savings account, which accrues interest on the saved amount. 55
  • Slide 56
  • Inheritance example We therefore derive two subclasses that inherit from the above parent class. They are SavingsAccount and Checking Account. class SavingsAccount: public BankAccount{ private: float InterestRate; float MinimumBalance; } class CheckingAccount: public BankAccount{ private: float MonthlyFee; } The subclasses SavingsAccount and CheckingAccount inherit the properties of BankAccount. 56

Recommended

View more >