part5

set 10

  1. Where is the isValid method specified? Which classes provide an implementation of this method?
    It is specified in the Grid interface. BoundedGrid and UnboudedGrid classes implement it.
  2. Which AbstractGrid methods call the isValid method? Why don’t the other methods need to call it?
    getValidAdjacentLocations method calls the isValid method. Other methods don’t directly call isValid. getEmptyAdjacentLocations and getOccupiedAdjacentLocations call getValidAdjacentLocations which calls isValid.Also, Method getNeighbors calls getOccupiedAdjacentLocations, which calls getValidAdjacentLocations which calls isValid.
  3. Which methods of the Grid interface are called in the getNeighbors method? Which classes provide implementations of these methods?
    Method getNeighbors calls the Grid methods get and getOccupiedAdjacentLocations. The AbstractGrid class implements the getOccupiedAdjacentLocations. The get method is implemented in the BoundedGrid and UnbounedGrid classes.
  4. Why must the get method, which returns an object of type E, be used in the getEmptyAdjacentLocations method when this method returns locations, not objects of type E?
    The get method returns a reference to the object stored in the grid at the given location or null if no object exists. The getEmptyAdjacentLocations calls the get method and tests the result to see if it is null. If null is returned, that location is “empty” and is added to the list.
  5. What would be the effect of replacing the constant Location.HALF_RIGHT with Location.RIGHT in the two places where it occurs in the getValidAdjacentLocations method?
    The number of possible valid adjacent locations would decrease from eight to four. The valid adjacent locations would be those that are north, south, east, and west of the given location

set 11

  1. What ensures that a grid has at least one valid location?
    The constructor for the BoundedGrid will throw an IllegalArgumentException if the number of rows <= 0 or the number of cols <= 0
  2. How is the number of columns in the grid determined by the getNumCols method?
  3. What assumption about the grid makes this possible?
    occupantArray[0].length

    getNumCols returns the number of columns in row 0 of the occupantArray. The constructor ensures
    that each BoundedGrid object has at least one row and one column.

  4. What are the requirements for a Location to be valid in a BoundedGrid?
    The location’s row value is greater than or equal to 0 and less than the number of rows in the BoundedGrid. The location’s column value is too.

    In the next four questions, let r = number of rows, c = number of columns, and n = number of occupied locations.
  5. What type is returned by the getOccupiedLocations method? What is the time complexity (Big-Oh) for this method?
    The ArrayList is returned by the getOccupiedLocations method.

    O(r * c)——each location must be visited and occupied locations are added to the end of the ArrayList

  6. What type is returned by the get method? What parameter is needed? What is the time complexity (Big-Oh) for this method?
    The type returned is E
    a Location object is needed
    Accessing a two-dimensional array given and row and column value is O(1)

  7. What conditions may cause an exception to be thrown by the put method? What is the time complexity (Big-Oh) for this method?
    If the location where an object is to be added is not in the grid (invalid location), an IllegalArgumentException will be thrown
    If the object sent to the put method is null, a NullPointerException will be thrown.
    O(1)
  8. What type is returned by the remove method? What happens when an attempt is made to remove an item from an empty location? What is the time complexity (Big-Oh) for this method?
    The generic type E
    If an attempt is made to remove an item from an empty location, null is stored in the location and null is returned. It is not an error to call the Grid class’s remove method on a location that is empty
    The time complexity for the remove method is O(1)
  9. Based on the answers to questions 4, 5, 6, and 7, would you consider this an efficient implementation? Justify your answer
    Yes. The only method that is inefficient is the getOccupiedLocations method, O(r * c). The other methods (put, get, and remove) are O(1).

set 12

  1. Which method must the Location class implement so that an instance of HashMap can be used for the map? What would be required of the Location class if a TreeMap were used instead? Does Location satisfy these requirements?
    The Location class must implement the hashCode and the equals methods. The hashCode method must return the same value for two locations that test true when the equals method is called. The Location class implements the Comparable interface. Therefore, the compareTo method must be implemented for the Location class to be a nonabstract class. The compareTo method should return 0 for two locations that test true when the equals method is called. The TreeMap requires keys of the map to be Comparable.
    The Location class satisfies all of these requirements.

  2. Why are the checks for null included in the get, put, and remove methods? Why are no such checks included in the corresponding methods for the BoundedGrid?
    The UnboundedGrid uses a HashMap as its data structure to hold the items in the grid. All non-null locations are valid in the UnBoundedGrid. The isValid method for the UnBoundedGrid always returns true; it does not check for null locations. In a Map object, null is a legal value for a key. In an UnboundedGrid object, null is not a valid location. Therefore, the UnboundedGrid methods get, put, and remove must check the location parameter and throw a NullPointerException when the parameter is null.
    The isValid method is called before using a location to access the occupantArray in the BoundedGrid. If the location parameter is null in the isValid method, an attempt to access loc.getRow() will cause a NullPointerException to be thrown.
    If code is written that does not call the isValid method before calling the get, put, and remove methods, attempting to access loc.getRow() in these methods will also cause a NullPointerException to be thrown.

  3. What is the average time complexity (Big-Oh) for the three methods: get, put, and remove? What would it be if a TreeMap were used instead of a HashMap?
    The average time complexity for the get, put, and remove is O(1).
    If a TreeMap were used instead of a HashMap, the average time complexity would be O(log n), where n is the number of occupied locations in the grid
  4. How would the behavior of this class differ, aside from time complexity, if a TreeMap were used instead of a HashMap?
    Most of the time, the getOccupiedLocations method will return the occupants in a different order.
    Keys (locations) in a HashMap are placed in a hash table based on an index that is calculated by using the keys’ hashCode and the size of the table. The order in which a key is visited when the keySet is traversed depends on where it is located in the hash table.
    A TreeMap stores its keys in a balanced binary search tree and traverses this tree with an inorder traversal. The keys in the keySet (Locations) will be visited in ascending order (row major order) as defined by Location’s compareTo method
  5. Could a map implementation be used for a bounded grid? What advantage, if any, would the two-dimensional array implementation that is used by the BoundedGrid class have over a map implementation?
    Yes, a map could be used to implement a bounded grid.
    If a HashMap were used to implement the bounded grid, the average time complexity for getOccupiedLocations would be O(n), where n is the number of items in the grid.
    If the bounded grid were almost full, the map implementation would use more memory, because the map stores the item and its location. A two-dimensional array only stores the items. The locations are produced by combining the row and column indices for each item.

猜你喜欢

转载自blog.csdn.net/winding125/article/details/109254647