Introduction | 2023 FRQ | Hw/Review |
Classes
Team Teach for Classes
Conclusion (Jason)
The “Classes” section gives a simple guide to handling Classes FRQs in APCSA. It explains key ideas like writing class headers, constructors, and methods, and keeping instance variables private. Following these steps makes your code organized and clear, which helps earn points on the exam.
Tips
-
Follow Instructions: Always match class headers, constructors, and method details to what’s asked in the question.
-
Keep it Simple: Use
this.variable = parameter;
to set instance variables in constructors. -
Check Scoping: Class and method headers should usually be
public
, and instance variables should always beprivate
.
Homework! (Jason)
2017 FRQ, Question 2 - Classes
Homework over here…
public interface StudyPractice {
/** Returns the current practice problem */
String getProblem();
/** Changes to the next practice problem */
void nextProblem();
}
public class MultPractice implements StudyPractice {
private int first;
private int second;
public MultPractice(int first, int second) {
this.first = first;
this.second = second;
}
public String getProblem() {
return first + " TIMES " + second;
}
public void nextProblem() {
second++;
}
}
StudyPractice p1 = new MultPractice(7, 3);
System.out.println(p1.getProblem());
p1.nextProblem();
System.out.println(p1.getProblem());
p1.nextProblem();
System.out.println(p1.getProblem());
p1.nextProblem();
System.out.println(p1.getProblem());
StudyPractice p2 = new MultPractice(4, 12);
p2.nextProblem();
System.out.println(p2.getProblem());
System.out.println(p2.getProblem());
p2.nextProblem();
p2.nextProblem();
System.out.println(p2.getProblem());
p2.nextProblem();
System.out.println(p2.getProblem());
7 TIMES 3
7 TIMES 4
7 TIMES 5
7 TIMES 6
4 TIMES 13
4 TIMES 13
4 TIMES 15
4 TIMES 16