Top Banner
03/25/22 B.Ramamurthy 1 Object-Oriented Design and Java B.Ramamurthy
41

11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

Dec 14, 2015

Download

Documents

Violet Parrish
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 1

Object-Oriented Design and Java

B.Ramamurthy

Page 2: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 2

Topics for DiscussionOO Design PrinciplesJava Virtual MachineJava Application StructureClass and objectsMethods and VariablesAccess/Visibility ModifiersSummary

Page 3: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 3

Object-Oriented PrinciplesOOP

Encapsulation(class)-- Information Hiding-- Interface and Implementations-- Standardization-- Access Control mechanisms (private /public etc.)

Inheritance-- Hierarchy-- Reusability-- Extensibility-- Expressive power-- Reflects many real-world problems

Polymorphism-- Many forms of same function-- Abstract Methods-- Abstract Classes

Page 4: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 4

Conventional Compiled Languages

C++source

C++source

C++source

Mac Compiler Mac Hardware

PC Hardware

Mac Compiler

PC Compiler

Sun Compiler Sun Hardware

Page 5: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 5

Java Virtual Machine

Java source

Java source

Java source

Mac Compiler Mac Hardware

PC Hardware

Mac interpreter

PC Interpreter

Sun Interpreter Sun Hardware

JVM

JVM

Byte code

Byte code

Byte code

Java compiler :javac

JVM

Page 6: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 6

“Run-anywhere” Capability

On any machine when you compile Java source code using javac byte code equivalent to the source is generated. Byte code is machine-independent. This enables the “run-anywhere” capability. You invoke java command which will feed the byte code to the machine-dependent interpreter.

Page 7: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 7

Java Application Program Interface (Java API)

(JAVA API)Package of related classes : java.awt

java.util

java.io, java.beans,..Etc..

Random

Date Dictionary

packageclass

Page 8: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 8

Java API : A Simplistic View

API

classes

packages

methods and data declarations

Page 9: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 9

Java API Classes

Unlike many other languages, you will referring to the classes in the API.Where is the API? Application program interface

Page 10: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 10

Types of Programs

Java is fully object-oriented.Every “function” has to be attached to a class. You will mainly deal with three types of programs: class: methods and data (variables +

constants) describing a collection (type) of object

application: class that has a main method: represents a our regular program

applet: class that is meant for execution using a appletviewer/browser

Page 11: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 11

Problem Solving Using Java

OO Design and Progamming in Java

Identify classes needed

Reuse API classes

Reuseyour classes

Design new classes

Write anapplicationclass

Write anappletclass

Create and use objects

Page 12: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 12

What is an Object?

Object-oriented programming supports the view that programs are composed of objects that interact with one another.How would you describe an object?Using its characteristics (has a ----?) and its behaviors (can do ----?)Object must have unique identity (name) : Basketball, Blue ballConsider a ball:

Color and diameter are characteristics (Data Declarations)

throw, bounce, roll are behaviors (Methods)

Page 13: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 13

Classes are Blueprints

A class defines the general nature of a collection of objects of the same type.The process creating an object from a class is called instantiation.Every object is an instance of a particular class.There can be many instances of objects from the same class possible with different values for data.

Page 14: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 14

Example

class Rose

blueRose

redRose

class

objects Object References

Page 15: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 15

Instantiation : Examples

class FordCar ---- defines a class name FordCarFordCar windstar; ---- defines a Object reference windStarwindstar = new FordCar(); ---- instantiates a windstar Object

class HousePlan1 { color….HousePlan1 blueHouse;blueHouse = new HousePlan1(BLUE);HousePlan1 greenHouse = new HousePlan1(GREEN);

Page 16: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 16

Operator new and “dot”

new operator creates a object and returns a reference to that object.After an object has been instantiated, you can use dot operator to access its methods and data declarations (if you have access permissions).EX: redRose.bloom(); greenHouse.color

Page 17: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 17

Elements of a Class

class

header methods data declarations (variables,constants)

header body

variables,constants

statementsmodifiers,type, name

parameters

selection repetitionassignment

others

Page 18: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 18

Class Structure

class

variablesconstants

methods

Page 19: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 19

Defining Classes

Syntax:class class_name { data-declarations constructors methods }Constructors are special methods used for instantiating (or creating) objects from a class.Data declarations are implemented using variable and constant declarations.

Page 20: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 20

Naming Convention

Constants: All characters in uppercase, words in the identifier separated by underscore: EX: MAX_NUMVariables, objects, methods: First word all lowercase, subsequent words start with uppercase. EX: nextInt, myPen, readInt()Instance variable begin with an _Classes: Begin with an uppercase letter. EX: Tree, Car, System , Math

Page 21: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 21

Debugging and Testing

Compile-time Errors : Usually typos or syntax errorsRun-time Errors : Occurs during execution. Example: divide by zero . Logic Errors: Software will compile and execute with no problem, but will not produce expected results. (Solution: testing, and debugging)

Page 22: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 22

Class Components

Class name (starts with uppercase), constants, instance variables, constructors definitions and method definitions.Constants:

public final static double PI = 3.14;Variables:

private double _bonus;public string _name;

Page 23: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 23

Operations

Behaviors, methods or messages to which an object will respond to.Methods:

1. Constructors2. Set/get methods (mutators/accesors)3. Predicate methods (boolean/status indicators;

Ex: isEmpty())4. Utility methods (for local use only: ex: internal

sort after an insert)5. Explicit methods or interface methods that

define the interface an object offers to the world.

Page 24: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 24

Method Invocation/CallSyntax:

method_name (values); object_name.method_name(values); classname.method_name(values);Examples:computeSum(); // call to method from within

the class where it is locatedYourRose.paintIt(Red);Math.abs(X);

Page 25: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 25

Defining MethodsA method is group of (related) statements that carry out a specified function.A method is associated with a particular class and it specifies a behavior or functionality of the class.A method definition specifies the code to be executed when the method is invoked/activated/called.

Page 26: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 26

Method Definition : Syntax

visibility return_type method_name

(parameter_list) { statements }

Page 27: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 27

Return Type

can be void, type or class identifiervoid indicates that the method called to perform an action in a self-standing way: Example: printlntype or class specify the value returned using a return statement inside the method.

Page 28: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 28

Return Statement

Syntax of return statement:return; // for void methodsreturn expression; // for type or class

return value// the expression type and return

type should be same

Page 29: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 29

Parameter List

Parameter list specified in method header provides a mechanism for sending information to a method.It is powerful mechanism for specializing an object.The parameter list that appears in the header of a method specifies the type and name of each parameter

and is called formal parameter list.

The corresponding parameter list in the method invocation is called an actual parameter list.

Page 30: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 30

Parameter list : Syntax

Formal parameter list: This is like molds or templates

(parm_type parm_name, parm_type parm_name, ....)Actual parameter list: This is like material that fit into the mold or template specified in the formal list:

(expression, expression....)

Page 31: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 31

Method Definition : review

return type Name parameter list

{ statements }

headerbody

definition

Visibilitymodifiers

Page 32: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 32

Method Definition : Example

Write a method that computes and returns the perimeter of a rectangle class.Analysis: Send to the method: Length and Width Compute inside the method: Perimeter Return from the method: Perimeter

Page 33: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 33

...Example (contd.)public int perimeter (int length, int width){ int temp; // local temporary variable temp = 2 * (length + width); // compute

perimeter return temp; // return computed value}

Page 34: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 34

What happens when a method is called?

Control is transferred to the method called and execution continues inside the method.Control is transferred back to the caller when a return statement is executed inside the method.

Page 35: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 35

Method Invocation : semantics

8Main method

Operating System

Rect.area(….)

area method

1 2

4

5 6

1. OS to main method2. Main method execution3. Invoke area4. Transfer control to area5. Execute area method6. Return control back to main method7. Resume executing main 8. Exit to OS

37

8

Page 36: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 36

ConstructorsA Constructor is used to create or instantiate an object from the class.Constructor is a special method: It has the same name as the class. It has no return type or return statement.

Typically a class has more than one constructor: a default constructor which has no parameters, and other constructors with parameters. (overloading)

Page 37: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 37

Constructors (contd.)

You don’t have to define a constructor if you need only a default constructor.When you want initializing constructors :

1. you must include a default constructor in this case.

2. You will use initializing constructors when you want the object to start with a specific initial state rather than as default state.

3. Example: Car myCar = new Car(RED); // initializing constructor for Car class with color as parameter

Page 38: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 38

Visibility Modifiers

Method/variable name

public protected “nothing”DEFAULTPackage Visibility

private

type

static “nothing”DEFAULTfor

class methodsand variables

forobjectmethods andvariables

Page 39: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 39

..Modifiers (contd.)

private : available only within class“nothing” specified : DEFAULT: within class and within packageprotected : within inherited hierarchy (only to sub classes)public : available to any class.

Page 40: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 40

Arrays

Array is a numbered collection of variables all of the same type.Length attribute gives the capacity of the arrayCells or the individual elements of the array distinguished by an index.Lets look at an example.Common error: ArrayIndexOutofBounds

Page 41: 11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

04/18/23 B.Ramamurthy 41

Summary

An overview of OOP, problem solving using OOP and Java language was presented.Apply these concepts in your web services design