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

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

Embed Size (px)

Citation preview

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

CSE:141 Introduction to Programming

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

Lecture 3

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

Quratulain 2

Lesson Plan

• Introduction to JAVA

• Algorithm using flow charts and Pseudocode.

2/13/2010

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

3

History of Java • In the early 1990's, putting intelligence into home appliances was

thought to be the next "hot" technology.• Examples of intelligent home appliances:

– Coffee pots and lights that can be controlled by a computer's programs.– Televisions that can be controlled by an interactive television device's

programs.• Anticipating a strong market for such things, Sun Microsystems in 1991

funded a research project (code named Green) whose goal was to develop software for intelligent home appliances.

• An intelligent home appliance's intelligence comes from its embedded processor chips and the software that runs on the processor chips.

• Appliance processor chips change often because engineers continually find ways to make them smaller, less expensive, and more powerful.

• To handle the frequent turnover of new chips, appliance software must be extremely portable.

Quratulain2/13/2010

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

4

History of Java • Originally, Sun planned to use C++ for its home appliance software, but

they soon realized that C++ was less than ideal because it wasn't portable enough.

• Thus, rather than write C++ software and fight C++'s inherent deficiencies, Sun decided to develop a whole new programming language to handle its home appliance software needs.

• Their new language was originally named Oak (for the tree that was outside project leader James Gosling's window), but it was soon changed to Java.

• When the home appliance software work dried up, Java almost died before being released.

• Fortunately for Java, the World Wide Web exploded in popularity and Sun realized it could capitalize on that.

Quratulain2/13/2010

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

5

History of Java• Web pages have to be very portable because they can be downloaded

onto any type of computer.• What's the standard language used for Web pages?• Java programs are very portable and they're better than HTML in

terms of providing user interaction capabilities.• Java programs that are embedded in Web pages are called applets.• Although applets still play a significant role in Java's current success,

some of the other types of Java programs have surpassed applets in terms of popularity.

• In this course, we cover Standard Edition (SE) Java applications. They are Java programs that run on a standard computer – a desktop or a laptop, without the need of the Internet.

Quratulain2/13/2010

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

6

Portability• A piece of software is portable if it can be used on many different

types of computers.• Object code is not very portable. As you know, object code is

comprised of binary-format instructions. Those binary-format instructions are intimately tied to a particular type of computer. If you've got object code that was created on a type X computer, then the object code can run only on a type X computer.

• The Java solution to improve portability:– Java compilers don't compile all the way down to object code. Instead,

they compile down to bytecode, which possesses the best features of both object code and source code:• Like object code, bytecode uses a format that works closely with computer

hardware, so it runs fast.• Like source code, bytecode is generic, so it can be run on any type of

computer.

Quratulain2/13/2010

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

7

Java Virtual Machine

• How can bytecode be run on any type of computer?

• As a Java program’s bytecode runs, the bytecode is translated into object code by the computer's bytecode interpreter program. The bytecode interpreter program is known as the Java Virtual Machine, or JVM for short.

Quratulain2/13/2010

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

8

Three C++ Program Stages

other code from libraries,

etc.

other code from libraries,

etc.

written in machine language

written in machine language

written in machine language

written in machine language

written in C++

written in C++

via compiler via linker

SOURCE OBJECT EXECUTABLE

myprog.cpp myprog.obj myprog.exe

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

9

Java Portability

Windows PCrunning JVMWindows PCrunning JVM

Java Program

Java Program

via compiler via interpreter JVM

SOURCE BYTECODE

EXECUTABLES

Payroll.java Payroll.class

Unix boxrunning JVM

Unix boxrunning JVM

Macintoshrunning JVMMacintosh

running JVM

Java Bytecode

Java Bytecode

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

Quratulain 10

Java Program Data for Java Program

Java Compiler

Byte-CodeProgram

Byte-Code Interpreter

Machine-LanguageInstructions

Computer Executionof Machine-Language Instructions

Output of Java Program

JavaVirtual

Machine

Linker

Previously Compiled Helper Programs

Java Program Translation Including Linker

2/13/2010

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

Quratulain 11

Object-Oriented Programming: OOP

• A design and programming technique• Some terminology:– object - usually a person, place or thing (a noun)– method - an action performed by an object (a verb)– type or class - a category of similar objects (such as

automobiles)• Objects have both data and methods• Objects of the same class have the same data elements and

methods• Objects send and receive messages to invoke actions

2/13/2010

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

Quratulain 12

Example of an Object Class

Data Items:– manufacturer’s name– model name– year made– color– number of doors– size of engine– etc.

Methods:– Define data items (specify

manufacturer’s name, model, year, etc.)

– Change a data item (color, engine, etc.)

– Display data items– Calculate cost– etc.

Class: Automobile

2/13/2010

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

Quratulain 13

Basic Control Structures in languages

• A sequence is a series of statements that executes one after another

• Selection (branch) executes different statements depending on certain conditions

• Loop (repetition) repeats statements while certain conditions are met

• A subprogram breaks the program into smaller units • Asynchronous control handles events that originate outside our

program, such as button clicks.

2/13/2010

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

Quratulain 14

SEQUENCE

Statement Statement Statement . . .

2/13/2010

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

Quratulain 15

SELECTION (branch)

IF Condition THEN Statement1 ELSE Statement2

Statement1 Statement

Statement2

Condition . . .

True

False

2/13/2010

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

Quratulain 16

LOOP (repetition)

Statement

. . .False

True

WHILE Condition DO Statement1

Condition

2/13/2010

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

Quratulain 17

SUBPROGRAM (function)

SUBPROGRAM1 . . .

SUBPROGRAM1 a meaningful collection of SEQUENCE, SELECTION, LOOP, SUBPROGRAM

2/13/2010

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

Quratulain 18

ASYNCHRONOUS CONTROL

EVENT

EVENTHANDLER

a subprogram executed when an event occurs

2/13/2010

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

Quratulain 19

Object-Oriented Programming

• A data type is the specification in a programming language of how information is represented as data and the operations that can be preformed on the data

• An object is a collection of data values and associated operations

• A class is a description of one or more like objects

2/13/2010

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

Quratulain 20

More OOP Vocabulary

• Instantiation is the process of creating an object based on the description provided by a class

• A package is a collection of related classes

2/13/2010

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

Quratulain 21

An Object of class Time

Set

Increment

Write . . .

Time

OPERATIONS DATA

Private data:

hrs 8

mins 25

secs 42

2/13/2010

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

Quratulain 22

Tools to document Algorithms

• There are two commonly used tools to help to document program logic (the algorithm). These are:– Flowcharts and Pseudocode. – Generally, flowcharts work well for small

problems but Pseudocode is used for larger problems.

2/13/2010

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

Quratulain 23

Flow Chart Symbols

• Common symbols used in flowcharts

2/13/2010

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

Quratulain 24

Example

• A flowchart and equivalent Pseudocode to compute the interest on a loan

2/13/2010

Read NAME, BALANCE, RATECompute INTEREST as BALANCE x RATEWrite (Display) NAME and INTEREST

Flow ChartPseudocode

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

Quratulain 25

Example 2

• The program computes the sum, average and product of three numbers.

2/13/2010

Read X, Y, ZCompute Sum (S) as X + Y + ZCompute Average (A) as S / 3Compute Product (P) as X x Y x ZWrite (Display) the Sum, Average and Product

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

Quratulain 26

Decisions (Switching logic)

• A step in an algorithm that leads to more than one possible continuation is called a decision. It is consists of two components:– a condition and – a goto command depending on the result of the condition test

as follows.

2/13/2010

Condition (Question) "Answer"Is A == B? NoIs B > A? YesIs K <= 25? YesIs SALES >= $5000.00? Yes

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

Quratulain 27

Decision Example

• In flowcharting, the diamond-shaped symbol is used to indicate a decision.

• Shows the flowchart for a program that reads two numbers and displays the numbers read in decreasing order

2/13/2010

Read A, BIf A is less than BBIG = BSMALL = AelseBIG = ASMALL = BWrite (Display) BIG, SMALL

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

Quratulain 28

Loops• Most programs involve repeating

a series of instructions over and over until some event occurs.

For example If we wish to read ten numbers

and compute the average, we need a loop to count the number of numbers we have read.

2/13/2010

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

Quratulain 29

Loop (Cond.)

2/13/2010

set count to zeroset total to zeroread numberwhile ( not end-of-data ) increment count by 1 total = total + number read numberif ( count > 0 ) thenaverage = total / countdisplay average

set count to zeroset total to zerodo read a number increment count by 1 total = total + numberwhile ( not end-of-data )if ( count > 0 ) thenaverage = total / countdisplay average

Pre Test Post Test