FRQ 2 - Class Design - Part 1

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java1 class period2 exercises16 graded tests

Files

README.mdDownload

FRQ 2 - Class Design - Part 1

Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.

From the 2025 Course and Exam Description:

Free-Response Question 2: Class Design. Students will be instructed to design and implement a class based on provided specifications and examples. A second class might also be included. Students will be provided with a scenario and specifications in the form of a table demonstrating ways to interact with the class and the results. The class must include a class header, instance variables, a constructor, a method, and implementation of the constructor and required method.

FRQ 2 requires the following skills:

  • Create a class header
  • Determine what private instance variables are needed by the class
  • Write a constructor with appropriate parameters
  • Implement a method according to the behavior specified in the table

Teacher Insights

  • You do not need to include import statements. You can assume that any standard Java class is already imported.
  • You will NEVER print. If you think you need to print, you probably need to return something.
  • You will NOT need to write a main. In this question, they are giving you the main.

Introduction to Class Design

These questions usually require the following components:

public class ClassName			// Class Header - Note no ()
{
    private type variableName1;		// Private instance variable(s)
    private type variableName2;

    public ClassName(type param1, ...)	// Constructor with parameters
    {
        variableName1 = param1;	// set private instance variables to parameters
        variableName2 = param2;
    }

    // Methods as requested
    public returnType methodName(type param, ...)
    {
        /* implementation will vary */
    }

    public type getVariableName1()	// Possibly a getter
    {
        return variableName1;
    }
}

2010 FRQ 2 - APLine

The following is a free response question from 2010. It was question 2 on the exam. You can see all the free response questions from past exams at Past AP CSA Exams.

Question 2. An APLine is a line defined by the equation ax + by + c = 0,where a is not equal to zero, b is not equal to zero, and a, b, and c are all integers. The slope of an APLine is defined to be the double value -a / b . A point (represented by integers x and y) is on an APLine if the equation of the APLine is satisfied when those x and y values are substituted into the equation. That is, a point represented by x and y is on the line if ax + by + c is equal to 0. Examples of two APLine equations are shown in the following table.

Ap Line Table

Assume that the following code segment appears in a class other than APLine. The code segment shows an example of using the APLine class to represent the two equations shown in the table.

APLine line1 = new APLine(5, 4, -17);
double slope1 = line1.getSlope();        // slope1 is assigned -1.25
boolean onLine1 = line1.isOnLine(5, -2); // true because 5(5) + 4(-2) + (-17) = 0

APLine line2 = new APLine(-25, 40, 30);
double slope2 = line2.getSlope();        // slope2 is assigned 0.625
boolean onLine2 = line2.isOnLine(5, -2); // false because -25(5) + 40(-2) + 30 != 0

Write the APLine class. Your implementation must include a constructor that has three integer parameters that represent a, b, and c, in that order. You may assume that the values of the parameters representing a and b are not zero.

It must also include a method getSlope() that calculates and returns the slope of the line (using the equation -a / b) and a method isOnLine(x, y) that returns true if the point represented by its two parameters (x and y, in that order) is on the APLine and returns false otherwise, by testing if ax + by + c is equal to 0.

Your class must produce the indicated results when the main method below is run. You may ignore any issues related to integer overflow.

Exercise 1

Write a class APLine with instance variables, a constructor with 3 paramaters for a, b, c, and the methods getSlope() and isOnLine(x,y).

Open the 01-APLineFRQ folder and edit Main.java. Press Run to try it; Run Tests checks it (6 tests). Name the class, then rename the file to match.

2024 FRQ 2 - Scoreboard

This question involves a Scoreboard for a game. The game is played between two teams who alternate turns so that at any given time, one team is active and the other team is inactive. During a turn, a team makes one or more plays. Each play can score one or more points and the team’s turn continues, or the play can fail, in which case no points are scored and the team’s turn ends. The Scoreboard class, which you will write, is used to keep track of the score in a game.

  • The constructor has two parameters. The first parameter is a String containing the name of team 1, and the second parameter is a String containing the name of team 2. The game always begins with team 1 as the active team.
  • The recordPlay method has a single nonnegative integer parameter that is equal to the number of points scored on a play or 0 if the play failed. If the play results in one or more points scored, the active team’s score is updated and that team remains active. If the value of the parameter is 0, the active team’s turn ends and the inactive team becomes the active team. The recordPlay method does not return a value.
  • The getScore method has no parameters. The method returns a String containing information about the current state of the game. The returned string begins with the score of team 1, followed by a hyphen ("-"), followed by the score of team 2, followed by a hyphen, followed by the name of the team that is currently active.

The following table contains a sample code execution sequence and the corresponding results. The code execution sequence appears in a class other than Scoreboard.

Scoreboard Table

Scoreboard Table

Exercise 2

Write the complete Scoreboard class. Your implementation must meet all specifications and conform to the examples shown in the preceding table.

Open the 02-frq2-2024-scoreboard folder and edit Main.java. Press Run to try it; Run Tests checks it (10 tests). Name the class, then rename the file to match.

For teachers

FRQ 2 - Class Design - Part 1

Students read CSAwesome2's “FRQ 2 - Class Design - Part 1” in CS Room and work through its 2 Java coding exercises.

  • Time: 1 class period
  • Tests: 16 in 2 graded exercises, set up for you when you import the lesson

Goals

  • Practice for the AP exam's free-response questions.

Before class

In class

  1. Students read README.md.
  2. Students do exercises 01–02, each in its own folder, pressing Run to try their code.
  3. Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.

Exercises

FolderFileGradingTests
01-APLineFRQMain.javaAutograded6
02-frq2-2024-scoreboardMain.javaAutograded10

Teachers can download a reference solution after signing in.