Welcome to FRQ 1: Methods and Control Structures
If you are feeling a bit nervous about the Free-Response Questions (FRQs) on the AP Computer Science A exam, don't worry! FRQ 1 is often seen as the "foundation" question. It tests your ability to handle the basic building blocks of Java: methods, loops, if-statements, and Strings. Think of it as showing the graders that you know how to write the "logic" of a program.
In this chapter, we will break down exactly what to expect, the specific tools you are allowed to use, and how to snag all 7 points available for this question.
The Anatomy of FRQ 1
On the exam, FRQ 1 is worth 7 points and is divided into two distinct parts. You will be asked to write code inside a class that has already been started for you.
Part A (4 points): This part usually focuses on algorithms. You will likely need to use iterative statements (loops like \(for\) or \(while\)) and conditional statements (\(if\), \(if-else\)). A key requirement here is that you must often call other methods within the same class to help solve the problem.
Part B (3 points): This part is almost always about String manipulation. You will be expected to use the standard methods from the Java \(String\) class to analyze or change text data.
Note: For other FRQ types, like Class Design or ArrayLists, check out the later chapters in this section!
Mastering Part A: Control Structures
Part A is all about the "flow" of your code. To succeed here, you need to be comfortable with Selection and Iteration.
1. Selection (if-statements)
Selection allows your program to make decisions. Remember the basics:
- Use \(if\) for a single condition.
- Use \(else\) for a fallback when the condition is false.
- Use \(else \ if\) when you have multiple specific paths to check.
Quick Tip: When comparing primitive numbers like \(int\) or \(double\), use \(==\). When comparing boolean values, you can just use the variable itself! For example, \(if \ (isFound)\) is cleaner than \(if \ (isFound == true)\).
2. Iteration (Loops)
You will almost certainly need a loop in Part A.
- \(for\) loops are great when you know exactly how many times to run (like through the length of a String).
- \(while\) loops are better when you are waiting for a specific condition to change.
Did you know? On the AP exam, you should stick to postfix incrementing, like \(i++\), rather than \(++i\), as the syllabus focuses on the postfix version.
3. Calling Methods
A common mistake is trying to "reinvent the wheel." If the prompt says there is a method called \(checkStatus()\), use it! Part A specifically tests if you can call methods correctly using the right parameters and handling the return value.
Mastering Part B: String Manipulation
Part B is where you show off your \(String\) skills. The AP Java Quick Reference provides a specific list of methods you are allowed to use. Here are the "Must-Knows":
\(int \ length()\): Returns the number of characters. Remember, if the length is \(5\), the last index is \(4\)!
\(String \ substring(int \ from, int \ to)\): This is the one that trips students up. It starts at index \(from\) and goes up to, but does not include, index \(to\).
Example: If \(str = "Computer"\), then \(str.substring(0, 3)\) is \("Com"\).
\(String \ substring(int \ from)\): This starts at \(from\) and goes all the way to the end of the string.
\(int \ indexOf(String \ str)\): This looks for a "needle in a haystack." It returns the index of the first occurrence of \(str\). If it can't find it, it returns \(-1\). Always check for \(-1\) if you aren't sure the string exists!
\(boolean \ equals(Object \ other)\): Crucial! Never use \(==\) to compare Strings. Always use \(str1.equals(str2)\).
\(int \ compareTo(String \ other)\): Used for alphabetical order. It returns \(0\) if they are the same, a negative value if the first string comes before \(other\), and a positive value if it comes after.
Step-by-Step Strategy for Success
When you open your exam to FRQ 1, follow these steps:
Step 1: Read the Preconditions. These are comments that tell you what you can assume is true. For example, "Precondition: \(n > 0\)" means you don't need to worry about negative numbers.
Step 2: Check the Return Type. If the method header says \(public \ int \ calculate()\), make sure your code ends with \(return\) followed by an integer. Mismatched return types are an easy way to lose points.
Step 3: Identify the Loop. Are you looking at every character in a String? You'll need \(for \ (int \ i = 0; \ i < str.length(); \ i++)\).
Step 4: Use the Quick Reference. Don't guess the method names! Look at the provided sheet to ensure you are using \(indexOf\) or \(substring\) correctly.
Common Mistakes to Avoid ("The Gotchas")
1. Off-by-One Errors: Remember that indices start at \(0\). In a loop, usually you want \(i < str.length()\), not \(i <= str.length()\). Using \(<=\) will often cause a "StringIndexOutOfBoundsException."
2. Forgetting to Return: If a method isn't \(void\), it must return a value. Make sure your return statement isn't trapped inside an \(if\) block that might not run!
3. Confusing \(==\) and \(equals()\): Use \(==\) for numbers (\(int, \ double\)) and true/false (\(boolean\)). Use \(.equals()\) for Strings.
4. Case Sensitivity: In Java, \("Apple"\) is not the same as \("apple"\). Unless the prompt says otherwise, be careful with capitalization.
Key Takeaways
- FRQ 1 Focus: Logic, Loops, and Strings.
- Part A: Use \(if\), \(for/while\), and call methods from the class.
- Part B: Use \(String\) methods like \(substring\), \(indexOf\), and \(equals\).
- Math Tools: You might also need \(Math.abs()\), \(Math.pow()\), or \(Math.random()\) from the Quick Reference.
- Types: Stick to \(int, \ double, \ boolean\), and \(String\). Other primitives like \(char\) or \(long\) are not on the exam.
Don't worry if your code isn't "pretty." The graders are looking for logic and correctness, not elegance. Keep your logic clear, follow the prompts, and you'll do great!