19
CSE:141 Introduction to Programming Faculty of Computer Science, IBA BS-I (Spring 2010) Lecture 2

CSE:141 Introduction to Programming

Embed Size (px)

DESCRIPTION

CSE:141 Introduction to Programming. Faculty of Computer Science, IBA BS-I (Spring 2010) Lecture 2. Lesson Plan. Introduction to computation Languages Program life cycle. What is computation?. Earlier computer were fixed program (Declarative) E.g Calculator - PowerPoint PPT Presentation

Citation preview

Page 1: CSE:141 Introduction to Programming

CSE:141 Introduction to Programming

Faculty of Computer Science, IBABS-I (Spring 2010)

Lecture 2

Page 2: CSE:141 Introduction to Programming

Quratulain 2

Lesson Plan

• Introduction to computation• Languages• Program life cycle

2/3/2010

Page 3: CSE:141 Introduction to Programming

Quratulain 3

What is computation?

• Earlier computer were fixed program (Declarative)– E.g Calculator

• Now, stored program computers(Imperative)– E.g Computers

2/3/2010

Page 4: CSE:141 Introduction to Programming

Quratulain 4

Computer System

2/3/2010

Page 5: CSE:141 Introduction to Programming

Quratulain 5

Memory Addressing• The capacity (size) of memory is described in

terms of number of bytes.• RAM capacities in a typical computer range

from 512 MB (megabyte) to 3 GB (gigabyte).• RAM is volatile – data is lost when power is

turned off.• Computers don't understand the alphabet.

They only understand 0’s and 1’s. • So computers map each alphabet character

to a series of sixteen 0's and 1's. For example, the letter E is 00000000 01000101.

• And each of the eight-bit groupings is a byte.

2/3/2010

Address Memory Contents

50,000

50,001

50,002

50,003

50,004

50,005

Page 6: CSE:141 Introduction to Programming

6

A Programming Language is . . .

• a language with strict grammatical rules, symbols, and special words used to construct a computer program

Page 7: CSE:141 Introduction to Programming

7

Code is . . .

• The product of translating an algorithm into a programming language

• Instructions for a computer that are written in a programming language

Page 8: CSE:141 Introduction to Programming

Quratulain 8

Languages

• High level vs Low level• General purpose vs Targeted• Interpreted vs Compiled• Synatx vs Semantics

2/3/2010

Page 9: CSE:141 Introduction to Programming

The high level vs low level

High-Level Language (HLL)– closest to natural language– words, numbers, and math

symbols– not directly understood by

hardware– “portable” source code

(hardware independent)– Java, C, C++, COBOL,

FORTRAN, BASIC, Lisp, Ada, etc.

Machine Language(lowest level)

– least natural language for humans, most natural language for hardware

– just 0s and 1s– directly understood by

hardware– not portable (hardware

dependent)

Page 10: CSE:141 Introduction to Programming

Programming Language Hierarchy

H ard w are

M ach in e L an g u ag e

A ssem b ly L an u ag e

H ig h -L eve l L an g u ag e (H L L )

Page 11: CSE:141 Introduction to Programming

Quratulain 11

General purpose vs Targeted Languages

• A "general purpose programming language" theoretically should be usable in multiple domains, but not specialized for any of them.

• A Targeted/Specific purpose programming language.

2/3/2010

Page 12: CSE:141 Introduction to Programming

Compilers vs. Assemblers vs. Interpreters

• Compilers and Assemblers– translation is a separate user step– translation is “off-line,” i.e. not at run time

• Interpreters - another way to translate source to object code– interpretation (from source to object code) is not a separate user step– translation is “on-line,” i.e. at run time

Compiler,

Assembler, or

Interpreter

SourceCode

ObjectCode

Page 13: CSE:141 Introduction to Programming

Quratulain 13

Synatx vs Semantics

• Semantics– Grammatical rules for assigning meaning to a

sentence. • Syntax

– Grammatical rules for specifying correct word order and inflectional structure in a sentence.

• Example– “Baby milk drinks“ not correct syntactically but

can extract meaning “Baby drinks milk”

2/3/2010

Page 14: CSE:141 Introduction to Programming

14

Programming Shortcut?

THINKINGCODE

TEST

PROBLEM-SOLVING PHASE

IMPLEMENTATION PHASE

Shortcut?

Algorithm

Code

Problem

Page 15: CSE:141 Introduction to Programming

15

Programming Life Cycle

Problem-Solving Analysis and Specification General Solution ( Algorithm ) Verify

Implementation Concrete Solution ( Code ) Test

Maintenance Use Maintain

Page 16: CSE:141 Introduction to Programming

16

Sample Problem

• A programmer needs an algorithm to determine an employee’s weekly wages

• How would the calculations be done by hand?

Page 17: CSE:141 Introduction to Programming

17

One Employee’s Wages • During one week an employee works 52 hours at the hourly

pay rate $24.75• How much is the employee’s wages? • Assume a 40.0 hour normal work week• Assume an overtime pay rate factor of 1.5

40 x $ 24.75 = $ 990.00

12 x 1.5 x $ 24.75 = $ 445.50___________

$ 1435.50

Page 18: CSE:141 Introduction to Programming

18

If hours is over 40.0, thenwages = (40.0 * payRate) + (hours - 40.0) * 1.5 *payRate

otherwise, wages = hours * payRate

RECALL EXAMPLE ( 40 x $ 24.75 ) + ( 12 x 1.5 x $ 24.75 ) = $1435.50

Weekly Wages, in General

Page 19: CSE:141 Introduction to Programming

19

Employee’s Weekly Wages

Objects: Employee, pay rate, hours worked, wagesAlgorithm: 1. Get the employee’s hourly pay rate 2. Get the hours worked this week 3. Calculate this week’s regular wages 4. Calculate this week’s overtime wages (if any) 5. Add the regular wages to overtime wages (if any) to determine

total wages for the week