Reference: https://apcentral.collegeboard.org/media/pdf/ap23-frq-comp-sci-a.pdf
Topics:
2DArrays
Traversal/Searching (Nested For Loops)
Null values vs. Objects
Move/Delete/Return Objects
Strings
Equals/EqualsIgnoreCase
This would be a good problem for 2D Arrays.
Part A:
null = no candy.
Remember, if you move candy to the front, it shouldn't also exist at its old location.
Part B:
Check for null BEFORE you check the name of the candy's flavor (If a candy doesn't exist, asking for its flavor will crash the program).
Disclaimer 1: The intent of FRQ's is to test a student's ability to code on paper - how well can you code when all computer assistance is removed? The following starter code is meant to help you figure out if your code works AFTER you've tried paper coding. You should attempt the problem on paper before you attempt it on the computer if you really want to prep for the AP CS A exam.
Disclaimer 2: This is my interpretation of what the Candy & BoxOfCandy classes could have been. There is no "answer key" for the actual hidden functions, constructors, etc.
public class Candy
{
/** Returns a String representing the flavor of this piece of candy */
public String getFlavor()
{
return flavor;
}
// The following are the "instance variables, constructors, and methods that [were] not shown"
// Instance Variable(s)
private String flavor;
// Constructor(s)
public Candy(String flavor)
{
this.flavor = flavor;
}
/**
* toString - unnecessary in this case, since it does the same thing as
* getFlavor. Was provided in case any function in BoxOfCandy used the toString
* method rather than the getFlavor function.
*
* @return String representation of the Candy.
*/
public String toString()
{
return getFlavor();
}
// Helpers
/**
* toStringPadded is a helper function designed to add spaces before & after the
* flavor name in order to help with table alignment. It's purely helping the
* printing capabilities of the BoxOfCandy.
*
* @param len the length of the expected String
* @return the name of the flavor with enough spaces around it to equal the
* given len
*/
public String toStringPadded(int len)
{
String flav = getFlavor();
while (flav.length() < len)
{
if (flav.length() + 2 <= len)
flav = " " + flav + " ";
else
flav += " ";
}
return flav;
}
}
public class BoxOfCandy
{
/** box contains at least one row and is initialized in the constructor */
private Candy[][] box;
/**
* Moves one piece of candy in col, if necessary and possible, so that the box
* element in row 0 of column col contains a piece of candy, as described in
* part (a).
* Return false if there is no piece of candy in column col and true otherwise.
* Precondition: col is a valid column index in box.
*/
public boolean moveCandyToFirstRow(int col)
{
//to be implemented in part (a)
}
/**
* Removes from box and returns a piece of candy with flavor specified by the
* parameter, or returns null if no such piece is found, as described in part
* (b).
*/
public Candy removeNextByFlavor(String flavor)
{
//to be implemented in part (b)
}
// The "instance variables, constructors, and methods that [were] not shown"
// Constructor
public BoxOfCandy(int numRows, int numCols)
{
box = new Candy[numRows][numCols];
}
// toString - Prints the entire box of candy.
public String toString()
{
String msg = "";
int len = calcLongestLength();
String empty = "";
String line = "";
String lineChunk = "";
for (int i = 0; i < len; i++)
{
empty += " ";
lineChunk += "-";
}
for (int i = 0; i < box[0].length; i++)
{
line += "-" + lineChunk;
}
line += "-\n";
for (int row = 0; row < box.length; row++)
{
msg += line;
msg += "|";
for (int col = 0; col < box[row].length; col++)
{
Candy cur = box[row][col];
if (cur != null)
msg += cur.toStringPadded(len);
else
msg += empty;
msg += "|";
}
msg += "\n";
}
msg += line;
return msg;
}
// Helper Functions
/**
* Adds a candy (yumYum) at a given location (row, col) within the box (if it
* can). - returns true if it was added. - returns false if there was already a
* candy there.
*
* @param yumYum the candy to be added to the box.
* @param row the row # (from the top) to begin counting
* @param col the col # (from the left) to begin counting
* @return whether a candy was successfully added.
*/
public boolean addCandy(Candy yumYum, int row, int col)
{
if (box[row][col] == null)
{
box[row][col] = yumYum;
return true;
}
return false;
}
/**
* calcLongestLength finds the longest named candy - used to help with box alignment.
* @return the # of characters in the longest named candy.
*/
public int calcLongestLength()
{
int longest = 0;
for (int row = 0; row < box.length; row++)
{
for (int col = 0; col < box[row].length; col++)
{
Candy cur = box[row][col];
if (cur != null)
{
int len = cur.getFlavor().length();
if (len > longest)
longest = len;
}
}
}
return longest;
}
}