22
Overview of Object-Oriented Programming Learning to program a computer… • is a matter of training yourself to solve problems • is a detailed and organized manner • is solving problems intuitively • Is the skill of writing a problem solution in terms of objects and actions

(Prog213) (introduction to programming)v1

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: (Prog213) (introduction to programming)v1

Overview of Object-Oriented Programming

Learning to program a computer…

• is a matter of training yourself to solve problems

• is a detailed and organized manner

• is solving problems intuitively

• Is the skill of writing a problem solution in terms of objects and actions

Page 2: (Prog213) (introduction to programming)v1

What is Programming?

Compared to human…since infancy,

• we have been learning how to act, how to do things

• we learned to expect certain behaviours from everything we encounter

• what we do every day we do automatically.

Page 3: (Prog213) (introduction to programming)v1

Programming - Developing instructions for carrying out a task involving a set of objects

Computer - A programmable device that can store, retrieve, and process data

Data - Information in a form that a computer can useInformation - Any knowledge that can be communicatedObject - A collection of data values and associated

operationsComputer programming - The process of specifying

objects and the ways in which those objects interact to solve a problem

Computer program - Instructions defining a set of objects and orchestrating their interactions to solve a problem

Page 4: (Prog213) (introduction to programming)v1

Programming Process

Page 5: (Prog213) (introduction to programming)v1

Problem-Solving Phase1. Analysis and specification. Understand (define) the problem and identify

what the solution must do.2. General solution (algorithm). Specify the objects and their interactions that

solve the problem.3. Verify. Follow the steps exactly to see if the solution really does solve the

problem.

Implementation Phase1. Concrete solution (program). Translate the object specifications and

algorithms (the general solution) into a programming language.2. Test. Have the computer carry out the program and then check the results. If

you find errors, analyze the program and the general solution to determine the source of the errors, and then make corrections. Once a program has been written, it enters a third phase: maintenance.

Maintenance Phase1. Use. Use the program.2. Maintain. Modify the program to meet changing requirements or to correct

any errors that show up in using it.

Page 6: (Prog213) (introduction to programming)v1

Class - A description of the representation of a specific kind of object, in terms of data and operational behaviours.

Algorithm - Instructions for solving a problem in a finite amount of time using a finite amount of data.

Flowcharts – are often used to graphically represent algorithms.

Constructing an Algorithm Example:

Objects: Employee Record, Personnel Database, Employee ID, Time Card, Pay Rate, Hours Worked, Regular Wages, Overtime Wages, Total Wages

1. Get the Employee Record from the Personnel Database, using the Employee ID from the Time Card.

2. Get the Pay Rate from the Employee Record.3. Get the Hours Worked during the week from the Time Card.4. If the number of Hours Worked is less than or equal to 40,multiply by the PayRate to calculate the Regular Wages.5. If the number of Hours Worked is greater than 40,multiply 40 by the Pay Rate to

calculate the Regular Wages, and then multiply the Hours Worked minus 40 by 11⁄2 times the Pay Rate to calculate the Overtime Wages.

6. Add the Regular Wages to the Overtime Wages (if any) to determine the Total Wages for the week.

Page 7: (Prog213) (introduction to programming)v1

Programming language – A set of rules, symbols, and special words used to construct a computer program

Code - Instructions for a computer that are written in a programming language

Note:An algorithm can be translated into more than one programming language. Each

translation produces a different implementation (fig. a)

Even when two people translate an algorithm into the same programming language, they often come up with different implementations (fig. b)

Every programming language allows the programmer some flexibility in terms of how an algorithm is translated. Given this flexibility, people adopt their own styles in writing programs, just as they do in writing short stories or essays

Some people try to speed up the programming process by going directly from the problem definition to coding (fig. c). Taking such a shortcut is very tempting and, at first glance, seems to save a lot of time. This kind of shortcut actually consumes more time and requires more effort.

Developing a general solution before you write Java code will help you manage the problem, keep your thoughts straight, and avoid mistakes. If you don’t take the time at the beginning to think out and polish your algorithm, you’ll spend a lot of extra time debugging and revising your code. So think first and code later! The sooner you start coding, the longer it takes to write an application that works. (fig. c)

Page 8: (Prog213) (introduction to programming)v1
Page 9: (Prog213) (introduction to programming)v1

How Is Java Code Converted into a Form That a Computer Can Use?

Page 10: (Prog213) (introduction to programming)v1
Page 11: (Prog213) (introduction to programming)v1
Page 12: (Prog213) (introduction to programming)v1
Page 13: (Prog213) (introduction to programming)v1

Bas

ic C

ontr

ol S

truc

ture

s of

Pro

gram

min

g La

ngua

ges

Page 14: (Prog213) (introduction to programming)v1
Page 15: (Prog213) (introduction to programming)v1

DefinitionsMachine language - The language, made up of binary coded instructions, that is used

directly by the computerAssembly language - A low level programming language in which a mnemonic

represents each machine language instruction for a particular computerAssembler - A program that translates an assembly language program into machine

codeCompiler - A program that translates code written in a high-level language into machine

codeSource code - Instructions written in a high-level programming languageObject code - A machine language version of a source codeBytecode - A standard machine language into which Java source code is compiledDirect execution - The process by which a computer performs the actions specified in amachine language programInterpretation - The translation, while a program is running, of non machine languageinstructions (such as Bytecode) into executable operationsVirtual machine - A program that makes one computer act like anotherData type - The specification in a programming language of how information is

represented in the computer as data and the set of operations that can be applied to it

Instantiate - To create an object based on the description supplied by a class.Package - A collection of related classes

Page 16: (Prog213) (introduction to programming)v1

Memory unit - Internal data storage in a computerCentral processing unit (CPU)- The part of the computer that executes the instructions

(object code) stored in memory; made up of the arithmetic/logic unit and the control unit

Arithmetic/logic unit (ALU)- The component of the central processing unit that performs arithmetic and logical operations

Control unit The component of the central processing unit that controls the actions of the other components so that instructions (the object code) execute in the correct sequence

Input/output (I/O) devices - The parts of the computer that accept data to be processed (input) and present the results of that processing (output)

Peripheral device - An input, output, or auxiliary storage device attached to a computerAuxiliary storage device – A device that stores data in encoded form outside the

computer’s main memoryHardware The physical components of a computerSoftware Computer programs; the set of all programs available on a computerInterface A connecting link at a shared boundary that allows independent systems to

meet and act on or communicate with each otherInteractive system A system that supports direct communication between the user and

the computerOperating system A set of programs that manages all of the computer’s resourcesEditor An interactive program used to create and modify source programs or data

Page 17: (Prog213) (introduction to programming)v1

Problem-Solving Techniques

• Ask Questions• Look for Things That Are Familiar• Solve by Analogy• Means-Ends Analysis• Divide and Conquer• The Building-Block Approach• Merging Solutions• Mental Blocks: The Fear of Starting• Object-Oriented Problem Solving

Page 18: (Prog213) (introduction to programming)v1

Ask Questions

Look for Things That Are Familiar

Page 19: (Prog213) (introduction to programming)v1

Solve by Analogy

Means-Ends Analysis

Page 20: (Prog213) (introduction to programming)v1

The Building-Block Approach

Divide and Conquer

Page 21: (Prog213) (introduction to programming)v1

Mental Blocks: The Fear of Starting

Object-Oriented Problem Solving

The initial step in solving many problems is to identify the obvious objects in the problem description. If you are given a recipe, for example, the first thing you do is to identify the ingredients.

Some may already be in your cupboard or refrigerator. For others, you may have to go shopping. Object-oriented problem solving involves much the same process. You look at a problem statement and make a list of the objects in it. Some of those objects are already available in the Java library, just waiting to be used. Other objects you may have to write yourself.

Remember that the computer can do only certain things. Your primary concern, then, is making the computer coordinate the actions of objects to produce the desired effects. If you keep in mind the objects, operations, and data types available in Java, you won’t design an algorithm that is difficult or impossible to code. (PARTY PLANNING Problem: You and some friends want to have a

party on Saturday night, and you need to plan and prepare for it.)

Page 22: (Prog213) (introduction to programming)v1

QUESTIONS ???