Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])"— Presentation transcript:

1 JAVA PROGRAMMING PART III

2 METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

3 METHOD STATEMENT 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 the data type that return from method Begin with small letter and follow () is option

4 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”

5 Object Oriented Programming Encapsulation Information hiding Inheritance Dynamic binding

6 Abstract Data Type #include 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; }

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

8 Objects Tied 2 things together Data (Attributes) Method (Behaviors) There are 2 properties Encapsulation Information hiding

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

10 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”); } 1 2

11 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

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

13 Try to design class of vector Member (x,y) Method Construct Method add Method subtract Method conjugate

14 CLASS ClassName attributes method() Counter data Counter() reset() count() show() Example

15 CLASS Definition CLASS define as format class [extends ] may be public private protected if empty mean default is identifier; [extends ] identify the parent-class if there is

16 Attribute and Method Definition Attribute define as format Method define as format ; [ ] [static] ( [ ]) { }

17 Example No1 //Sawasdee.java class Sawasdee{ String data=‘Sawasdee ’; public show(String s){ System.out.println(data+s); }

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

19 Instance of Class Define as format = new Example Counter c1 = new Counter(); Student x = new Student();

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

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

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

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

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

25 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

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

27 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

28 Assignment y x 356 90 y x 356

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

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

31 Lifetime of Instances


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

Similar presentations


Ads by Google