290 likes | 429 Views
CSE:141 Introduction to Programming. Faculty of Computer Science, IBA BS-I (Spring 2010) Lecture 3. Lesson Plan. Introduction to JAVA Algorithm using flow charts and Pseudocode. History of Java .
E N D
CSE:141 Introduction to Programming Faculty of Computer Science, IBA BS-I (Spring 2010) Lecture 3
Lesson Plan • Introduction to JAVA • Algorithm using flow charts and Pseudocode. Quratulain
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
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
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
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
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
myprog.cpp myprog.obj myprog.exe SOURCE OBJECT EXECUTABLE written in C++ written in machine language written in machine language via compiler via linker other code from libraries, etc. Three C++ Program Stages
Java Program Java Portability EXECUTABLES Windows PC running JVM Payroll.java Payroll.class SOURCE BYTECODE Java Bytecode Unix box running JVM via interpreter JVM via compiler Macintosh running JVM
Java Program Translation Including Linker Previously Compiled Helper Programs Data for Java Program Java Program Java Compiler JavaVirtual Machine Byte-Code Program Byte-Code Interpreter Machine-Language Instructions Linker Computer Execution of Machine-Language Instructions Output of Java Program Quratulain
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 Quratulain
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. Example of an Object Class Class: Automobile Quratulain
Basic Control Structures in languages • A sequenceis 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. Quratulain
SEQUENCE . . . Statement Statement Statement Quratulain
SELECTION (branch) IF Condition THEN Statement1 ELSE Statement2 True Statement1 Statement Condition . . . Statement2 False Quratulain
Statement LOOP (repetition) WHILE Condition DO Statement1 False . . . Condition True Quratulain
SUBPROGRAM (function) . . . SUBPROGRAM1 SUBPROGRAM1 a meaningful collection of SEQUENCE, SELECTION, LOOP, SUBPROGRAM Quratulain
ASYNCHRONOUS CONTROL EVENTHANDLER a subprogram executed when an event occurs EVENT Quratulain
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 Quratulain
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 Quratulain
An Object of class Time OPERATIONSDATA Set Private data: hrs 8 mins 25 secs 42 Increment Write . . . Time Quratulain
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. Quratulain
Flow Chart Symbols • Common symbols used in flowcharts Quratulain
Example • A flowchart and equivalent Pseudocode to compute the interest on a loan Pseudocode Flow Chart Read NAME, BALANCE, RATE Compute INTEREST as BALANCE x RATE Write (Display) NAME and INTEREST Quratulain
Example 2 • The program computes the sum, average and product of three numbers. 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 Quratulain
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. Condition (Question) "Answer" Is A == B? No Is B > A? Yes Is K <= 25? Yes Is SALES >= $5000.00? Yes Quratulain
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 Read A, B If A is less than B BIG = B SMALL = A else BIG = A SMALL = B Write (Display) BIG, SMALL Quratulain
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. Quratulain
Loop (Cond.) Pre Test Post Test 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 Quratulain