Problem solving object oriented design and java
Download
1 / 42

121110 B.Ramamurthy - PowerPoint PPT Presentation


  • 176 Views
  • Updated On :

Problem Solving, Object-Oriented Design and Java. B.Ramamurthy. Topics for Discussion. OO Design Principles Java Virtual Machine Java Application Structure Class and objects Methods and Variables Access/Visibility Modifiers Summary. Object-Oriented Principles. OOP. Polymorphism

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about '121110 B.Ramamurthy' - Solomon


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Topics for discussion l.jpg
Topics for Discussion

  • OO Design Principles

  • Java Virtual Machine

  • Java Application Structure

  • Class and objects

  • Methods and Variables

  • Access/Visibility Modifiers

  • Summary

B.Ramamurthy


Object oriented principles l.jpg
Object-Oriented Principles

OOP

Polymorphism

-- Many forms of

same function

-- Abstract Methods

-- Abstract Classes

Inheritance

-- Hierarchy

-- Reusability

-- Extensibility

-- Expressive power

-- Reflects many

real-world problems

Encapsulation

(class)

-- Information Hiding

-- Interface and

Implementations

-- Standardization

-- Access Control mechanisms

(private /public etc.)

B.Ramamurthy


Conventional compiled languages l.jpg

Mac Hardware

Mac Compiler

Mac Compiler

C++source

PC Hardware

C++source

PC Compiler

Sun Hardware

C++source

Sun Compiler

Conventional Compiled Languages

B.Ramamurthy


Java virtual machine l.jpg
Java Virtual Machine

JVM

Java compiler :javac

Mac Hardware

Byte code

Mac Compiler

Java source

Mac interpreter

JVM

PC Hardware

Byte code

Java source

PC Interpreter

JVM

Sun Hardware

Byte code

Java source

Sun Interpreter

B.Ramamurthy


Run anywhere capability l.jpg

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.

“Run-anywhere” Capability

B.Ramamurthy


Java application program interface java api l.jpg
Java Application Program Interface (Java API)

(JAVA API)

Package of related

classes : java.awt

Random

java.util

Date

Dictionary

java.io, java.beans,..

Etc..

package

class

B.Ramamurthy


Java api a simplistic view l.jpg
Java API : A Simplistic View

API

packages

classes

methods and data declarations

B.Ramamurthy


Java api classes l.jpg
Java API Classes

  • Unlike many other languages, you will referring to the classes in the API.

  • Where is the API?

  • Docuemntation for the packages are in:

    http://java.sun.com/j2se/1.4/docs/api/

  • Open in it in your browser and traverse through the javadoc tree

B.Ramamurthy


Types of programs l.jpg

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

Types of Programs

B.Ramamurthy


Problem solving using java l.jpg
Problem Solving Using Java

OO Design and Progamming in Java

Write an

applet

class

Identify classes needed

Write an

application

class

Reuse API

classes

Reuse

your classes

Design new

classes

Create and use objects

B.Ramamurthy


What is an object l.jpg
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 ball

  • Consider a ball:

    • Color and diameter are characteristics (Data Declarations)

    • throw, bounce, roll are behaviors (Methods)

B.Ramamurthy


Classes are blueprints l.jpg
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.

B.Ramamurthy


Example l.jpg
Example

objects

Object

References

redRose

class Rose

blueRose

class

B.Ramamurthy


Instantiation examples l.jpg
Instantiation : Examples

  • class FordCar ---- defines a class name FordCar

  • FordCar windstar; ---- defines a Object reference windStar

  • windstar = new FordCar(); ---- instantiates a windstar Object

  • class HousePlan1 { color….

  • HousePlan1 blueHouse;

  • blueHouse = new HousePlan1(BLUE);

  • HousePlan1 greenHouse = new HousePlan1(GREEN);

B.Ramamurthy


Operator new and dot l.jpg
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

B.Ramamurthy


Elements of a class l.jpg
Elements of a Class

class

data declarations (variables,

constants)

methods

header

body

header

statements

modifiers,

type, name

variables,

constants

parameters

repetition

others

selection

assignment

B.Ramamurthy


Class structure l.jpg
Class Structure

class

variables

constants

methods

B.Ramamurthy


Defining classes l.jpg
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.

B.Ramamurthy


Naming convention l.jpg

Constants: All characters in uppercase, words in the identifier separated by underscore: EX: MAX_NUM

Variables, 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

Naming Convention

B.Ramamurthy


Debugging and testing l.jpg

Compile-time Errors : Usually typos or syntax errors identifier separated by underscore: EX: MAX_NUM

Run-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)

Debugging and Testing

B.Ramamurthy


Class components l.jpg
Class Components identifier separated by underscore: EX: MAX_NUM

  • 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;

B.Ramamurthy


Operations l.jpg
Operations identifier separated by underscore: EX: MAX_NUM

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

  • Methods:

    • Constructors

    • Set/get methods (mutators/accesors)

    • Predicate methods (boolean/status indicators; Ex: isEmpty())

    • Utility methods (for local use only: ex: internal sort after an insert)

    • Explicit methods or interface methods that define the interface an object offers to the world.

B.Ramamurthy


Method invocation call l.jpg
Method Invocation/Call identifier separated by underscore: EX: MAX_NUM

  • Syntax:

    method_name (values);

    object_name.method_name(values);

    classname.method_name(values);

    Examples:

    computeSum(); // call to method from within the class where it is located

    YourRose.paintIt(Red);

    Math.abs(X);

B.Ramamurthy


Defining methods l.jpg
Defining Methods identifier separated by underscore: EX: MAX_NUM

  • A 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.

B.Ramamurthy


Method definition syntax l.jpg
Method Definition : Syntax identifier separated by underscore: EX: MAX_NUM

visibilityreturn_type method_name

(parameter_list)

{

statements

}

B.Ramamurthy


Return type l.jpg
Return Type identifier separated by underscore: EX: MAX_NUM

  • can be void, type or class identifier

  • void indicates that the method called to perform an action in a self-standing way: Example: println

  • type or class specify the value returned using a return statement inside the method.

B.Ramamurthy


Return statement l.jpg
Return Statement identifier separated by underscore: EX: MAX_NUM

  • Syntax of return statement:

    return; // for void methods

    return expression; // for type or class return value

    // the expression type and return type should be same

B.Ramamurthy


Parameter list l.jpg
Parameter List identifier separated by underscore: EX: MAX_NUM

  • 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.

B.Ramamurthy


Parameter list syntax l.jpg
Parameter list : Syntax identifier separated by underscore: EX: MAX_NUM

  • 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....)

B.Ramamurthy


Method definition review l.jpg
Method Definition : review identifier separated by underscore: EX: MAX_NUM

definition

header

body

Visibility

modifiers

parameter list

return type

Name

{ statements }

B.Ramamurthy


Method definition example l.jpg
Method Definition : Example identifier separated by underscore: EX: MAX_NUM

  • 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

B.Ramamurthy


Example contd l.jpg
...Example (contd.) identifier separated by underscore: EX: MAX_NUM

public int perimeter (int length, int width)

{

int temp; // local temporary variable

temp = 2 * (length + width); // compute perimeter

return temp; // return computed value

}

B.Ramamurthy


What happens when a method is called l.jpg
What happens when a method is called? identifier separated by underscore: EX: MAX_NUM

  • 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.

B.Ramamurthy


Method invocation semantics l.jpg
Method Invocation : semantics identifier separated by underscore: EX: MAX_NUM

Operating

System

1. OS to main method

2. Main method execution

3. Invoke area

4. Transfer control to area

5. Execute area method

6. Return control back to

main method

7. Resume executing main

8. Exit to OS

8

1

2

Main method

Rect.area(….)

3

7

4

8

area

method

5

6

B.Ramamurthy


Constructors l.jpg
Constructors identifier separated by underscore: EX: MAX_NUM

  • A 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)

B.Ramamurthy


Constructors contd l.jpg
Constructors (contd.) identifier separated by underscore: EX: MAX_NUM

  • 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

B.Ramamurthy


Visibility modifiers l.jpg
Visibility Modifiers identifier separated by underscore: EX: MAX_NUM

type

Method/variable name

public

protected

“nothing”

DEFAULT

Package

Visibility

private

static

“nothing”

DEFAULT

for

class methods

and variables

for

object

methods and

variables

B.Ramamurthy


Modifiers contd l.jpg

private : available only within class identifier separated by underscore: EX: MAX_NUM

“nothing” specified : DEFAULT: within class and within package

protected : within inherited hierarchy (only to sub classes)

public : available to any class.

..Modifiers (contd.)

B.Ramamurthy


Classpath l.jpg

Many times classes needed are available in a packages other than the Java API.

Set the CLASSPATH environment variable of your .cshrc file to point to such packages. One such is located at /projects/bina/CSE116/

setenv CLASSPATH .:/projects/bina/CSE116/other paths

You may now import a class from a package in this directory.

CLASSPATH

B.Ramamurthy


Arrays l.jpg
Arrays than the Java API.

  • Array is a numbered collection of variables all of the same type.

  • Length attribute gives the capacity of the array

  • Cells or the individual elements of the array distinguished by an index.

  • Lets look at an example.

  • Common error: ArrayIndexOutofBounds

B.Ramamurthy


Summary l.jpg
Summary than the Java API.

  • An overview of OOP, problem solving using OOP and Java language was presented.

  • Next class we will “review” interface, abstract class, concrete class, polymorphic dispatch and relationship among classes.

B.Ramamurthy


ad