2009 Number Tile

<< 2009BatteryCharger | APQuestionsTrailIndex | 2008TripFlights >>

Question from College Board

NumberTile.java

/**

 * @version 11 May 2009
 */
public class NumberTile 
{ 
  /** Rotates the tile 90 degrees clockwise  
   */ 
  private int[] tile;
  public NumberTile(int a, int b, int c, int d)
  {
      tile = new int[4];
      tile[0]=a;
      tile[1]=b;
      tile[2]=c;
      tile[3]=d;
  }
  public void rotate()  
  {
      int buff=tile[3];
      tile[3]=tile[2];
      tile[2]=tile[1];
      tile[1]=tile[0];
      tile[0]=buff;     
  } 


  /** @return value at left edge of tile  
   */ 
  public int getLeft()  
  {  return tile[3]; } 


  /** @return value at right edge of tile  
   */ 
  public int getRight()  
  {  return tile[1];  } 
  public String toString()
  {
      return "["+tile[3]+" (top:"+tile[0]+" bottom:"+tile[2]+") "+tile[1]+"]"; 
  }
  // There may be instance variables, constructors, and methods that are not shown. 
} 

TileGame

import java.util.ArrayList;
/**

 * @version 11 May 2009
 */
public class TileGame 
{ 
  /** represents the game board; guaranteed never to be null */ 
  private ArrayList<NumberTile> board; 

  public TileGame()  
  { board = new ArrayList<NumberTile>(); } 
 public int getIndexForFit(NumberTile tile) 
  {  /* to be implemented in part (a) */  

    return -1;

  } 
  public boolean insertTile(NumberTile tile) 
  {  /* to be implemented in part (b) */  

     return false;

  }
  public void addTile(NumberTile tile) 
  {      
     board.add(tile);  
   } 
  public String toString()
  {
      String result="";
      for (NumberTile t:board) result+=t.toString();
      return result;
  }
  // There may be instance variables, constructors, and methods that are not shown. 
}

NumberTileTester.java

/**
 * TileTester.
 * @version 11 May 2009
 */
public class TileTester
{
	public static void main (String[] args)
	{
	    System.out.println("Test of Tile Rotation");
	    NumberTile a=new NumberTile(4,3,7,4);
	    System.out.print(a);
	    a.rotate();
	     System.out.print(a);
	    a.rotate();
	     System.out.print(a);
	    a.rotate();
	     System.out.print(a);
	    a.rotate();
	    System.out.print(a);
	    System.out.println("\n\nTest of Tile Game");
	    TileGame game=new TileGame();
	    game.addTile(a);
	    game.addTile(new NumberTile(6,4,3,3) );
	    game.addTile(new NumberTile(1,2,3,4) );
	    game.addTile(new NumberTile(3,2,5,2) );
	    game.addTile(new NumberTile(5,9,2,2) );
	    System.out.println(game);
	     System.out.println("\n\nTest of part a");
	    NumberTile tile1=new NumberTile(4,2,9,2);
	    NumberTile tile2=new NumberTile(4,2,9,8);
	    NumberTile tile3=new NumberTile(0,4,0,0);
	    NumberTile tile4=new NumberTile(0,0,0,9);
	    System.out.println("getIndexForFit(tile1) should return 3 or 4: "+game.getIndexForFit(tile1) );
	    System.out.println("getIndexForFit(tile2) should return -1: "+game.getIndexForFit(tile2) );
	    System.out.println("getIndexForFit(tile3) should return 0: "+game.getIndexForFit(tile3) );
	    System.out.println("getIndexForFit(tile4) should return 5: "+game.getIndexForFit(tile4) );
	    TileGame empty=new TileGame();
	    System.out.println("empty.getIndexForFit(tile1) should return 0: "+empty.getIndexForFit(tile1) );
	    System.out.println("\n\nTest of part b");
	    System.out.println("Before:\n"+game);
	    System.out.println("Able to insert tile1? "+game.insertTile(tile1));
	    System.out.println("After tile 1:\n"+game);
	     System.out.println("Able to insert tile2? "+game.insertTile(tile2));
	    System.out.println("After tile 2:\n"+game);
	   }

}