120 likes | 233 Vues
This document provides essential tips and coding solutions for tackling free response problems in the AP Computer Science III exam from March-April 2014. It emphasizes the importance of understanding the methods required for each problem, reading all relevant classes, and utilizing the provided appendix. With practice and familiarity with the methods for arrays, strings, and other data structures, students can enhance their coding skills and problem-solving abilities. Detailed coding examples for string manipulation and array processing are included to illustrate these principles.
E N D
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
What We’re Given…(cont.) public class StringCoder { private String masterString; /** @param master the master string for the StringCoder * Precondition: the master string contains all the letters of the alphabet */ public StringCoder(String master) { masterString = master; } /** @param parts an ArrayList 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 (intind = 0; ind < parts.size(); ind ++) { StringPartthisUnit = parts.get(ind); result += masterString.substring (thisUnit.getStart(), thisUnit.getStart()+thisUnit.getLength()); } return result;
Part B /** @paramstr 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 StringPartfindPart(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 Solution String remaining = word; int limit = word.length(); intcurrentInd = 0; ArrayList<StringPart> units = new ArrayList<StringPart>(); while (currentInd < limit) { StringPartthisUnit = 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 Solution private void fillBlock (String str) { if (str.length() > numRows*numCols) { for (int row = 0; row < numRows; row++) { for (intcol = 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 (intcol = 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); } } } } }
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.