HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Welcome to OO, Part 1"
Vol. 2, Issue 11, p. 18

	

Listing 1. class Pawn 

// This line expresses the classes identity 
{ 
   protected Color      the_color;      // These lines express the possible 
states 
   protected Position   the_position; 
   protected boolean      is_captured; 
   protected boolean      in_jeopardy; 
   protected Player      the_owner; 

// These are the behaviors of a pawn.  
   public void setColor( Color c )         // You can set the color of a pawn 
   { 
      the_color = c; 
   } 
   public Color getColor()            // you can get the color of a pawn 
   { 
      return the_color; 
   } 

/* 
   Notice that this method is commented out. We donít know yet if a Pawn is 
smart enough to recognize whether a move is legal or not. 
   public boolean isLegalMove( Position p );      // A pawn will know if a move 
to a new  
   {                  // position is legal or not. 
      if ( this is a legal move for a pawn ) 
         return true; 
      else 
         return false; 
   } 
*/ 
   public void moveTo( Position p );         // A pawn can be moved to a new 
position 
   { 
      the_position = p; 
   } 
   public Position getPosition()         // A pawn can report its position 
   { 
      return the_position; 
   } 
   public void setCaptured ( boolean freedom_state )   // A pawn can be captured 
or somtimes freed 
   { 
      is_captured = freedom_state; 
   } 
   public boolean getCaptured()         // A pawn can report whether it is 
captured 
   { 
      return freedom_state; 
   } 
   public void setJeopardy( boolean jeopardy_state )   // A pawn can be alerted 
when it is in  
   {                  // Jeopardy 
      in_jeopardy = jeopardy_state; 
   } 
   public void getInJeopardy()         // A pawn can report whether it is in  
   {                  // Jeopardy 
      return in_jeopardy; 
   } 

   public void setOwner( Player p ) 
   { 
      the_owner = p; 
   } 

   public Player getOwner() 
   { 
      return the_owner; 
   } 
}  

Listing 2. class Player 

{ 
   ...               /**   Some state variable decleration 
   ...                  Some state variable decleration 
   ...                  Some state variable decleration 
                  **/ 
   protected Pawn pawn1;         // our pawn1 state-variable. pawn1 is a 
variable of  
                  // type Pawn 
   protected Pawn pawn2;         // our pawn state-variable pawn1 is a variable 
of  
                  // type Pawn 
  

/** This is an arbitrary method declaration. The purpose is to demnonstrate the 
use of new public void foo() 

   { 
      pawn1 = new Pawn();      // Here, an instance of class Pawn is created. 
      pawn2 = new Pawn();      // Here, another instance of class Pawn is 
created. 
   } 

Listing 3. class Piece 

  // This line reflects the new identity of our class 
 // The states and behaviors remain the same. 
{ 
   protected Color      the_color;      // States 
   protected Position   the_position; 
   protected boolean      is_captured; 
   protected boolean      in_jeopardy; 
   protected Player      the_owner; 

                     // These are the behaviors of a pawn.  
   public void setColor( Color c )         // You can set the color of a pawn 
   { 
      the_color = c; 
   } 
   public Color getColor()            // you can get the color of a pawn 
   { 
      return the_color; 
   } 
/* 
   Notice that this method is commented out. We donít know yet if a Pawn is 
smart enough to recognize whether a move is legal or not. 
   public boolean isLegalMove( Position p );      // A pawn will know if a move 
to a new  
   {                  // position is legal or not. 
      if ( this is a legal move for a pawn ) 
         return true; 
      else 
         return false; 
   } 
*/ 
   public void moveTo( Position p );         // A pawn can be moved to a new 
position 
   { 
      the_position = p; 
   } 
... 
... 
... 
}  

Listing 4. class Player 

{ 
   ...               /**   Some state variable decleration 
   ...                  Some state variable decleration 
   ...                  Some state variable decleration 
                  **/ 

/** 
   Here are the players pieces. Note that these should really be in an array, 
but for the purpose of this 
   exercise, this is fine. 
   protected Piece   pawn1; 
   protected Piece   pawn2; 
   ... 
   ... 
   protected Piece   pawn7; 
   protected Piece   pawn8; 
   protected Piece   queen; 
   protected Piece   king; 
   protected Piece   knight1; 
   protected Piece   knight2; 
   ... and so on for every piece. 
  

   /** This is an arbitrary method decleration. The purpose is to demnonstrate 
the use of new 
   public void foo()  
   { 
      pawn1 = new Piece();      // Here, an instance of class Piece is created. 
      pawn2 = new Piece();      // Here, another instance of class Piece is 
created. 
      ... 
      ... 
      pawn7 = new Piece();  
      pawn8 = new Piece();      // and we create instances of all of our 
pieces. 

      king = new Piece(); 
      queen = new Piece(); 
      knight1 = new Knight(); 
      ... and so on, new instances are created for each Piece decleration. 

   } 

Listing 5. abstract class Piece 

            // class Piece must be declared abstract because 
of  
                  // method legalMove 
                  // The states and behaviors remain the same. 
{ 
   protected Color      the_color;      // States 
   protected Position   the_position; 
   protected boolean      is_captured; 
   protected boolean      in_jeopardy; 
   protected Player      the_owner; 

                     // These are the behaviors of a pawn.  
   public void setColor( Color c )         // You can set the color of a pawn 
   { 
      the_color = c; 
   } 
   public Color getColor()            // you can get the color of a pawn 
   { 
      return the_color; 
   } 
/* 
   We do not provide an implementation for this method, only the declaration. 
The method is  
   declared abstract to indicate that classes who inheret from this class will 
have to provide the  
   implementation. 
*/ 
   abstract public boolean isLegalMove( Position p ); 

   ... 
   ... 
   ... 
} 

/** 
The Queen class inherits most of the behaviors and states from class Piece, it 
must only 
provide the specific logic necessary to determine whether a move is legal for a 
Queen. 
**/ 
class Queen extends Piece 
{ 
   public boolean isLegalMove( Position p ) 
   { 
//      Insert Queens legal move logic here!! 
   } 

} 

/** 
Similarly, the King class inherets most of the behaviors and states from class 
Piece, it must only provide the specific logic necessary to determine whether a 
move is legal for a King. 
**/ 
class King extends Piece 
{ 
   public boolean isLegalMove( Position p ) 
   { 
//      Insert Kingís legal move logic here!! 
   } 

}



 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.