Advertisement
RovkirHexus

BoundedGrid

Jun 8th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. package feAttempt;
  2.  
  3. /*
  4.  * AP(r) Computer Science GridWorld Case Study:
  5.  * Copyright(c) 2002-2006 College Entrance Examination Board
  6.  * (http://www.collegeboard.com).
  7.  *
  8.  * This code is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation.
  11.  *
  12.  * This code is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * @author Alyce Brady
  18.  * @author APCS Development Committee
  19.  * @author Cay Horstmann
  20.  */
  21.  
  22. import java.util.ArrayList;
  23.  
  24. /**
  25.  * A <code>BoundedGrid</code> is a rectangular grid with a finite number of
  26.  * rows and columns. <br />
  27.  * The implementation of this class is testable on the AP CS AB exam.
  28.  */
  29. public class BoundedGrid<E> extends AbstractGrid<E>
  30. {
  31.   private Object[][] occupantArray; // the array storing the grid elements
  32.  
  33.   /**
  34.    * Constructs an empty bounded grid with the given dimensions.
  35.    * (Precondition: <code>rows > 0</code> and <code>cols > 0</code>.)
  36.    * @param rows number of rows in BoundedGrid
  37.    * @param cols number of columns in BoundedGrid
  38.    */
  39.   public BoundedGrid(int rows, int cols)
  40.   {
  41.     if (rows <= 0)
  42.       throw new IllegalArgumentException("rows <= 0");
  43.     if (cols <= 0)
  44.       throw new IllegalArgumentException("cols <= 0");
  45.     occupantArray = new Object[rows][cols];
  46.   }
  47.  
  48.   public int getNumRows()
  49.   {
  50.     return occupantArray.length;
  51.   }
  52.  
  53.   public int getNumCols()
  54.   {
  55.     // Note: according to the constructor precondition, numRows() > 0, so
  56.     // theGrid[0] is non-null.
  57.     return occupantArray[0].length;
  58.   }
  59.  
  60.   public boolean isValid(Location loc)
  61.   {
  62.     return 0 <= loc.getRow() && loc.getRow() < getNumRows()
  63.       && 0 <= loc.getCol() && loc.getCol() < getNumCols();
  64.   }
  65.  
  66.   public ArrayList<Location> getOccupiedLocations()
  67.   {
  68.     ArrayList<Location> theLocations = new ArrayList<Location>();
  69.    
  70.     // Look at all grid locations.
  71.     for (int r = 0; r < getNumRows(); r++)
  72.     {
  73.       for (int c = 0; c < getNumCols(); c++)
  74.       {
  75.         // If there's an object at this location, put it in the array.
  76.         Location loc = new Location(r, c);
  77.         if (get(loc) != null)
  78.           theLocations.add(loc);
  79.       }
  80.     }
  81.    
  82.     return theLocations;
  83.   }
  84.  
  85.   public E get(Location loc)
  86.   {
  87.     if (!isValid(loc))
  88.       throw new IllegalArgumentException("Location " + loc
  89.                                            + " is not valid");
  90.     return (E) occupantArray[loc.getRow()][loc.getCol()]; // unavoidable warning
  91.   }
  92.  
  93.   public E put(Location loc, E obj)
  94.   {
  95.     if (!isValid(loc))
  96.       throw new IllegalArgumentException("Location " + loc
  97.                                            + " is not valid");
  98.     if (obj == null)
  99.       throw new NullPointerException("obj == null");
  100.    
  101.     // Add the object to the grid.
  102.     E oldOccupant = get(loc);
  103.     occupantArray[loc.getRow()][loc.getCol()] = obj;
  104.     return oldOccupant;
  105.   }
  106.  
  107.   public E remove(Location loc)
  108.   {
  109.     if (!isValid(loc))
  110.       throw new IllegalArgumentException("Location " + loc
  111.                                            + " is not valid");
  112.    
  113.     // Remove the object from the grid.
  114.     E r = get(loc);
  115.     occupantArray[loc.getRow()][loc.getCol()] = null;
  116.     return r;
  117.   }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement