some practice free response problems

12
Some Practice Free Response Problems (For CS III AP) March – April 2014

Upload: mills

Post on 06-Jan-2016

26 views

Category:

Documents


1 download

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

Page 1: Some Practice Free Response Problems

Some Practice Free ResponseProblems

(For CS III AP)

March – April 2014

Page 2: Some Practice Free Response Problems

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

Page 3: Some Practice Free Response Problems

Ex 1: What We’re Given…

Page 4: Some Practice Free Response Problems

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

Page 5: Some Practice Free Response Problems

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;

Page 6: Some Practice Free Response Problems

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.}

Page 7: Some Practice Free Response Problems

Part B: One Solution

String 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(“”))

Page 8: Some Practice Free Response Problems

Another Interesting Free Response Problem…

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

Page 9: Some Practice Free Response Problems
Page 10: Some Practice Free Response Problems

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); } } } }}

Page 11: Some Practice Free Response Problems

Part B

Page 12: Some Practice Free Response Problems

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.