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

31
JAVA PROGRAMMING PART III

Upload: alfred-brown

Post on 18-Jan-2016

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

JAVA PROGRAMMING

PART III

Page 2: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

METHOD STATEMENT

Form of method statement

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

Examplepublic static void main(String args[])

Page 3: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public 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

Page 4: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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”

Page 5: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

Object Oriented Programming

Encapsulation Information hiding InheritanceDynamic binding

Page 6: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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

Page 7: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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(); }}

Page 8: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

Objects

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

There are 2 properties Encapsulation Information hiding

Page 9: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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.

Page 10: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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

Page 11: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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

Page 12: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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

Page 13: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

Try to design class of vector

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

Page 14: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

CLASS

ClassName

attributes

method()

Counter

data

Counter()

reset()

count()

show()

Example

Page 15: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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

Page 16: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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>}

Page 17: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

Example No1

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

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

Page 18: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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

Page 19: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

Instance of Class

Define as format

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

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

Page 20: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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()); }}

Page 21: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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

Page 22: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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

Page 23: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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); }}

Page 24: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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

Page 25: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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

Page 26: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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); }}

Page 27: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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

Page 28: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

Assignment

y

x 356

90 y

x 356

356356

Page 29: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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); }}

Page 30: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

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()); }}

Page 31: JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

Lifetime of Instances