introduction to object- oriented programming and problem...

59
Chapter 1 Introduction to Object- Oriented Programming and Problem Solving

Upload: others

Post on 29-Mar-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

Chapter 1Introduction to Object-

Oriented Programming andProblem Solving

Page 2: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

2

Knowledge Goals

• Understand what a computer program is• Know the three phases of the software life

cycle• Understand what an algorithm is• Learn what a high-level programming

language is• Understand the difference between machine

code and Bytecode

Page 3: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

3

Knowledge Goals• Understand the compilation, execution, and

interpretation processes• Learn what the major components of a

computer are and how they work together• Understand the concept of an object in the

context of computer problem solving

Page 4: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

4

Skills Goals• List the basic stages involved in writing a

computer application• Distinguish between hardware and software• List the ways of structuring code in a Java

application• Name several problem-solving techniques• Choose an applicable problem-solving

technique• Identify the objects in a problem statement

How can you distinguish the knowledge goals from the skills goals?

Page 5: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

5

What is Programming?ComputerA programmable device that can store, retrieve, and process dataComputer programmingThe process of specifying objects and the ways in which those objects interact to solve problemsProgrammingWriting out instructions for solving a problem or performing a task Computer programInstructions defining a set of objects and orchestrating their interactions to solve a problem

Page 6: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

6

How do we write a Program?

Water-fall

Model

Page 7: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

7

How do we write a program …

Problem-Solving Phase

• Understand (define) the problem and identify what the solution must do

• Specify the objects and their interactions to solve the problem

• Follow the steps exactly to see if the solution really does solve the problem

Page 8: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

8

How do we write a program …

Implementation Phase

• Translate the object specifications (the general solution) into a programming language

• Have the computer carry out the solution and check the results

What if the solution isn't correct?

Page 9: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

9

How do we write a program …

Maintenance Phase

• Use the program• Modify the program to meet changing

requirements, to enhance its functionality, or to correct any errors that show up in using it

Page 10: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

10

How do we write a program …

Can'twe

shortenthe

process?

Page 11: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

11

How can a computer work with objects?

InformationAny knowledge that can be communicatedDataInformation in a form that a computer can useObjectA collection of data values and associated operations

How does all this fit together?

Page 12: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

12

How can a computer work with objects…

Data with

relatedoperationsrepresentsinformation

asobjects

Page 13: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

13

How can a computer work with objects…

Objects contain data and operations

Consider a Name objectWhat sort of data would this object contain?What sort of operations might we need to manipulate the data?

Page 14: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

14

How can a computer work with objects…

Whatother

operationsmight

wewant

?

Page 15: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

15

Classes of Objects

What about multiple objects with the same properties?

ClassA specification of the representation of a particular kind of object, in terms of data and behavior (operations)

Page 16: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

16

Classes of ObjectsDate class

data: month, day, yearoperations: set and return month, day, year

Date object (an instance of a class)June232004

Page 17: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

17

What is an algorithm?

How are the operations specified?

AlgorithmInstructions for solving a problem in a finite amount of time using a finite amount of data

Page 18: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

18

What is an algorithm?Objects: Key, Transmission, Gas Pedal, Engine, Phone

1. Insert the key.2. Make sure the transmission is in Park (or Neutral).3. Depress the gas pedal.4. Turn the key to the start position.5. If the engine starts within six seconds, release the key.6. If the engine doesn’t start within six seconds, release the key and gas pedal, wait ten seconds, and repeat Steps 3 through 6, but not more than five times.7. If the car doesn’t start, phone your mechanic.

Page 19: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

19

What is an algorithm?

Once we have the algorithm, then what?

We translate the algorithm into statements in a programming language

Say again?

Page 20: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

20

What is a programming language?

Programming languageA set of rules, symbols and special words used to construct a computer programStatementsSpecific combinations of symbols and special words that are defined by a programming language to be complete units within a program; analogous to sentences in a human languageCodeInstructions for a computer that are written programming language

Page 21: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

21

What is a programming language?

There isno

singlewayto

implementan

algorithm

Page 22: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

22

What kind of instructions …

ComputerA programmable device that can store, retrieve, and process data

Implies

accept data, send data, perform arithmetic operations, perform logical operations

Page 23: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

What kind of instructions …

Computer can …• Input data from an input device and output

data to an output device• Store and retrieve data from in memory and

secondary storage• Transfer data • Perform arithmetic operations• Compare data values• Branch to a different sections of instruction

23

Page 24: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

24

What kind of instructions …

Control structuresStatements that organize the instructions that specify the behaviors of objects

- sequence- selection- repetition- subprograms- asynchronous

Page 25: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

25

What kind of instructions …

Page 26: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

26

What kind of instructions …

Page 27: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

27

Software Maintenance

Maintaining software accounts for the majority of its live cycle (i.e., old programs never die, they just get modified)

Where were you on January 1, 2000?

e.g., Y2K Problem

Page 28: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

28

Software Maintenance

Software maintenance tips- Check existing code- Make changes to a copy- Change related aspects to leave clean,

consistent code for next change- Keep backup copies

Software maintenance is not glamorousbut it is necessary!

Page 29: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

Binary Representation of Data

• Binary Number• Decimal Number• Hexadecimal Number

29

Page 30: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

30

How It All WorksMachine languageThe language, made up of binary-coded instructions, that is used directly by the computerAssembly languageA low-level programming language in which a mnemonic represents each machine language instruction for a particular computer

Page 31: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

31

How It All Works

High-level language:

R1 = R3 + R5

R2 = R6 - R1

Page 32: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

32

How It All Works

Levelsof

abstraction

Page 33: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

33

Machine Language

• Is not portable

• Runs only on specific type of computer

• Is made up of binary-coded instructions (strings of 0s and 1s)

• Is the language that can be directly used by the computer

Why is portability important?

Page 34: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

34

Assembly Languages

• Are machine dependent and run on only one specific type of computer

• Are translated into machine code by assemblers

• Are made up of English-like abbreviations such as LOAD, STORE, or ADD

Better than machine language…

Page 35: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

35

High-Level Languages• Are portable • Are translated into machine code by

compilers• Instructions are written in language similar to

natural language• Examples -- FORTRAN, COBOL, Pascal,

C, C++• Many are standardized by ISO/ANSI to

provide an official description of the language

Page 36: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

36

High-Level Languages

Source codeInstructions written in a high-level languageObject codeA machine language version derived from source code

Page 37: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

37

Portability

Compiler portability

Page 38: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

38

Java's Road to Portability

BytecodeA standardized machine language into which Java source code is compiled

Page 39: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

39

High-Level LanguagesDirect executionThe process by which a computer performs the actions specified in a machine-language programInterpretationThe translation, while a program is running, of non-machine language instructions (such as Bytecode) into executable operationsVirtual machineA program that makes one computer act like another

Page 40: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

40

High-Level Languages

Do you understand the differencebetween C++ & Java portability?

Page 41: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

41

What's inside the computer?

Can you

nameany

externaldevices

?

Page 42: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

42

What's inside the computer?

Memory unitInternal data storage inside a computer, made up of an ordered sequence of addressable cells

Addressable?

Page 43: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

43

What's inside the computer?

Executes instructions

The Manager

Does operations

Holds data and instructions

Page 44: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

44

What's inside the computer?HardwareThe physical components of a computerSoftwareComputer programs; the set of all programs available on a computerClockAn electrical circuit that sends out a train of pulses to coordinate the actions of the computer's hardware components; its speed is measured in hertz (cycles per second)

Page 45: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

45

What is system software?User/computer interfaceA connecting link that translates between the computer's internal representation of data and representations that humans are able to work withInteractive systemA system that supports direct communication between the user and the computerSystem softwareThe set of programs that simplifies the user/ communication interfaceOperating systemSet of programs that manages all the computer's resources

Page 46: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

46

What is system software?

Seehow

itallfits

together?

Page 47: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

47

Problem Solving

Problem solvingThe act of finding a solution to a perplexing, distressing, vexing, or unsettled question

How do you define problem solving?

Page 48: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

48

Problem Solving

How to Solve It: A New Aspect of Mathematical Method by George Polya"How to solve it list" written within the context of mathematical problemsBut the list is quite general

We can use it to solve computerrelated problems!

Page 49: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

49

Problem Solving

How do you solve problems?

Understand the problemDevise a plan

Carry out the planLook back

Page 50: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

50

Problem-Solving Techniques

Ask questions!- What do I know about the problem?

- What is the information that I have to process in order the find the solution?

- What does the solution look like?

- What sort of special cases exist?

- How will I recognize that I have found the solution?

Page 51: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

51

Problem-Solving Techniques

Never reinvent the wheel!Similar problems come up again and again in different guisesA good programmer recognizes a task or subtask that has been solved before and plugs in the solution

Can you think of two similar problems?

Page 52: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

52

Problem-Solving Techniques

Look for familiar things

Page 53: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

53

Problem-Solving Techniques

Solve by analogy

Page 54: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

54

Problem-Solving Techniques

Means-end analysis

Page 55: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

55

Problem-Solving Techniques

Divide and conquer

Page 56: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

56

Problem-Solving Techniques

Building-block approach

Page 57: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

57

Problem-Solving Techniques

Merging solutions

Page 58: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

58

Problem-Solving TechniquesObject-Oriented Problem Solving Stages

- Brainstorm List all objects that might contribute to solution

- Filter Review the classes to find duplicates or remove unnecessary objects

- Responsibilities Determine the operations associated with a class of objects

- Collaborations Determine the interactions between classes

Page 59: Introduction to Object- Oriented Programming and Problem ...jelee/courses/CS101_W17/notes/Chap01.pdf · Computer programming. The process of specifying objects and the ways in which

59

Problem-Solving Techniques

- Attributes Determine the values defined by a class that are used to represent its objects

- Driver Write a program for creating the objects and coordinating their collaborations to solve the problem