Skip Top Navigation Bar

AP Computer Science A Free Response Practice - 2D Array - Game Board

Mutual Connections

This single part question is worth 6 points.

AP CSA Alignment

Unit Alignment Learning Objectives Resources Skills
Unit 1: Using Objects and Methods 1.10.A Develop code to call class methods and determine the result of those calls. 2.C Write program code involving procedural abstractions.
Unit 2: Selection and Iteration 2.3.A Develop code to represent branching logical processes by using selection statements and determine the result of these processes. 2.A Write program code to implement an algorithm.
Unit 4: Data Collections 4.12.A Develop code used to traverse the elements in a 2D array and determine the result of these traversals 2.B Write program code involving data abstractions.
4.13.A Develop code for standard and original algorithms for a particular context or specification that involves 2D arrays and determine the result of these algorithms.

Directions

The Piece class is used to represent a game piece on a board. The game piece is either Red or Black. A partial declaration of the Piece class is shown.

The GameBoard class models a 2D grid of Piece game pieces. A partial declaration of the GameBoard class is shown.

Write the columnWithMostOfColor method. The method should return the column index that has the most pieces of color color. If there are multiple columns with the same highest count, either column index can be returned.

Consider the following board.

  • columnWithMostOfColor("Red") returns 0
  • columnWithMostOfColor("Black") returns either 2 or 4

If board was empty or doesn't contain the given color, any column value could be returned.

Write Your Response

  • In order to test your program, we have provided some test cases for calls to columnWithMostOfColor.
  • Write the code for the method columnWithMostOfColor in the Java Playground as indicated by the comment add your code here.
  • All tests should return true if your code is correct.