20090720100704mts 2023 Chp 1

  • Uploaded by: muru85
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View 20090720100704mts 2023 Chp 1 as PDF for free.

More details

  • Words: 2,532
  • Pages: 47
MTS 2023 Object Oriented Programming Introduction to Object Oriented Programming

1

An overview of OOP 

OOP is a style programming that focuses on an application’s data and the methods you need to manipulate that data

2

The evolution of programming techniques 



People have been writing computer programs since the 1940s The oldest programming languages required programmers to work with memory addresses and to memorize awkward codes associated with machine languages

3



Newer programming languages    





more like natural languages Easier for programmers to use Allow programmers to name variable Allow to create self-contained modules / program segments that can be pieced together in a variety of ways Usually created by teams of programmers, each developing his own reusable & connectable program procedures

Writing several small modules is easier than writing 1 large program 

Large tasks are easier when you break into units 4

2 major techniques to develop programs & procedures Procedural programming

1.

Focuses on the procedures that programmers create to manipulate data Focus on the action that are carried out. Eg getting input data for an employee Breaking down the process into manageable subtasks







Object oriented programming

2.

Focuses on object and describes their features / attributes and behaviors.







Attributes of an object – the features it has, the value of an object’s attributes constitute the object’s state. Behavior of an object – the thing it does 5

Object oriented approach 

Defining the object’s needed to accomplish a task and developing the objects so that each object maintains its own data and carries out tasks when another object request them.

6

Two major types of OOP 1. Computer simulations applications 2.

Graphical User Interface (GUI)

Computer simulations Example : A city might want to develop a program that provides a simulation of traffic patterns so as to better prevent traffic tie-ups. By creating a model with objects such as cars and pedestrians that each contain their own data and rules for behavior, the simulation can set into motion. So, a computer can create a simulation of a real city at rush hour 7

Graphical User Interface (GUI) 





It is easy to think of the components a user manipulates on a computer screen such as buttons and scroll bars, as similar to real world objects Each GUI contains data – eg button with a specific size and color Each object contains behavior – eg button can be clicked

8

Understanding the programming process Object oriented approach involves 3 separate tasks:



1.

2.

3.

Analyzing the system using an object-oriented approach (object oriented analysis) Designing the system using an object-oriented approach (object oriented design) Writing the programs using an object-oriented approach (object oriented programming)

9

Writing a program involves: 1.

2.

3. 4. 5.

Identify all the objects you want to manipulate and how they relate to each other – data modeling Create a general category for the object – class and how they will communicate with each other Code the statements in programming languages Test the program Put the program into production

10

With OOP : 











You analyze the objects you’re working with and the tasks that need to be performed with, and on, those objects You pass messages to objects, requesting the objects to take action The same message works differently when applied to different objects A module / procedure can work appropriately with different types of data it receives, without the need to write separate modules Objects can share or inherit traits of objects that have already been created, reducing the time it takes to create new objects Encapsulation and information hiding are emphasized.

11

OOP terminology 

Object Oriented Programming (OOP) emphasizes the following concepts: 

 





Class – describes a group or collection of objects with common properties Objects – an instance of a class Inheritance – process of acquiring the traits of one’s predecessors Polymorphism – multiple methods with the same name but act differently and appropriately when used with different types of objects. Encapsulation – process of combining all of an object’s attributes and methods into a single package. It exposes only the functional details, but hides the implementation details of the class. 12

Object Oriented Programs 







Implementation of all the collections of objects that were modeled in the analysis and design phase of the software development process. A real world problem consists of several collections of entities interacting with one another and with their surroundings When solving a real-world problem, a simplified representation of the problem is used to study the problem and construct the solution The representation – model of the problem

13







A model is composed of objects, each one representing a real-world entity The model includes descriptions about these objects and their interactions A task of designing and constructing a model is called modeling

14

Objects and Problem Solving 



A simplification of problem solving process Model of problem

Algorithm & data design

Program

OO approach for problem solving – all phases of the process are based on objects Real-world objects

Model of problem

Software objects

15

Part of the development process 

Grouping similar real-world objects into collections of objects, modeling of these collections and software implementation of the corresponding classes. When the program executes, objects of these classes are created and made to interact among themselves

16

Collection of real-world objects Collection A of objects

Model of class A

Class A

Model of class B

Class B

Collection B of objects

Real world

Model of problem

Software implementation

17

Modules 







A problem is often too complex to deal with a single unit A general approach is to divide the problem into smaller problems that are easier to solve The partitioning of a problem into smaller parts is known as decomposition These small parts are called modules, which are easier to manage. 18

General structure of program Attributes

Attributes

Operations

Operations

Class A

Class B

Attributes Operations

Class C

Attributes Operations

Class D

Program_1 19

Difference between Structured Programming Object Oriented Programming is aOOP subset of structured Language from programming. After objects are created in a program, you use 







those objects and their methods to operate the program. In structured programming, you have a program with many methods in which you can use. One difference between structured programming and objectoriented programming is that structured programming uses the data that is given to them through parameters, while in objectoriented programming, the methods act upon the object's data (fields). This makes programming much easier because the fields are all there and you do not have to make sure that the correct field is passed to the correct method. All you have to do is call which field you want to work with. 20

What is an object?  







Object is a thing (tangible/intangible) A program written in object oriented style will consist of interacting objects. Objects are given the responsibility of carrying out specific tasks of the solution Objects are models of the real-world entities identified in the real-world environment of the problem Objects with similar characteristics are grouped into collections called classes 21

What is an object? 

Example :  A program to maintain bank accounts may have many Account, Customer, Transaction and ATM objects.  An Account object may consist of data such as account number, owner, date opened, initial balance etc.  Program also can has many objects of the same type. Two Customer objects with the names of Jack and Jill 22



Every object has: 





State – represented by the set of properties or attributes and their associated values Behavior – represented by the operations, also known as method, of the object Identity – which is a property that can help identify an object identity :Person

name = “Siti” age = “20” play() stop()

state behavior

23

What is an object?

account 1 This is an Account object named account1

Jack Customer

Jill Inside the object icon we indicate the type of an object.

Customer

Two Customer objects with the names Jack and Jill

24

Classes 



    

In the real-world problem, a class describes a collection of real-world entities or objects with similar characteristics The abstract descriptions of the collections of objects are called classes (or class model) A class is a template to create objects. An object is called an instance of a class. An object is an instance of exactly one class. An instance of a class belongs to the class. Example : The two Customer objects Jack and Jill are instances of the Customer Class. 25

Classes 





A class must be defined before you can create an instance (object) of the class. One class is defined, we can create as many objects of the class as a program requires. Every class defines:  

Attributes (data declaration) One or more operations (functions or method)

26

Classes A Customer class with two Customer objects Customer

This is the Customer class

Jack

Jill

Customer

Customer

This line shows an instance of relationship; e.g Jill is an instance of Customer

27

Example of Classes and Objects Class

Objects

Car

Satria Neo, MyVi, Honda Civic, Toyota Vios

Flower

Orchid, Rose, Bougainvillea, Morning Glory

Shape

Circle, Square, Rectangle, Triangle

28

Java Programming Basics  

An object-oriented program uses objects To use an object in a program, we first declare and create an object.

29

Object Declaration   

Every object we use in a program must be declared. An object declaration designates the name of an object and the class to which the object belongs. Object declaration syntax: ;



Example: Customer Jack, Jill;

30

Object Creation  

 

No objects are actually created by the declaration. Example: Account account; account is used to refer to an Account object, but the actual Account object is not yet created. We create an object by invoking new operation. The syntax for new is = new (<arguments>);

<arguments>

The name of a declared object The name of the class to which the object belongs A sequences of values passed to the new operation

31

Distinction between object declaration and object creation Account account; account = new Account ();

Process Account account; The identifier account is declared and space is allocated in memory account = new Account (); An Account object is created and the identifier account is set to refer to it.

State of memory

account

account Account 32

Example of object declaration and object creation Customer profWu, drCafe; profWu = new Customer (); drCafe = profWu;

profWu drCafe

Customer profWu, drCafe; profWu = new Customer (); drCafe = profWu;

profWu

Customer profWu, drCafe; profWu = new Customer (); drCafe = profWu;

profWu

drCafe

drCafe

Customer

Customer 33

Defining classes 

A basic class definition takes the form: class { [declaration;] [methods;] }

34

Class Definition Program Template Import Statements Class Comment Describe the class in the javadoc format

{

class

Class Name Declarations Declare data members shared by multiple methods here, outside of method declarations

. . . Methods

}

35

Defining classes 

Example of a Java program (a class) class hello { public static void main (String args[]) { System.out.println(“Hello, World!”); } }



The above code defines one class(hello) and one method (main()). 36

Method       

To instruct a class or an object to perform a task, we send a message to it. We cannot send a message to any class or object. We can send a message only to the classes and objects that understand the message. Class or object need a matching method to process the message it receives. Method is a sequence of instructions a class or an object follows to perform a task. A method defined for a class is called a class method, a method defined for an object is an instance method. Class method – for a task that pertains to all instances Instance method – for a task that pertains to an individual instance

37

Class, Object and Method Class Method The dotted line signifies that you cannot send instance messages to a class



Object Method

38

Defining Methods 

A method in Java takes the basic form: [modifier] return-type method-name (parameter list) { [statements;] }

39

Defining Methods 

where specifies the scope(e.g.,public) return-type specifies what type of value the method will return. It can be a simple data type or a class type method-name an identifier Parameter-list contains the variables to be passed. The parameters must always be enclosed in parentheses even if the parameter list is empty. modifier

 

Instance methods are declared WITHOUT the static modifier Class methods are declared WITH the static modifier

40

class Square{ private double width; private double height; Square(){ width = 0; height = 0; }

Method name Return type

public double wide(){ return width * height; }

Access control

}

41

Coding Example

42

Example 1 : Program FunTime 1. /* The program will allow you to draw a picture by 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

dragging a mouse. */ import javabook.*; class FunTime { public static void main (String args[ ]) { SketchPad doodleBoard; doodleBoard = new SketchPad(); doodleBoard.show(); } This statement makes doodleBoard } appear on the screen

Creates a new SketchPad object doodleBoard

43

Explanation – Example 1 

The program consists of 2 classes:  



FunTime : defined in the program SketchPad : predefined outside the program (in a place called the javabook package).

This program opens a SketchPad window named doodleBoard and makes it appear on the screen by sending the message show to it

44

Example 2 - GradeBook 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

// GradeBook.java // Class declaration with one method public class GradeBook { //display a welcome message to the GradeBook public void displayMessage() { System.out.println(“Welcome to the Grade Book!”); } //end method displayMessage } //end class GradeBook

45

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.

// Fig. 3.2 GradeBookTest.java // Create a GradeBook object and call its displayMessage method public class GradeBookTest { // main method begins program execution public static void main (String args[]) { // create a GradeBook Object and assign it GradeBook myGradeBook = new Grade Book ( ) //call myGradeBook’s displayMessage method myGradeBook.displayMessage ( ); } // end main } // end class GradeBookTest

Use class instance creation expression to create object of class GradeBook

Call method displayMessage using GradeBook object

Welcome to the Grade Book!

46

Benefits of OOP      



Reusability (reusable components) Reliability Robustness Extensibility Maintainability Reducing large problems to smaller, more manageable problems Better Analysis and Design of Complex Applications 47

Related Documents


More Documents from ""