# 1 Chapter 1 Introduction to Object-Oriented Programming.

## Presentation on theme: "1 Chapter 1 Introduction to Object-Oriented Programming."— Presentation transcript:

1 Chapter 1 Introduction to Object-Oriented Programming

2 Chapter 1 Topics l Overview of Object-Oriented Programming l How is Java Code Converted into a Form that a Computer Can use? l Interpreting Code vs Executing Code l Compilation, Interpretation, and Execution l Kinds of Instructions l Inside the Computer l Problem-Solving Techniques

3 What is Computer Programming? It is the process of specifying the data types and the operations for a computer to apply to data in order to solve a problem STEP 1 STEP 2 STEP 3...

4 Programming Life Cycle Phases 1 Problem-Solving 2 Implementation 3 Maintenance

5 Problem-Solving Phase ANALYZE the problem and SPECIFY what the solution must do Develop a GENERAL SOLUTION (ALGORITHM) to solve the problem l VERIFY that your solution really solves the problem

6 Sample Problem l A programmer needs an algorithm to determine an employee’s weekly wages l How would the calculations be done by hand?

7 One Employee’s Wages l During one week an employee works 52 hours at the hourly pay rate \$24.75 l How much is the employee’s wages? l Assume a 40.0 hour normal work week l 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

8 If hours is over 40.0, then wages = (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

9 A class is... l a description of the representation of a specific kind of object, in terms of data and behavior l Date class n data: month, day, year n operations to set and return month, day, year

10 An Object is... l an instance of a class l a Date object n June n 23 n 2004

11 An Algorithm is... l instructions for solving a problem in a finite amount of time using a finite amount of data

12 A Program is... l an algorithm written for a computer that defines classes of objects and orchestrates their interactions to solve a problem l objects work together to create an application (or program) that solves a problem

13 Employee’s Weekly Wages Objects: Employee, pay rate, hours worked, wages Algorithm: 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

14 A Programming Language is... a language with strict grammatical rules, symbols, and special words used to construct a computer program

15 Code is... l the product of translating an algorithm into a programming language instructions for a computer that are written in a programming language

16 Implementation Phase: Test Testing means executing (running) your program on the computer, to see if it produces correct results l If it does not, check the algorithm and/or code to find the error and fix it Finding known errors is called debugging

17 Maintenance Phase Use and modify the program to meet changing requirements or correct errors that show up in using it l Maintenance begins when your program is put into use and accounts for the majority of effort on most programs

18 Programming Life Cycle Problem-Solving Analysis and Specification General Solution ( Algorithm ) Verify Implementation Concrete Solution ( Code ) Test Maintenance Use Maintain

19 Programming Shortcut? THINKING CODE TEST PROBLEM-SOLVING PHASE IMPLEMENTATION PHASE Shortcut? Algorithm Code Problem

20 Binary Representation of Data l Circuit states correspond to 0 and 1 l Bit (short for binary digit) refers to a single 0 or 1 l Bit patterns represent data n 1 byte = 8 bits n 1 KB = 1024 bytes n 1 MB = 1024 x 1024 = 1,048,576 bytes

21 How Many Possible Digits? l Binary (Base 2) Numbers use 2 digits: Just 0 and 1 l Decimal (Base 10) Numbers use 10 digits: 0 through 9

22 Machine Language l Is not portable l Runs only on specific type of computer l Is made up of binary-coded instructions (strings of 0s and 1s) Is the language that can be directly used by the computer

23 Assembly Languages l Are machine dependent and run on only one specific type of computer l Are translated into machine code by assemblers l Are made up of English-like abbreviations such as LOAD, STORE, or ADD

24 High Level Languages l Are portable l Are translated into machine code by compilers l Instructions are written in language similar to natural language l Examples -- FORTRAN, COBOL, Pascal, C, C++ l Many are standardized by ISO/ANSI to provide an official description of the language

25 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

26 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

27 Java programming language l Achieves portability by using both a compiler and an interpreter l Java compiler translates a Java program into an intermediate Bytecode--not machine language An interpreter program called the Java Virtual Machine (JVM) translates each successive instruction in the Bytecode program to machine language and immediately runs it

28 Basic Control Structures l A sequence is a series of statements that executes one after another l Selection (branch) executes different statements depending on certain conditions l Loop (repetition) repeats statements while certain conditions are met l A subprogram breaks the program into smaller units l Asynchronous control handles events that originate outside our program, such as button clicks.

29 SEQUENCE Statement...

30 SELECTION (branch) IF Condition THEN Statement1 ELSE Statement2 Statement1 Statement Statement2 Condition... True False

31 LOOP (repetition) Statement... False True WHILE Condition DO Statement1 Condition

32 SUBPROGRAM (function) SUBPROGRAM1... SUBPROGRAM1 a meaningful collection of SEQUENCE, SELECTION, LOOP, SUBPROGRAM

33 ASYNCHRONOUS CONTROL EVENT EVENTHANDLER a subprogram executed when an event occurs

34 Object-Oriented Programming l 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 l An object is a collection of data values and associated operations l A class is a description of one or more like objects

35 More OOP Vocabulary l Instantiation is the process of creating an object based on the description provided by a class l A package is a collection of related classes

36 An Object of c lass Time Set Increment Write. Time OPERATIONS DATA Private data: hrs 8 mins 25 secs 42

37 Basic Computer Components Arithmetic Logic Unit Control Unit Auxiliary Storage Device Memory Unit ( RAM & Registers ) Central Processing Unit ( CPU ) Input Device Output Device Peripherals

38 Memory Unit Is an ordered sequence of storage cells, each capable of holding a piece of data Each memory cell has a distinct address The information held can be input data, computed values, or program instructions

39 Central Processing Unit (CPU) l Has two components to execute program instructions -- n Arithmetic/Logic Unit performs arithmetic and logical operations Control Unit controls the order in which instructions in the program are executed

40 Peripheral Devices l Are input, output, or auxiliary storage devices attached to a computer n Input Devices include keyboard and mouse n Output Devices include printers, video display, LCD screens n Auxiliary Storage Devices include disk drives, CD-ROM and DVD-ROM drives

41 Problem Solving Techniques l ASK QUESTIONS -- about the data, the process, the output, error conditions l LOOK FOR FAMILIAR THINGS -- certain situations arise again and again l SOLVE BY ANALOGY -- it may give you a place to start l USE MEANS-ENDS ANALYSIS -- Determine the I/O and then work out the details

42 More Problem Solving Techniques l DIVIDE AND CONQUER -- break up large problems into manageable units l BUILDING-BLOCK APPPROACH -- can you solve small pieces of the problem? l MERGE SOLUTIONS -- instead of joining them end to end to avoid duplicate steps l OVERCOME MENTAL BLOCK -- by rewriting the problem in your own words