Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSE:141 Introduction to Programming Faculty of Computer Science, IBA BS-I (Spring 2010) Lecture 3."— Presentation transcript:

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

2 Lesson Plan Introduction to JAVA Algorithm using flow charts and Pseudocode. 2/13/20102Quratulain

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. Quratulain 2/13/20103

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. Quratulain 2/13/20104

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. Quratulain 2/13/20105

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. Quratulain 2/13/20106

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. Quratulain 2/13/20107

8 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 compilervia linker SOURCEOBJECT EXECUTABLE myprog.cppmyprog.objmyprog.exe

9 9 Java Portability Windows PC running JVM Windows PC running JVM Java Program Java Program via compiler via interpreter JVM SOURCEBYTECODE EXECUTABLES Payroll.javaPayroll.class Unix box running JVM Unix box running JVM Macintosh running JVM Macintosh running JVM Java Bytecode Java Bytecode

10 Java Program Data for Java Program Java Compiler Byte-Code Program Byte-Code Interpreter Machine-Language Instructions Computer Execution of Machine-Language Instructions Output of Java Program Java Virtual Machine Linker Previously Compiled Helper Programs Java Program Translation Including Linker 2/13/201010Quratulain

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/201011Quratulain

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/201012Quratulain

13 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/2010Quratulain

14 14 SEQUENCE Statement... 2/13/2010Quratulain

15 15 SELECTION (branch) IF Condition THEN Statement1 ELSE Statement2 Statement1 Statement Statement2 Condition... True False 2/13/2010Quratulain

16 16 LOOP (repetition) Statement... False True WHILE Condition DO Statement1 Condition 2/13/2010Quratulain

17 17 SUBPROGRAM (function) SUBPROGRAM1... SUBPROGRAM1 a meaningful collection of SEQUENCE, SELECTION, LOOP, SUBPROGRAM 2/13/2010Quratulain

18 18 ASYNCHRONOUS CONTROL EVENT EVENTHANDLER a subprogram executed when an event occurs 2/13/2010Quratulain

19 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/2010Quratulain

20 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/2010Quratulain

21 21 An Object of class Time Set Increment Write. Time OPERATIONS DATA Private data: hrs 8 mins 25 secs 42 2/13/2010Quratulain

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/2010Quratulain22

23 Flow Chart Symbols Common symbols used in flowcharts 2/13/2010Quratulain23

24 Example A flowchart and equivalent Pseudocode to compute the interest on a loan 2/13/2010Quratulain24 Read NAME, BALANCE, RATE Compute INTEREST as BALANCE x RATE Write (Display) NAME and INTEREST Flow Chart Pseudocode

25 Example 2 The program computes the sum, average and product of three numbers. 2/13/2010Quratulain25 Read X, Y, Z Compute Sum (S) as X + Y + Z Compute Average (A) as S / 3 Compute Product (P) as X x Y x Z Write (Display) the Sum, Average and Product

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/2010Quratulain26 Condition (Question) "Answer" Is A == B? No Is B > A? Yes Is K <= 25? Yes Is SALES >= $5000.00? Yes

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/2010Quratulain27 Read A, B If A is less than B BIG = B SMALL = A else BIG = A SMALL = B Write (Display) BIG, SMALL

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/2010Quratulain28

29 Loop (Cond.) 2/13/2010Quratulain29 set count to zero set total to zero read number while ( not end-of-data ) increment count by 1 total = total + number read number if ( count > 0 ) then average = total / count display average set count to zero set total to zero do read a number increment count by 1 total = total + number while ( not end-of-data ) if ( count > 0 ) then average = total / count display average Pre TestPost Test


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

Similar presentations


Ads by Google