Solution to Location class practice test

Complete the Location class practice test before reviewing the solution.

Review the Location class practice test with AP CS Tutor Brandon Horn.

Question 1

new Location(4,0);

All GridWorld methods use Location objects, rather than individual row and column coordinates, to refer to locations. It is occasionally necessary to construct a Location object given a row and column to use one of the methods.

Question 2

Location.SOUTH

getAdjacentLocation takes an integer representing a direction as a parameter. Although it is possible to pass in 180, you should use the Location class constants to make your code clearer for the grader.

Question 3

(A) loc2 will refer to a Location object representing (4,1).
(B) loc2 will refer to a Location object representing (5,0).
(C) loc2 will refer to a Location object representing (1,4).
(D) loc2 will refer to a Location object representing (0,5).
(E) loc2 will refer to null since the location 1 row below loc1 is not valid in the grid.
(F) An exception will be thrown since the location 1 row below loc1 is not valid in the grid.

Although the resulting location is not valid in the grid, the Location class knows nothing about the grid. This becomes important when you need to use a location obtained from one of the methods in the grid, a common requirement. You must first check if the location is valid using the isValid method of Grid.

Question 4

(A) int
(B) double
(C) Location
(D) Direction
(E) String

Directions in GridWorld are represented as integers. There is no Direction class in GridWorld.

Question 5

if(dir1 == Location.SOUTH)
 System.out.println("pirates");

Directions are represented as integers in the case study. Integers are primitive types and are compared using ==, not equals. The Location class constants are all integers.

Question 6

if(loc5.equals(new Location(3,3)))
 System.out.println("treasure");

Objects are compared with equals, not ==. In order to check if a location matches a specific location, you must construct a Location object. Comparing the row and column values individually duplicates the functionality of an existing method (equals) and may not receive full credit.

Question 7

Location loc5 = new Location((int) (Math.random() * r), (int) (Math.random() * c));

You will likely be asked to construct a random value within a given range on the exam. Often, this range will be from 0 up to but not including some number. In this case, the range is from 0 up to but not including r.

Question 8

int result = hunterLoc.compareTo(new Location(3,3));
if(result < 0)
 System.out.println("Keep going!");
else if(result > 0)
 System.out.println("Turn around!");
else
 System.out.println("You found it!");

The Location class implements the Comparable interface. The compareTo method orders locations in row major order, which is exactly the order described in this problem.

Get AP CS Help

GridWorld Practice Problems

GridWorld Case Study Explanations

Leave a Reply