+ All Categories
Home > Documents > Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to...

Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to...

Date post: 04-Jan-2016
Category:
Upload: egbert-mckinney
View: 216 times
Download: 0 times
Share this document with a friend
44
opyright © Prentice Hall 2000 Programming and Languages Chapter 14 Chapter 14 Telling the Computer What to Telling the Computer What to Do Do
Transcript
Page 1: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 1

Programming and Languages

Chapter 14Chapter 14

Telling the Computer What to DoTelling the Computer What to Do

Page 2: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 2

Programs

A program is a set of A program is a set of step-by-step step-by-step instructions that instructions that directs the computer directs the computer to do the tasks you to do the tasks you want it to do and want it to do and produce the results produce the results you want.you want.

Page 3: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 3

Programming Languages

A programming A programming language is a set language is a set of rules that of rules that provides a way of provides a way of telling a computer telling a computer what operations what operations to perform.to perform.

Page 4: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 4

Programmers

A programmer’s job is to A programmer’s job is to convert problem solutions convert problem solutions into instructions for the into instructions for the computer.computer.

If the project is large, they may If the project is large, they may also need to coordinate the also need to coordinate the needs of users, managers, needs of users, managers, and systems managers.and systems managers.

Page 5: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 5

The Programming Process

The steps involved in developing a The steps involved in developing a program include:program include:

• Define the problemDefine the problem• Plan the solutionPlan the solution• Code the programCode the program• Test the programTest the program• Document the programDocument the program

Page 6: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 6

Define the Problem

The task of defining The task of defining the problem the problem consists of consists of identifying what it identifying what it is you know and is you know and what it is you what it is you want to obtain.want to obtain.

Page 7: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 7

Plan the Solution

Planning the Planning the solution may solution may involve drawing involve drawing a flowchart or a flowchart or writing writing pseudocode, or pseudocode, or both.both.

Page 8: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 8

Flowcharts

A flowchart is a A flowchart is a pictorial pictorial representation representation of a step-by-step of a step-by-step solution to a solution to a problem.problem.

Page 9: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 9

Flowchart Basics

A flowchart consists A flowchart consists of arrows to of arrows to represent direction represent direction the program takes the program takes and boxes and and boxes and symbols to symbols to represent actions.represent actions.

Page 10: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 10

Flowchart Symbols

Process Start/Stop

Input/Output

Decision

Connector

Flow direction

Page 11: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 11

Pseudocode

Pseudocode is an English-like Pseudocode is an English-like nonstandard language. nonstandard language.

It allows programmers to focus on It allows programmers to focus on the program logic without being the program logic without being concerned about the particulars of concerned about the particulars of a formal programming language. a formal programming language.

Page 12: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 12

Code the Program

Coding a program means to Coding a program means to translate the logic from a flowchart translate the logic from a flowchart of pseudocode into a programming of pseudocode into a programming language.language.

The rules of computer languages are The rules of computer languages are called syntax. These rules must be called syntax. These rules must be followed precisely.followed precisely.

Page 13: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 13

Test the Program

After coding, tests must be performed After coding, tests must be performed to make sure the steps are correct to make sure the steps are correct and render the desired results.and render the desired results.

Testing includes:Testing includes:• Desk-checkingDesk-checking• TranslatingTranslating• DebuggingDebugging

Page 14: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 14

Desk-checking

This form of testing This form of testing involves mentally involves mentally checking the checking the logic of the logic of the program to program to ensure that it is ensure that it is error-free and error-free and workable.workable.

Page 15: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 15

Translating

Programs are commonly Programs are commonly translated by a compiler translated by a compiler which checks for syntax which checks for syntax errors and converts the errors and converts the program into a form the program into a form the computer understands.computer understands.

Page 16: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 16

Debugging

Debugging means to Debugging means to detect, locate, and detect, locate, and correct mistakes in correct mistakes in the program.the program.

To find errors, you To find errors, you must test every part must test every part of the program under of the program under various conditions. various conditions.

Page 17: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 17

Document the Program

A programmer must A programmer must document the document the various stages the various stages the program has gone program has gone through as well as through as well as other specific facts other specific facts about the about the program. program.

Page 18: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 18

Levels ofProgramming Languages

There are several levels (or There are several levels (or “generations”) of programming “generations”) of programming languages:languages:

• MachineMachine• AssemblyAssembly• High-levelHigh-level• Very high-levelVery high-level• NaturalNatural

Page 19: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 19

First Generation:Machine Language

This is the lowest level of This is the lowest level of programming language because it programming language because it represents data and program represents data and program instructions as 0s and 1s.instructions as 0s and 1s.

All programming languages are All programming languages are eventually converted into machine eventually converted into machine language.language.

Page 20: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 20

Second Generation:Assembly Language

Assembly languages replace 0s and Assembly languages replace 0s and 1s with mnemonic codes.1s with mnemonic codes.

Since machine language is the only Since machine language is the only language the computer can language the computer can execute, assembly is eventually execute, assembly is eventually translated into machine language translated into machine language to execute the program.to execute the program.

Page 21: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 21

Third Generation:High-Level Language

High-level languages use English-like High-level languages use English-like words that are much easier for words that are much easier for humans to understand.humans to understand.

A translator is needed to convert the A translator is needed to convert the high-level language into machine high-level language into machine language that computers language that computers understand.understand.

Page 22: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 22

Fourth Generation:Very High-Level Language

This generation of language is often This generation of language is often known as 4GLs.known as 4GLs.

4GLs are a shorthand programming 4GLs are a shorthand programming language that is about 10 times language that is about 10 times more productive than third more productive than third generation languages.generation languages.

Page 23: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 23

Fifth Generation:Natural Language

This generation of This generation of programming languages programming languages more resembles more resembles “natural” spoken English.“natural” spoken English.

The user communicates The user communicates with the computer by with the computer by speaking.speaking.

Page 24: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 24

Major Programming Languages

There are several languages with There are several languages with which to write your program:which to write your program:

• FORTRANFORTRAN• COBOLCOBOL• BASICBASIC• PascalPascal

• CC• JavaJava• Object-OrientedObject-Oriented

Page 25: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 25

FORTRAN

FORTRAN is a FORTRAN is a scientifically scientifically oriented language oriented language designed to execute designed to execute complex formulas as complex formulas as in economic analysis in economic analysis and engineering.and engineering.

Page 26: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 26

COBOL

COBOL was designed COBOL was designed for business needs for business needs such as processing such as processing large files and large files and performing business performing business calculations.calculations.

Page 27: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 27

BASIC

This programming This programming language is designed language is designed to be easy to learn.to be easy to learn.

BASIC can be used for BASIC can be used for business applications business applications and for use with and for use with personal computers.personal computers.

Page 28: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 28

Pascal

Pascal was Pascal was developed as a developed as a teaching language teaching language for computer for computer students.students.

Page 29: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 29

C

C was originally C was originally developed to write developed to write systems software but systems software but is now considered a is now considered a general-purpose general-purpose language.language.

Page 30: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 30

Java

Java is a network-Java is a network-friendly friendly programming programming language that language that permits a piece of permits a piece of software to run on software to run on many different many different platforms.platforms.

Page 31: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 31

Object-Oriented

Object-oriented Object-oriented programming is programming is based on objects based on objects and their and their attributes.attributes.

Page 32: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 32

Object Classes

An object is a self-contained unit An object is a self-contained unit that contains both data and that contains both data and related facts and functions.related facts and functions.

Objects are arranged hierarchically Objects are arranged hierarchically in classes and subclasses based on in classes and subclasses based on their dominant characteristics.their dominant characteristics.

Page 33: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 33

Attributes and Methods

Facts related to an object are called Facts related to an object are called attributesattributes..

MethodsMethods are the instructions that tell are the instructions that tell the data what to do.the data what to do.

Page 34: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 34

Object Inheritance

An object in a subclass automatically An object in a subclass automatically possesses all the characteristics of possesses all the characteristics of the class from which it is derived.the class from which it is derived.

Page 35: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 35

Object-Oriented Languages

There is an object-oriented version of There is an object-oriented version of C, called C, called C++C++. Versions of C++ are . Versions of C++ are available for large and personal available for large and personal computers.computers.

SmalltalkSmalltalk supports a visual system of supports a visual system of programming. Text is entered with a programming. Text is entered with a keyboard but all other interaction keyboard but all other interaction takes place with mouse and icons.takes place with mouse and icons.

Page 36: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 36

Advantages of Object-Oriented Programming

In a programming environment, a In a programming environment, a programmer does not have to programmer does not have to repeat the instructions for repeat the instructions for “inherited” characteristics.“inherited” characteristics.

This means a savings in time and This means a savings in time and money.money.

Page 37: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 37

Sample Programs

The following five slides The following five slides demonstrate the syntax and demonstrate the syntax and structure of high-level structure of high-level programming languages.programming languages.

Only segments of an entire program Only segments of an entire program are shown.are shown.

Can you “guess” what each line in Can you “guess” what each line in the program will do?the program will do?

Page 38: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 38

Fortran

……

WRITE (6,10)WRITE (6,10)

SUM=0SUM=0

COUNTER = 0COUNTER = 0

WRITE (6,60)WRITE (6,60)

READ (5,40) NUMBERREAD (5,40) NUMBER

11 IF (NUMBER .EQ. 999) GOTO 2IF (NUMBER .EQ. 999) GOTO 2

SUM = SUM + NUMBERSUM = SUM + NUMBER

GOTO 1GOTO 1

……

Page 39: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 39

COBOL

……

300-INITIALIZATION-ROUTINE.300-INITIALIZATION-ROUTINE.

DISPLAY “PLEASE ENTER A NUMBER”.DISPLAY “PLEASE ENTER A NUMBER”.

ACCEPT NUMBER-ITEM.ACCEPT NUMBER-ITEM.

400-ENTER-AND-ADD.400-ENTER-AND-ADD.

ADD NUMBER-ITEM TO SUM-ITEM.ADD NUMBER-ITEM TO SUM-ITEM.

ADD 1 TO COUNTER.ADD 1 TO COUNTER.

DISPLAY “PLEASE ENTER NEXT NUMBER”DISPLAY “PLEASE ENTER NEXT NUMBER”

……

Page 40: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 40

BASIC

……

SUM = 0SUM = 0

COUNTER = 0COUNTER = 0

PRINT “PLEASE ENTER A NUMBER”PRINT “PLEASE ENTER A NUMBER”

INPUT NUMBERINPUT NUMBER

DO WHILE NUMBER <> 999DO WHILE NUMBER <> 999

SUM = SUM + NUMBERSUM = SUM + NUMBER

COUNTER = COUNTER + 1COUNTER = COUNTER + 1

……

Page 41: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 41

Pascal

……

BEGINBEGIN

sum :=0;sum :=0;

counter :=0;counter :=0;

WRITELN (‘PLEASE ENTER A NUMBER’);WRITELN (‘PLEASE ENTER A NUMBER’);

READLN (number);READLN (number);

WHILE number <> 999 DOWHILE number <> 999 DO

Begin (*while loop*)Begin (*while loop*)

sum := sum + number;sum := sum + number;

……

Page 42: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 42

C++

……

cout << “PLEASE ENTER A NUMBER”;cout << “PLEASE ENTER A NUMBER”;

cin>> number;cin>> number;

while (number !=999)while (number !=999)

{{

sum := sum + number;sum := sum + number;

counter ++;counter ++;

cout <<“\nPlease enter the next number”;cout <<“\nPlease enter the next number”;

……

Page 43: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 43

Learning OOP

• Read booksRead books• Use the tutorialsUse the tutorials• Study sample codeStudy sample code• Write OO codeWrite OO code• Start smallStart small• Use the help fileUse the help file• Use your time productivelyUse your time productively

Page 44: Copyright © Prentice Hall 20001 Programming and Languages Chapter 14 Telling the Computer What to Do.

Copyright © Prentice Hall 2000 44

Conclusion

Programming languages have Programming languages have evolved along with the hardware evolved along with the hardware they instruct.they instruct.

In addition, there is a variety of In addition, there is a variety of programming languages designed programming languages designed to fit the problem the programmer to fit the problem the programmer is trying to solve.is trying to solve.


Recommended