Introduction to FRQ#2 HW
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
{
String getProblem();
void nextProblem();
}
public class MultPractice implements StudyPractice
{
// declare the variables needed
private int x;
private int y;
private String problem;
// constructor with the instance variables
public MultPractice(int x, int y)
{
this.x = x;
this.y = y;
this.problem = x + " TIMES " + y;
}
// return the problem you are on
@Override
public String getProblem()
{
return problem;
}
// increment the variable so that you can do the next problem
@Override
public void nextProblem()
{
y++;
problem = x + " TIMES " + y;
}
}
// create a new object
StudyPractice p2 = new MultPractice(4,12);
// go to the next problem and fetch it. do that twice
p2.nextProblem();
System.out.println(p2.getProblem());
p2.nextProblem();
System.out.println(p2.getProblem());