CHAPTER ONE Problem Solving and the Object- Oriented Paradigm.

23
CHAPTER ONE Problem Solving and the Object-Oriented Paradigm

Transcript of CHAPTER ONE Problem Solving and the Object- Oriented Paradigm.

CHAPTER ONE

Problem Solving and the Object-Oriented Paradigm

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 2

Objectives

• Understand problem-solving concepts.

• Formulate algorithms using pseudocode.

• Realize the complexity writing algorithms.

• Understand an event-driven environment.

• Describe how classes and objects work.

• Use and understand the terminology of the object paradigm.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 3

Objectives (cont.)

• Demonstrate the difference between visual and nonvisual software components.

• Begin understanding the relationship of Visual Basic .NET to the event-driven and object paradigms.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 4

1.1 Problem Solving

• Use computer software to help solve problems.– Sample Problem

• Problem Description– Find the largest number in the set of numbers.

• Describing Your Solution– Solve the problem yourself.– Formulate your solution approach.– Translate your solution.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 5

1.1 Problem Solving (cont.)

• Pseudocode (false code) is an outline style used to document the solution to a program.

• Algorithm is the formal term for the set of steps used to solve a problem.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 6

1.1 Problem Solving (cont.)

• Algorithms– A set of ordered steps for solving a problem,

such as a mathematical formula or the instructions in a program. The terms algorithm and logic are synonymous. Both refer to a sequence of steps to solve a problem. Taken from TechEncyclopedia.com.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 7

1.1 Problem Solving (cont.)

• Algorithms (cont.)– A formula or set of steps for solving a

particular problem. To be an algorithm, a set of rules must be unambiguous and have a clear stopping point. Algorithms can be expressed in any language, from natural languages like English or French to programming languages like Visual Basic.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 8

1.1 Problem Solving (cont.)

• Getting Data and Displaying Results– Most algorithms need data.– You must be able to display the results of an

algorithm.– Input data must be accurate.– Algorithm results range from simple to

complex.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 9

1.1 Problem Solving (cont.)

• Getting Data and Displaying Results (cont.)– Major Tasks of a Computer-Base Solution

• Write appropriate computer code to accept and validate data.

• Write appropriate computer code to implement the algorithm.

• Write appropriate code to format the results.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 101.2 The Event-Driven Problem-Solving Environment

• Most of today’s computer applications are event driven.

• Helps developer organize a solution into small segments of code.

• One organizes a solution around a series of events.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 111.3 Using the Object-Oriented Paradigm in Problem Solving

• Classes and Objects– Payroll System problem.– Procedural programming has separate data

and program logic.– Object-oriented programming (OOP)

combines the data and program logic.– Methods describe the behavior associated

with an object.– Encapsulates means an object holds in its

capsule both data and behavior.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 121.3 Using the Object-Oriented Paradigm in Problem Solving (cont.)

– A class is a definition or template that defines how objects are created.

– An instance of a class is another name for an object.

– Special methods known as classes methods may be accessed by a class.

– Each object instantiated from a class has identical data elements and methods.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 13

+Bark()+Run()+Fetch()+Rollover()+Sleep()

-EyeColor-CoatType-CoatColor

Dog

+Sleep()+Wake()

-NumOfLegs-NumOfArms-Upright

Mammal

+Move()

-ColdBlooded-LiveBearing

Animal

+Walk()

-WingType-FlightEnabled-SwimEnabled

BirdPhyllum

+Fly()+Walk()

-BillType-BillColor-FeatherColor

Duck

+Breath()+Reproduce()

Living Organism

Kingdom

Superclass

Subclass

Subclass of Animal,Superclass of Dog

Discriminator

Generalization:“is a” relationship

UML

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 141.3 Using the Object-Oriented Paradigm in Problem Solving (cont.)

• Class Hierarchies– The “is a” relationship may be used describe

class hierarchies.– A UML (Unified Modeling Language) class

diagram may be used to define classes.– A subclass descends from a superclass or

base class.– A superclass contains data and behavior

common to subclasses.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 151.3 Using the Object-Oriented Paradigm in Problem Solving (cont.)

• Inheritance means a subclass acquires all data elements and behaviors of its related superclasses.

• Member describes the contents of a class.

• Siblings describe any two or more classes that descend from the same superclass.

• An abstract class provides a base class but cannot be used to instantiate objects.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 161.3 Using the Object-Oriented Paradigm in Problem Solving (cont.)

• Benefits of Using Object Orientation in Problem Solving– Reflects the natural way we think.– The same class definition can be used in

many applications.• Supports the concept of code reuse.

– In creating a subclass, we automatically inherit all members from the superclass.

– Polymorphism enables the same method to assume different behavior.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 171.3 Using the Object-Oriented Paradigm in Problem Solving (cont.)

• Visual versus Nonvisual Components– Some objects can be seen on the GUI.– Other objects cannot be seen on the GUI.– Microsoft provides both visual and nonvisual

components with Visual Basic .NET.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 181.4 Object Paradigm in Visual Basic .NET

• Supports both the procedural and object-oriented paradigms.

• Supports both visual and nonvisual classes.

• Fully supports inheritance and polymorphism.

• A namespace identifies a set of classes that are treated as a group.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 19

Chapter Summary

• Problem solving with a computer requires the developer to first solve the problem without a computer.

• Writing algorithms may be difficult.• Most computer applications also include code to

obtain input data and to display results.• An event-driven environment is an application

which responds to events generated by the user or by the system.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 20

Chapter Summary (cont.)

• In an event-driven environment, code is organized into small units called event procedures.

• Objects are software components created from a template called a class.

• Each class template defines data and behaviors that each object created from the class possesses.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 21

Chapter Summary (cont.)

• Classes can be arranged in hierarchies that define a set of “is a” relationship.

• Superclasses (or base classes) are at the top of the hierarchy and subclasses descend from superclasses.

• A subclass “is a” superclass.• The object paradigm and polymorphism

provide a very natural and flexible environment.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 22

Chapter Summary (cont.)

• Classes can define visual or nonvisual objects.

• Most classes created to solve business problems are nonvisual.

• Visual Basic .NET creates event-driven solutions that use a combination of the object and nonprocedural paradigms.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.

1- 23

Review Questions

• What are the three common steps that most computer algorithms contain?

• Explain what is meant by the term algorithm and describe an example of one.

• How does and “instance of a class” relate to an object”?

• Does Visual Basic .NET directly support single inheritance or multiple inheritance?

• Is an arm a subclass of a body?• Is a cat a subclass of a mammal?