Page 1

Watcharin Puangplia 29/04/2011


Class

attribute

method

Class       2   - attributes   !   - methods  "# $%  $&


Class

Object A

Object B

Object C

Object D

attribute

method

Object '  () $%  * + ,-(). + Object . ! %  ! ()  ( Object 

$%/ *!(%   Instance  


Rectangle

( %  Diagram  & %*

length width setLength

Class name Attribute

setWidth getLength getWidth getArea

Method

Rectangle length wigth setLength() setWidth() getLength() getWidth() getArea()


Object

Class Rectangle

myRec 10

length

5 setLength

width

setWidth

setLength

getLength getWidth

setWidth getLength

getArea

getWidth getArea

Object

yourRec 20 15 setLength setWidth getLength getWidth getArea


Music player state: current song volume battery life behavior: power on/off change station/song change volume choose random song

creates Music player #1

Music player #2

Music player #3

state: song = "Thriller" volume = 17 battery life = 2.5 hrs

state: song = "Lovesong" volume = 9 battery life = 3.41 hrs

state: song = "Closer" volume = 24 battery life = 1.8 hrs

behavior: power on/off change station/song change volume choose random song

behavior: power on/off change station/song change volume choose random song

behavior: power on/off change station/song change volume choose random song


[AccessSpecifier] class Attribute Member Method Meber }    

Name{

AccessSpecifier    ,(  (- public class ()% 0( 0,-1 Name -$%1 * Member  - 2 , (- Atribute ' Method


[AccessSpecifier] dataType Name;



 

AccessSpecifier ' modifier       4 !  public, protected, private, default dataType "#" $% Attribute &' int , char Name & Attribute   &!"


[AccessSpecifier] return_type Name([arguments]){ Method body }



  

AccessSpecifier ' modifier    Method   4 !  public, protected, private, default return_type ()(3$  /$%  (  method $ (4! Name - Method !(    (- get , set arguments () .0 ( 0$,% -  /( / method !!&  %4&




Static Method â—Ś Method $% %  Static '  (% ,-&( + &   Object 



Instance Method â—Ś Method $%&  % Static '   Object  !(% ,- Method &



Overloading Method â—Ś Method ' 2 Method $% %-('  . %!  ' -  " (0  


() Method $% (% ,- &+ &   Object    (-  %$% /, Class (%  (% ,-- Method *&( public class SimpleMethod { //5(   +.

public static void main(String[] args) //( 6' { System.out.println("Hello from the main method."); displayMessage();

//(% ( 6 displayMessage() $% *

System.out.println("Back in the main method."); } public static void displayMessage() //( 6,' $% * { System.out.println("Hello from the displayMessage method."); } }


public class LoopCall{ public static void main(String[] args) { System.out.println("Hello from the main method."); for (int i = 0; i < 5; i++)// / /(% ( 6$% * $  5 *

displayMessage(); System.out.println("Back in the main method."); } public static void displayMessage() //( 6$% * { System.out.println("Hello from the displayMessage method."); } }


 %$% / Class  (% ,-- Class   ! . .- Method public class LoopCall{ public static void main(String[] args) { System.out.println("Hello from the main method."); for (int i = 0; i < 5; i++) // /(% ( 6$% * $  5  * LoopMethod.displayMessage(); System.out.println("Back in the main method."); } } class LoopMethod{ public static void displayMessage() //( 6$% * { System.out.println("Hello from the displayMessage method."); } }


, void '- method Method &  .&  % parameter public void displayMessage(){ System.out.println(â&#x20AC;&#x153;Helloâ&#x20AC;?); }

Method &  . % parameter public void welcome(Srting name){ System.out.println(â&#x20AC;&#x153;Hello â&#x20AC;? +name ); }


, (3$  /$%  $%'- method ., return $%  ,$$  method Method  .&  % parameter ,-,  Attribute   public double getLength(){ return length; }

Method  . % parameter public int add(int x,int y){ return x+y; }


public class Rectangle{ private double length; private double width;

Data Member (Attribute) Method Member

public void setLength(double len){ length = len; } } class Sawasdee{ String data="Sawasdee "; public void show(String s){ System.out.println(data+s); } }

Data Member (Attribute)

Method Member


 Object ! $& . %* 6%$% 1. '-  Object . !  Object ClassName ObjectName; ObjectName = new ClassName([arguments]); Rectangle box ; box = new Rectangle();

6%$% 2. '-  Object "  Object ClassName ObjectName = new ClassName([arguments]); Rectangle box = new Rectangle();


 

 Object box   Rectangle  Reference   Rectangle $%- box Rectangle box ; box adress

',' Reference $%- box ,'-%*&  Object   Rectangle box = new Rectangle(); box

length :0.0

adress

Object Rectangle

Width:0.0 setLength setWidth getLength getWidth getArea


* /' AccessSpecifier  Attribute . Method $%&   & , - private !(% ,- &!3 ,(% ($* - public !(% ,- &!$* 3 ,(% .

/.  - .   ( '  ! . .    -   ObjectName.AttributeMember; // Atrribute ObjectName.MethodMember();

// Method

(-(% ,- Method setLength()  Object box box.setLength(10.0);


box.setLength(10.0); box.setWidth(5.5); Object Rectangle box

length length:10.0 :0.0

adress

Width:0.0 Width:5.5 setLength setWidth getLength getWidth getArea


public class Rectangle{ private double length; private double width; public void setLength(double len){ //    length = len; //    } public void setWidth(double w){ //    width = w; } public double getLength(){ //     return length; //!   } public double getWidth(){ //       return width; } public double getArea(){ //  "!#$%&   return length * width; //!"!#$%&   } }


public class RectangleDemo { public static void main(String[] args) { Rectangle box = new Rectangle();// (!40- box ,'() %('%

box.setLength(10.0); //'    box box.setWidth(20.0); //'     box System.out.println("The box's length is " + box.getLength());//. 9    box System.out.println("The box's width is " + box.getWidth());//. 9     box System.out.println("The box's area is " + box.getArea());//. "*$%  box } }


class CalScore{ //  calscore public void calculate(){ //( 6''( double score = Math.random()*100; //  .'  0  100 if (score >= 80){ //.   80 System.out.println("Grade is A"); //&( A }else if (score >= 70){ //&    80 .  '($ 70 System.out.println("Grade is B"); //&( B } else{

//'().2 System.out.println("Grade is C"); //&.

} }

} //! CalScore

//!( 6''(

public class CalGrade{ //( 6  +. ' public static void main(String args[]){ CalScore obj = new CalScore(); // (!40- obj   CalScore obj.calculate(); //(% ,-( 6 calculate } }

class and object1  

class and object1