java programming part iii. method statement form of method statement [ ] [static] ( [ ]) { } example...

Post on 18-Jan-2016

235 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

JAVA PROGRAMMING

PART III

METHOD STATEMENT

Form of method statement

[<modifier>] [static] <returned type> <method name> ( [<parameter list>]){ <method body>}

Examplepublic static void main(String args[])

METHOD STATEMENT

<modifier> is option Public : other class Private : same class Protected : same package or the extended class in other package Default : same package

[static] is option Called from static method in the same class Called from method in different class by class name

<returned type> the data type that return from method <method name> Begin with small letter and follow () <parameter list> is option

ABSTRACT DATA TYPE

User define data type as array and structure in imperative is not secure as simple type.

How properties of data type to be more secure should be is the interesting topic. How and where to store data is not necessary to

know when we used data. Don’t allow to access the data directly. It can access data used some operations.

Data type have all above properties is “abstract data types”

Object Oriented Programming

Encapsulation Information hiding InheritanceDynamic binding

Abstract Data Type

#include <stdio.h>

int count(int i){ return i++;}void main (){ int c = 0; c = count(c); }

public class Counter{ private int c; private Counter(){ c=0;} public void reset(){ c=0;} public void count(){ c++;} public void show(){ System.out.println(“value:”+c); } public static void main(String args[]){ counter c1 = new counter(); c1.show; }}

Consider this program

class Counter{ private int c; private counter(){ c=0;} public void reset(){ c=0;} public void count(){ c++;} public void show(){ System.out.println(“value:”+c); }}public class MyProgram{ public static void main(String args[]){ counter c1 = new counter(); c1.show(); c1.count(); c1.show(); }}

Objects

Tied 2 things together Data (Attributes) Method (Behaviors)

There are 2 properties Encapsulation Information hiding

Classes and Instances

Classes is definition of object. They are consisted of data (attribute) and method (behavior)

Objects is something that have the attribute and behavior as define in class.

Classes and Instances

class Counter{ private int c; private counter(){ c=0; } public void reset(){ c=0; } public void count(){ c++; } public void show(){ System.out.println(“value:”+c); }}

public class MyProgram{ public static void main(String args[]){ new counter(); System.out.println(“Counter is created”); }}

11

22

Instance and Reference Variable

Reference Variable used to point to instance of class for refer any member and method in the class.

Counter Instance

c

Counter()

Reset()

Count()

Show()

mycounter

Classes and Instances

class Counter{ private int c; private counter(){ c=0; } public void reset(){ c=0; } public void count(){ c++; } public void show(){ System.out.println(“value:”+c); }}

public class MyProgram{ public static void main(String args[]){ counter mycounter; new counter(); mycounter = new counter(); mycounter.show(); mycounter.count(); mycounter.show(); }}

11

22

Try to design class of vector

Member (x,y) Method Construct Method add Method subtract Method conjugate

CLASS

ClassName

attributes

method()

Counter

data

Counter()

reset()

count()

show()

Example

CLASS Definition

CLASS define as format

<modifier> class <class name> [extends <super-class name>]

<modifier> may be• public• private• protected• if empty mean default<class name> is identifier;[extends <super-class name>] identify the parent-class if there is

Attribute and Method Definition

Attribute define as format

Method define as format

<modifier> <type> <attribute name>;

[<modifier>] [static] <returned type> <method name> ( [<parameter list>]){ <method body>}

Example No1

//Sawasdee.javaclass Sawasdee{ String data=‘Sawasdee ’;

public show(String s){ System.out.println(data+s); }}

Example No2

class Student { public int id; private String name; private double gpa; public Student() { id = 0; name =“”; gpa = 0.0} public void setName(String n) {name = n;} public String getName() { return name;} public void SetGpa(double g) { gpa = g;} public double getGpa() { return gpa;}}

Instance of Class

Define as format

<class name> <reference varible> = new <construtor method>

ExampleCounter c1 = new Counter();Student x = new Student();

Instance of Class

public class StudentInstance{ public static void main(String args[]) { Student x = new Student(); x.id = 123; // x.name = “Herry Potter”; x.setName(“Herry Potter”); x.setGpa(4.00); System.out.println(x.id+”,”+x.getName()); System.out.println(x.id+”,”+x.getGpa()); }}

Method

Construtors to initial attribute of class

Accessors for read

Mutators for write

public Student() { id = 0; name =“”; gpa = 0.0}

public String getName() { return name;}public double getGpa() { return gpa;}

public void setName(String n) {name = n;}public void setGpa(double g) { gpa = g;}

Constructor Method

Name same as class Can be more than one, overload constructors

class Student { public int id; private String name; private double gpa; public Student() { id = 0; name = null; gpa = 0.0;} public Student(int I, String n) {id = i; name = n;} public Student(Student s) {id = s.id; name = s.name;} public void setName(String n) {name = n;} public String getName() { return name;} public void SetGpa(double g) { gpa = g;} public double getGpa() { return gpa;}}

Test Constructor Method

public class StudentOverCons{ public static void main(String args[]) { Student x = new Student(); Student y = new Student(123,”John Rambo”); Student z = new Student(y); }}

This References

Used for reference the instance of class, but when class is defined , nobody know the instance will be what its name is.

Class Complex { private double r,i; Complex(double r, double i) { this.r = r; this.i = i} public void add(Complex c) { this.r += c.r; this.i += c.i; }}

This Constructor

Used for reference constructor of class. Java allow used this constructor in

constructor method only and in the first line command.

Used only one time in method

This Constructor

Class Complex { private double r,i; Complex(){this(0.0,0.0);} Complex(double r, double i) { this.r = r; this.i = i} Complex(Complex c) { this(c.r.c.i);} public void add(Complex c){ this.r += c.r; this.i += c.i; } public void print() { system.out.println(this.r+”+i”+this.i); }}

Variables and Instances

Memory allocation Variable do both allocate memory and reference Instance create reference firstly, then allocate

memory. Assignment

Variable assign by copy value Instance assign by copy reference

Assignment

y

x 356

90 y

x 356

356356

Copy value

Class VarAssign{ public static void main(String args[]){ int x=1;y=2; x = y; System.out.println(x+”,”+y); y = 3; System.out.println(x+”,”+y); }}

Copy reference

Class InsAssign{ public static void main(String args[]){ Student x = new Student(123,”Herry Potter”); Student y = new Student(456,”Ron Wisly”); x = y; System.out.println(x.getId()+”,”+x.getName()); System.out.println(y.getId()+”,”+y.getName()); y.setName(“Load Vodermor”); System.out.println(x.getId()+”,”+x.getName()); System.out.println(y.getId()+”,”+y.getName()); }}

Lifetime of Instances

top related