FRQ2: Class Design
- Easiest FRQ to score points on.
- Steps:
- Declare instance variables as
private
. - Write the constructor:
- Initialize variables using parameters or defaults.
- Set initial state.
- Implement each method:
- Handle edge cases first.
- Update variables and compute results.
- Return results in the required format.
- Check your work:
- Instance variables are used and updated correctly.
- Method signatures match.
- Return types and formats are correct.
- Logic matches sample runs.
- Universal Template:
public class ClassName {
// 1. Declare private instance variables
private Type1 var1;
private Type2 var2;
// ... add more as needed
// 2. Constructor
public ClassName(Type1 param1, Type2 param2 /* ... */) {
this.var1 = param1;
this.var2 = param2;
// ... other initialization
}
// 3. Method 1
public ReturnType1 method1(TypeA paramA, ...) {
// Handle special/edge cases
// Main logic: update instance variables, compute result
return result;
}
// 4. Method 2
public ReturnType2 method2(/* parameters if any */) {
// Similar structure: handle edge cases, main logic, return
return result;
}
// ...add more methods as required by the problem
}
FRQ3: Array/ArrayList
- Two classes:
- Element Class A: Represents a single object.
- Container Class B: Holds a collection of element objects.
- Steps:
- Traverse the Array/ArrayList in B.
- Get element A using
B[i]
or B.get(i)
. - Call method on Element A.
- Return results.
- Universal Template:
public class ElementClass {
private Type field1;
private Type field2;
// ... other instance variables
public ElementClass(Type f1, Type f2) {
field1 = f1;
field2 = f2;
}
public Type getField1() {
return field1;
}
public Type getField2() {
return field2;
}
// ... other methods
}
public class ContainerClass {
private ElementClass[] arr; // or ArrayList<ElementClass> arr;
// Calculate average
public double getAverage() {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
ElementClass elem = arr[i];
sum += elem.getField1(); // Use method, not direct access
}
return (double) sum / arr.length;
}
// Filter/collect elements
public ArrayList<String> collectSomething() {
ArrayList<String> result = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
ElementClass elem = arr[i];
if (elem.someCondition()) {
result.add(elem.getField2());
}
}
return result;
}
// Find first matching element
public ElementClass findFirst(Type key) {
for (int i = 0; i < arr.length; i++) {
ElementClass elem = arr[i];
if (elem.getField1().equals(key)) {
return elem;
}
}
return null;
}
// Remove all matching elements (for ArrayList, use reverse order)
public void removeElements(Type key) {
for (int i = arr.size() - 1; i >= 0; i--) {
ElementClass elem = arr.get(i);
if (elem.getField1().equals(key)) {
arr.remove(i);
}
}
}
// Update all matching elements
public void updateAll(Type oldValue, Type newValue) {
for (int i = 0; i < arr.length; i++) {
ElementClass elem = arr[i];
if (elem.getField1().equals(oldValue)) {
elem.setField1(newValue);
}
}
}
}
FRQ4: 2D Array
- Similar to FRQ3, generally considered easier.
- Involves two classes and nested loops to traverse the container class.
- Universal Template:
public class ElementClass {
// Instance variables
private Type var1;
private Type var2;
// Constructor
public ElementClass(Type v1, Type v2) {
var1 = v1;
var2 = v2;
}
// Getter methods
public Type getVar1() {
return var1;
}
public Type getVar2() {
return var2;
}
// Other methods
}
public class Array2DClass {
// Instance variables
private int[][] arr; // or ElementClass[][] arr;
// Constructor omitted
// Generic template for traversing a 2D array
public void traverse2D() {
for (int row = 0; row < arr.length; row++) {
for (int col = 0; col < arr[0].length; col++) {
// Operate on arr[row][col]
// If array of objects, can call methods: arr[row][col].getVar1()
}
}
}
// Find maximum value (assuming comparing values returned by getValue())
public int findMaxValue() {
int max = arr[0][0].getValue();
for (int row = 0; row < arr.length; row++) {
for (int col = 0; col < arr[0].length; col++) {
int value = arr[row][col].getValue(); // Get via method
if (value > max) {
max = value;
}
}
}
return max;
}
// Find minimum value
public int findMinValue() {
int min = arr[0][0].getValue();
for (int row = 0; row < arr.length; row++) {
for (int col = 0; col < arr[0].length; col++) {
int value = arr[row][col].getValue(); // Get via method
if (value < min) {
min = value;
}
}
}
return min;
}
// Calculate sum
public int sumAll() {
int sum = 0;
for (int row = 0; row < arr.length; row++) {
for (int col = 0; col < arr[0].length; col++) {
sum += arr[row][col].getValue();
}
}
return sum;
}
}