FRQ 1A - Methods and Control - Part 1

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

Java1 class period2 exercises7 graded tests

Files

README.mdDownload

FRQ 1A - Methods and Control - 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 1: Methods and Control Structures. Students will write two methods or one constructor and one method of a given class based on the provided specifications and examples. In Part A (4 points), the method or constructor will require students to write iterative or conditional statements, or both, as well as statements that call methods in the specified class. In Part B (3 points), the method or constructor will require calling String methods.

FRQ 1A requires the following skills:

  • Complete a method or a constructor
  • Call a method in the same class or from an object
  • Use if statements
  • Use a for loop or a while loop

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.

Return Types and Method Headers

The return type determines the type of information the method returns.

Frq1a part1 returntypes

Recommendation: If you have a return type, create a variable of that type, set it to the default value, and immediately return it. Then add code in the middle!!!

Calling Methods

Some FRQs will give you methods that you need to call in order to get points. Sometimes they will be in the same class as your method. Sometimes, you will need to call the methods on an object.

When methods are in the same class, you just need to call the method name and include the proper parameters. When methods are in different classes, you use an object to call the method with dot notation and include the proper parameters. If the method returns a value, make sure you are storing the value somehow.

Example: The orderCoffee and calculateTotal methods are in the same class, so you can call them with the method name. Make sure you include the correct parameters.

public class CoffeeShop
{
    public double orderCoffee(int quantity, double pricePerCup)
    {
        // Calls the calculateTotal method and stores the return value as total
        double total = calculateTotal(quantity, pricePerCup);
        return total * 1.10;
    }

    public double calculateTotal(int count, double price)
    {
        return count * price;
    }
}

Remember that when a method returns a value, you must store it as a variable in order to use it again!

2019 FRQ - AP Calendar - Part (b)

The APCalendar class contains methods used to calculate information about a calendar. You will write one method of the class.

public class APCalendar
{
    /** Returns the value representing the day of the week for the first day of year,
    * where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday.
    */
    private static int firstDayOfYear(int year)
    { /* implementation not shown */ }

    /** Returns n, where month, day, and year specify the nth day of the year.
    * Returns 1 for January 1 (month = 1, day = 1) of any year.
    * Precondition: The date represented by month, day, year is a valid date.
    */
    private static int dayOfYear(int month, int day, int year)
    { /* implementation not shown */ }

    /** Returns the value representing the day of the week for the given date
    * (month, day, year), where 0 denotes Sunday, 1 denotes Monday, ...,
    * and 6 denotes Saturday.
    * Precondition: The date represented by month, day, year is a valid date.
    */
    public static int dayOfWeek(int month, int day, int year)
    { /* to be implemented in part (b) */ }

    // There may be instance variables, constructors, and other methods not shown.
}

Write the static method dayOfWeek, which returns the integer value representing the day of the week for the given date (month, day, year), where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday.

For example, 2019 began on a Tuesday, and January 5 is the fifth day of 2019. As a result, January 5, 2019, fell on a Saturday, and the method call dayOfWeek(1, 5, 2019) returns 6.

As another example, January 10 is the tenth day of 2019. As a result, January 10, 2019, fell on a Thursday, and the method call dayOfWeek(1, 10, 2019) returns 4.

In order to calculate this value, two helper methods are provided for you.

  • firstDayOfYear(year) returns the integer value representing the day of the week for the first day of year, where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday. For example, since 2019 began on a Tuesday, firstDayOfYear(2019) returns 2.
  • dayOfYear(month, day, year) returns n, where month, day, and year specify the nth day of the year. For the first day of the year, January 1 (month = 1, day = 1), the value 1 is returned. This method accounts for whether year is a leap year. For example, dayOfYear(3, 1, 2017) returns 60, since 2017 is not a leap year, while dayOfYear(3, 1, 2016) returns 61, since 2016 is a leap year.

This table shows how the calculations would be made:

Frq1a part1 apcalendar b table

Check your understanding: What is the return type of the dayOfWeek method? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ1A-Part1.html#frq1a1-chooseType1

Check your understanding: The dayOfWeek method includes the parameters month, day, year. What is the correct way to call the firstDayOfYear method? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ1A-Part1.html#frq1a1-chooseType2

Check your understanding: The dayOfWeek method includes the parameters month, day, year. What is the correct way to call the dayOfYear method? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ1A-Part1.html#frq1a1-chooseType3

Exercise 1

Complete the method dayOfWeek. You must use firstDayOfYear and dayOfYear appropriately to receive full credit.

You could do this on a single line or several lines.

Open the 01-FRQ1A-2019-APCalendar-B folder and edit APCalendar.java. Press Run to try it; Run Tests checks it (3 tests).

Calling Static Methods

Some methods are static methods and should be called using the class name instead of an object.

Math methods are static methods:

  • Math.random()
  • Math.pow(x, y)
  • Math.abs(x)

Calling Methods on Objects

Sometimes you need to call methods on objects. In most FRQs, these objects will already exist. We will specifically look at calling String methods later.

To call a method on an object, you use objectName.methodName(parameters).

Example: The Match class has two instance variables team1 and team2 of type Team. We can call the getScore() method that is part of Team using the name of the team object and the method name: team1.getScore() and team2.getScore().

public class Team {
    public int getScore() { /* Implementation not shown */ }
}

public class Match {
    Team team1, team2;

    public int determineWinner() {
        if (team1.getScore() > team2.getScore())
            return 1;
        else if (team1.getScore() < team2.getScore())
            return 2;
        return 0;
    }
}

2022 FRQ - Game and Level

In this simplified version of this FRQ, you will use the 3 Level objects for the 3 levels of the game and then call their getPoints() methods to calculate the score for the game.

You are given the following classes:

public class Level
{
    /** Returns the number of points recorded for this level */
    public int getPoints()
    { return points; }
}

public class Game
{
    private Level levelOne, levelTwo, levelThree;

    public int getScore()
    { /* to be implemented in this question */}
}

The score is calculated by adding the points from each level, but in this version, level 2 points are doubled, and level 3 points are tripled. You are to write the getScore method in Game.

Check your understanding: What is the return type of this method? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ1A-Part1.html#frq1a1-chooseType4

Check your understanding: How do you call the getPoints() method for levelOne? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ1A-Part1.html#frq1a1-chooseType5

Exercise 2

Complete the getScore method. The score is calculated by adding the points from each level, but in this version, level 2 points are doubled, and level 3 points are tripled.

Open the 02-FRQ-2022-Game-Score-Part0 folder and edit Game.java. Press Run to try it; Run Tests checks it (4 tests).

For teachers

FRQ 1A - Methods and Control - Part 1

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

  • Time: 1 class period
  • Tests: 7 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. Questions that need CS Awesome's interactive widgets link back to it.
  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-FRQ1A-2019-APCalendar-BAPCalendar.javaAutograded3
02-FRQ-2022-Game-Score-Part0Game.javaAutograded4

Teachers can download a reference solution after signing in.