22
12/10/2018 1 Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : [email protected] [email protected] [email protected] Object Oriented Programming Lecture 2 Programming Paradigms User Defined Class

Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

1

Designed and Presented by

Dr. Ayman Elshenawy Elsefy

Dept. of Systems & Computer Eng..

Al-Azhar University

Website: eaymanelshenawy.wordpress.com

Email : [email protected]

[email protected]

[email protected]

Object Oriented Programming

Lecture 2

Programming Paradigms

User Defined Class

Page 2: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

2

Structured Programming The program is developed in a sensible blocks of codes that

makes the program easier to be understand and maintain.

Lecture 2: User Defined Classes

A person perform the following

activities on daily basis

Organize into a groups of related activities and give

each block a title that summarize these activities

One section can be improved without

affecting other sections.

Large programs can be understand

and maintains.

13/10/2018

Structured Programming

MAIN PROGRAM

FUNCTION 3 FUNCTION 2

GLOBAL DATA

FUNCTION 5 FUNCTION 4

FUNCTION 1

Using function

Function & program is divided into modules

Every module has its own data and function which can be

called by other modules. 13/10/2018 Lecture 2: User Defined Classes

Page 3: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

3

Object Oriented Programming Encapsulation:

Program and data can be processed

and store it in one object.

Get reusable S.W components

Abstraction:

Show only essential features

Hide unnecessary features

Hides implementation details from user

Show only function outline

Inheritance:

Features are transferred from Parent to child.

Polymorphism:

enables an entity to co exist in more than

one form

Lecture 2: User Defined Classes

Reusability

13/10/2018

The Software Development Process Analyze the Problem

Figure out exactly the problem to be solved. Try to understand it as much as possible.

Determine Specifications

Describe exactly what your program will do. (the inputs, outputs, and how they relate to one

another )

Don’t worry about how the program will work, but what it will do.

Create a Design

Formulate the overall structure of the program.

Choose or develop your own algorithm that meets the specifications.

Implement the Design

Translate the design into a computer language (Java).

Test/Debug the Program

Try out your program to see if it worked.

If there are any errors (bugs), locate and fix. (debugging).

Your goal is to find errors, so try everything that might “break” your program!

Maintain the Program

Continue developing the program in response to the needs of your users.

In the real world, most programs are never completely finished – they evolve over time.

Page 4: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

4

The Software Development Process

the inputs, outputs, and how they relate to one another

Figure out exactly the problem to be solved

Formulate the overall structure of the

program.

Choose or develop your own algorithm that

meets the specifications

Translate the design into a

computer language (Java).

Try out your program to see if it worked.

If there are any errors (bugs), locate and fix. (debugging).

Continue developing the program in response to the needs of your users 13/10/2018 Lecture 2: User Defined Classes

Example Program: Temperature Converter

Analysis –

the temperature is given in Celsius, user wants it expressed in degrees

Fahrenheit.

Specification

Input – temperature in Celsius

Output – temperature in Fahrenheit

Output = 9/5(input) + 32

Design

Input, Process, Output (IPO)

Prompt the user for input (Celsius temperature)

Process it to convert it to Fahrenheit using F = 9/5(C) + 32

Output the result by displaying it on the screen

Pseudocode:

Input the temperature in degrees Celsius (call it Celsius)

Calculate Fahrenheit as (9/5)*celsius+32

Output Fahrenheit 13/10/2018 Lecture 2: User Defined Classes

Page 5: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

5

Defining Classes and Methods

Objectives Describe concepts of class, class object

Create class objects

Define a Java class, its methods

Describe use of parameters in a method

Use modifiers public, private

Define accessor, matador class methods

Describe information hiding, encapsulation

Write method pre- and post-conditions

Describe purpose of javadoc

Draw simple UML diagrams

Describe references, variables, parameters of a class type

Define Boolean-valued methods such as equals

In applets use class Graphics, labels, init method 13/10/2018 Lecture 2: User Defined Classes

Page 6: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

6

Class and Method Definitions Java program consists of objects

Objects of class types that interact with one another

Objects in real world or Abstractions

class:

Reserved word;

a fixed number of class members

Class member may be:

Named constant,

Variable

Method

Members accessed by name

Class categories/modifiers

Private: is accessible only within class

Public : members of class are accessible outside class

Default : Accesses only with in package

Protected: is accessible within package and outside the package but through inheritance only.

Lecture 2: User Defined Classes

Class and Method Definitions

13/10/2018 Lecture 2: User Defined Classes

Page 7: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

7

Class and Method Definitions

A class as a blueprint

Objects that are instantiations of the

class Automobile

UML class diagram 13/10/2018 Lecture 2: User Defined Classes

Class and Method Definitions

13/10/2018 Lecture 2: User Defined Classes

Page 8: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

8

Class Files and Separate Compilation

Each Java class definition usually in a file name

Begins with name of the class and Ends with .java (Automobile.java)

Class can be compiled separately

Helpful to keep all class files used by a program in the same directory

13/10/2018 Lecture 2: User Defined Classes

Class Syntax

The general syntax for defining a class is:

class Clock:

Data Members (Instance Variables) •private int hr; //store hours

•private int min; //store minutes

•private int sec; //store seconds

Methods

•public void setTime(int hours, int minutes,

int seconds)

•public int getHours()

•public int getMinutes()

•public int getSeconds()

•public void printTime()

•public void incrementSeconds()

•public void incrementMinutes()

•public void incrementHours()

•public boolean equals(Clock otherClock)

•public void makeCopy(Clock otherClock)

•public Clock getCopy() 13/10/2018 Lecture 2: User Defined Classes

Page 9: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

9

13/10/2018 Lecture 2: User Defined Classes

class Clock:

• Two types of constructors - With parameters

- Without parameters (default constructor) Constructors have the following properties:

Class can have one or more constructors.

all constructors name = class name

A method, has no type

Any two constructors must have different signatures

Constructors are automatically executed when a class object is

instantiated (created).

If there are multiple constructors, which constructor executes

depends on the type of values passed to the class object when

the class object is instantiated

Constructors

13/10/2018 Lecture 2: User Defined Classes

Page 10: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

10

• Default constructor is: - public Clock()

• Constructor with parameters is: - public Clock(int hours, int minutes, int seconds)

Clock Constructors

13/10/2018 Lecture 2: User Defined Classes

Variable Declaration and Object Instantiation The general syntax for using the operator new is:

or

Clock myClock;

Clock yourClock;

myClock = new Clock();

yourClock = new Clock(9, 35, 15);

13/10/2018 Lecture 2: User Defined Classes

Page 11: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

11

Variable Declaration and Object Instantiation The general syntax for using the operator new is:

or

Clock myClock;

Clock yourClock;

myClock = new Clock();

yourClock = new Clock(9, 35, 15);

13/10/2018 Lecture 2: User Defined Classes

Accessing Class Members

The syntax to access a data member of a class object or method is:

13/10/2018 Lecture 2: User Defined Classes

Page 12: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

12

Lecture 2: User Defined Classes

Assignment Operator: A Precaution

myClock = yourClock;

• Copy the value of the reference variable yourClock into the

reference variable myClock

• After this statement executes, both yourClock and myClock refer

to the same object

Assignment Operator

Lecture 2: User Defined Classes

Page 13: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

13

setTime Method

Lecture 2: User Defined Classes

class Clock methods

Lecture 2: User Defined Classes

Page 14: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

14

class Clock printTime()

Lecture 2: User Defined Classes

class Clock methods

Lecture 2: User Defined Classes

Page 15: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

15

class Clock methods

Lecture 2: User Defined Classes

class Clock methods

Lecture 2: User Defined Classes

Page 16: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

16

class Clock methods

Lecture 2: User Defined Classes

Default Constructor

or

Constructor with Parameters

or

Lecture 2: User Defined Classes

Page 17: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

17

The Copy Constructor

Executes when an object is instantiated

Initialized using an existing object

Syntax:

13/10/2018 Lecture 2: User Defined Classes

public value-returning method

Takes no parameters

Returns address of a String object

Output using print, println, printf methods

Default definition creates String with name of object’s class name followed by hash code of object

The Method toString

13/10/2018 Lecture 2: User Defined Classes

Page 18: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

18

Class Circle

13/10/2018

Circle

- radius : double

+circle() +circle(double r) +setRadius(double r) +getRadius(double r) : double +area() : double +perimeter() : double +toString() : String

Class Circle

13/10/2018

Circle

- radius : double

+circle() +circle(double r) +setRadius(double r) +getRadius(double r) : double +area() : double +perimeter() : double +toString() : String

Page 19: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

19

Class Circle

13/10/2018

Lecture 2: User Defined Classes

The Modifier static

In the method heading, it specifies that the method can be invoked by using the name of the class

If used to declare data member, data member invoked by using the class name

Static data members of class exist even when no object of class type instantiated

Static variables are initialized to their default values

13/10/2018

Page 20: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

20

public class Illustrate

{

private int x;

private static int y;

public static int count;

public Illustrate()

{ x = 0; }

public Illustrate(int a)

{ x = a; }

void setX(int a)

{ x = a; }

public String toString()

{

return("x = " + x + ", y = “ +

y + ",count = " count);

}

public static void incrementY()

{ y++; }

}

Static Members of a Class

Illustrate illusObject = new Illustrate();

Illustrate.incrementY();

Illustrate.count++;

13/10/2018 Lecture 2: User Defined Classes

Illustrate illusObject1 = new Illustrate(3);

Illustrate illusObject2 = new Illustrate(5);

Static Members of a Class

Illustrate.incrementY();

Illustrate.count++;

13/10/2018 Lecture 2: User Defined Classes

Page 21: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

21

Finalizers (destructors)

Automatically execute when class object goes out of scope

Have no parameters

Only one finalizer per class

Name of finalizer: finalize

Accessor Method: a method of a class that only accesses

(that is, does not modify) the value(s) of the data member(s)

Mutator Method: a method of a class that modifies the value of

one or more data member(s)

Accessor and Mutator Methods

13/10/2018 Lecture 2: User Defined Classes

The Reference this

Refers to instance variables and methods of a class

Used to implement cascaded method calls

Defined within other classes

Can be either a complete class definition or anonymous inner

class definition

Used to handle events

Inner Classes

A data type that specifies the logical properties without the

implementation details

Abstract Data Types

13/10/2018 Lecture 2: User Defined Classes

Page 22: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Define accessor, matador class methods Describe information hiding, encapsulation Write method pre- and post-conditions Describe

12/10/2018

22

13/10/2018 Lecture 2: User Defined Classes