Some Practice Free Response Problems

Post on 25-Feb-2016

45 views 0 download

Tags:

description

Some Practice Free Response Problems. (For CS III AP) March – April 2014. Some tips for these Problems…. Focus on what you need to return in each method Don’t neglect to read the class( es ) on which the problem relies Use the Given Appendix when Needed - PowerPoint PPT Presentation

Transcript of Some Practice Free Response Problems

Some Practice Free ResponseProblems

(For CS III AP)March – April 2014

Some tips for these Problems…• Focus on what you need to return in each method• Don’t neglect to read the class(es) on which the

problem relies• Use the Given Appendix when Needed– (A Better solution is to Memorize the methods for

Array, String, Actor, etc.)• Practice makes Perfect

Ex 1: What We’re Given…

What We’re Given…(cont.)public class StringCoder { private String masterString;

/** @param master the master string for theStringCoder * Precondition: the master string contains all the letters of the alphabet */ public StringCoder(String master) { masterString = master; } /** @param parts anArrayList of string parts that are valid in the master string * Precondition:parts.size()> 0 * @return the string obtained by concatenating the parts of the master string*/ public String decodeString(ArrayList<StringPart> parts) { /* to be implemented in part (a)*/}

This is what you code

Part A: One Solution

String result = new String (“”);for (int ind = 0; ind < parts.size(); ind ++){

StringPart thisUnit = parts.get(ind);result += masterString.substring (thisUnit.getStart(), thisUnit.getStart()+thisUnit.getLength());

}return result;

Part B/** @param str the string to encode using the master string* Precondition: all of the characters in str appear in the master string; * str.length() > 0 * @return a string part in the master string that matches the beginning of str . * The returned string part has length at least 1.*/ private StringPart findPart(String str) { /* implementation not shown*/} /** @param word the string to be encoded* Precondition: all of the characters in word appear in the master string;* word.length() > 0 * @return an ArrayList of string parts of the master string that can be combined * to create word */ public ArrayList<StringPart> encodeString(String word) { /* to be implemented in part (b)*/} // There may be instance variables, constructors, and methods that are not shown.}

Part B: One SolutionString remaining = word;int limit = word.length();int currentInd = 0;

ArrayList<StringPart> units = new ArrayList<StringPart>();while (currentInd < limit){

StringPart thisUnit = findPart (remaining);currentInd += thisUnit.getLength();remaining = remaining.substring (thisUnit.getLength());units.add (thisUnit);

}return units;

← Could just as easily be (!remaining.equals(“”))

Another Interesting Free Response Problem…

• 2011 AB #4 – Using a 2-D array and Strings

Part A: One Solutionprivate void fillBlock (String str){ if (str.length() > numRows*numCols) { for (int row = 0; row < numRows; row++) { for (int col = 0; col < numCols; col++) { letterBlock[row][col] = str.substring (numRows*row + col, numRows*row +

col+1); } } } else { for (int row = 0; row < numRows; row++) { for (int col = 0; col < numCols; col++) { if ((str.substring (numRows * row + col, numRows * row + col + 1).equals (“”))) { letterBlock[row][col] = “A”; } else { letterBlock[row][col] = str.substring (numRows * row + col, numRows * row + col + 1); } } } }}

Part B

Thank you very much

• Please come next week with your solution(s)• We will check back then• Let me know if you have any questions• This ppt will be posted (like the last one)– At http://mthcompsci.wordpress.com/–With some other sources for Free Response Questions

• See you next week! Good luck.