Skip to main content
Logo image

Section 15.3 FRQ 1A - Methods and Control - Part 3

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.

Subsection 15.3.1 For Loops

The syntax of a for loop is like this:
for (int i = start; i < end; i++) {
  /* code you want to repeat */
}
Typically the start value is whatever provided and the end value is not included. If you want to include the end, change the < to <=.
For loops are most often used when the problem requires the code to repeat over a range of values. Several common algorithms that use for loops and if statements are:
  • calculating the sum or average of a set of values
  • counting the number of times a condition is met within a range of values
  • calculating the minimum or maximum value in a set of values
Many of these algorithms fit the accumulator pattern: an algorithm that loops through a set of values and updates an accumulator variable based on those values.
There are four steps to the accumulator pattern:
  • initialize the accumulator variable before the loop
  • loop through the values
  • update the accumulator variable inside the loop
  • return the accumulated value (if necessary)
The following FRQs can all be solved with the accumulator pattern and for loops.

Subsection 15.3.2 2019 FRQ - APCalendar Part (a)

The APCalendar class contains methods used to calculate information about a calendar. You will write one methods of the class.
public class APCalendar {
    /** Returns true if year is a leap year and false otherwise. */
    private static boolean isLeapYear(int year) {
        /* implementation not shown */
    }

    /**
     * Returns the number of leap years between year1 and year2, inclusive. Precondition: 0 <= year1
     * <= year2
     */
    public static int numberOfLeapYears(int year1, int year2) {
        /* to be implemented below */
    }

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

Activity 15.3.1.

Write the static method numberOfLeapYears, which returns the number of leap years between year1 and year2, inclusive.
In order to calculate this value, a helper method is provided for you.
  • isLeapYear(year) returns true if year is a leap year and false otherwise.
Complete method numberOfLeapYears below. You must use isLeapYear appropriately to receive full credit.

Subsection 15.3.3 2022 FRQ - Game Part (b)

Play of the game is represented by the Game class. You will write one method of the Game class.
public class Game {
    private Level levelOne;
    private Level levelTwo;
    private Level levelThree;

    /** Postcondition: All instance variables have been initialized. */
    public Game() {
        /* implementation not shown */
    }

    /** Returns true if this game is a bonus game and returns false otherwise */
    public boolean isBonus() {
        /* implementation not shown */
    }

    /**
     * Simulates the play of this Game (consisting of three levels) and updates all relevant game
     * data
     */
    public void play() {
        /* implementation not shown */
    }

    /** Returns the score earned in the most recently played game, as described in part (a) */
    public int getScore() {
        /* to be implemented in part (a) */
    }

    /**
     * Simulates the play of num games and returns the highest score earned, as described in part
     * (b) Precondition: num > 0
     */
    public int playManyTimes(int num) {
        /* to be implemented below */
    }

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

Activity 15.3.2.

Write the playManyTimes method, which simulates the play of num games and returns the highest game score earned. For example, if the four plays of the game that are simulated as a result of the method call playManyTimes(4) earn scores of 75, 50, 90, and 20, then the method should return 90.
Play of the game is simulated by calling the helper method play. Note that if play is called only one time followed by multiple consecutive calls to getScore, each call to getScore will return the score earned in the single simulated play of the game.
Complete the playManyTimes method. Assume that getScore works as intended. You must call play and getScore appropriately in order to receive full credit.

Subsection 15.3.4 2025 FRQ - DogWalker Part (b)

This question involves dog walkers who are paid through a dog-walking company to take dogs for one-hour walks. A dog-walking company has a varying number of dogs that need to be walked during each hour of the day. A dog-walking company is represented by the following DogWalkCompany class.
public class DogWalkCompany {
    /**
     * Returns the number of dogs, always greater than 0, that are available for a walk during the
     * time specified by hour Precondition: 0 <= hour <= 23
     */
    public int numAvailableDogs(int hour) {
        /* implementation not shown */
    }

    /**
     * Decreases, by numberDogsWalked, the number of dogs available for a walk during the time
     * specified by hour Preconditions: 0 <= hour <= 23 numberDogsWalked > 0
     */
    public void updateDogs(int hour, int numberDogsWalked) {
        /* implementation not shown */
    }

    /* There may be instance variables, constructors,
    and methods that are not shown. */
}
A dog walker is associated with a dog-walking company and is represented by the DogWalker class. You will write one method of the DogWalker class.
public class DogWalker {
    /** The maximum number of dogs this walker can walk simultaneously per hour */
    private int maxDogs;

    /** The dog-walking company this dog walker is associated with */
    private DogWalkCompany company;

    /** Assigns max to maxDogs and comp to company Precondition: max > 0 */
    public DogWalker(int max, DogWalkCompany comp) {
        /* implementation not shown */
    }

    /**
     * Takes at least one dog for a walk during the time specified by hour, as described in part (a)
     * Preconditions: 0 <= hour <= 23 maxDogs > 0
     */
    public int walkDogs(int hour) {
        /* implementation not shown */
    }

    /**
     * Performs an entire dog-walking shift and returns the amount earned, in dollars, as described
     * in part (b) Preconditions: 0 <= startHour <= endHour <= 23 maxDogs > 0
     */
    public int dogWalkShift(int startHour, int endHour) {
        /* to be implemented below */
    }

    /* There may be instance variables, constructors, and methods that are not shown. */
}

Activity 15.3.3.

Write the dogWalkShift method, which performs a dog-walking shift of the hours in the range startHour to endHour, inclusive, and returns the total amount earned. For example, a dog-walking shift from 14 to 16 is composed of three one-hour dog walks starting at hours 14, 15, and 16.
For each hour, the base pay is $5 per dog walked plus a bonus of $3 if at least one of the following is true.
  • maxDogs dogs are walked
  • the walk occurs between the peak hours of 9 and 17, inclusive
The following table shows an example of the calculated amount earned by walking dogs from hour 7 through hour 10, inclusive.
Figure 15.3.1.
Complete method dogWalkShift. Assume that walkDogs works as specified. You must use walkDogs appropriately to earn full credit.

Subsection 15.3.5 While Loops

While loops are used when we are unsure of how many times the loop will need to repeat. It will based on a condition, similar to an if statement that repeats.
while (/* condition */) {
  /* code you want to repeat */
}
The loop continues as long as the condition is true. It will stop once the condition is false. If the condition is never false, the loop will repeat infinitely.
The following examples show two different implementations of while loops being used in an FRQ.

Subsection 15.3.6 2007 FRQ - Self Divisor - Part (a)

This question involves a class called SelfDivisor.
public class SelfDivisor {
    /**
     * @param number the number to be tested Precondition: number > 0
     * @return true if every decimal digit of number is a divisor of number; false otherwise
     */
    public static boolean isSelfDivisor(int number) {
        /* to be implemented below */
    }

    // There may be fields, constructors, and methods that are not shown.
}
A positive integer is called a "self-divisor" if every decimal digit of the number is a divisor of the number, that is, the number is evenly divisible by each and every one of its digits. For example, the number 128 is a selfdivisor because it is evenly divisible by 1, 2, and 8. However, 26 is not a self-divisor because it is not evenly divisible by the digit 6. Note that 0 is not considered to be a divisor of any number, so any number containing a 0 digit is NOT a self-divisor. There are infinitely many self-divisors.
The algorithm for this problem is:
  • Repeat while the number is greater than 0
  • Make a new variable num that is the same as number
  • Repeat while num is greater than 0
    • Access the right-most digit of number using num % 10
    • Make a new variable digit that is set to right-most digit of num using num % 10
    • Do something with the right-most digit. In this case, if the original number is not divisible by digit, return false
    • Remove the right-most digit of number by dividing number by 10 (num / 10)
    • Remove the right-most digit of num by dividing number by 10 (num / 10)
  • If it makes it all the way through the loop without returning false, return true
To access the right-most digit, you should use num % 10. To access the rest of the digit, you should use num / 10.

Activity 15.3.4.

A positive integer is called a "self-divisor" if every decimal digit of the number is a divisor of the number, that is, the number is evenly divisible by each and every one of its digits. For example, the number 128 is a selfdivisor because it is evenly divisible by 1, 2, and 8. However, 26 is not a self-divisor because it is not evenly divisible by the digit 6. Note that 0 is not considered to be a divisor of any number, so any number containing a 0 digit is NOT a self-divisor. There are infinitely many self-divisors.
Write method isSelfDivisor, which takes a positive integer as its parameter. This method returns true if the number is a self-divisor; otherwise, it returns false.

Subsection 15.3.7 2025 FRQ - Message Builder Part (a)

This FRQ also uses a while loop in a manner similar to getting input from a user or a file.

Activity 15.3.5.

Complete the MessageBuilder constructor below that builds a message starting with the startingWord specified by the parameter. It should use a while loop to add on to the message using the given getNextWord method and increment numWords. Make sure there are spaces between the words in the message.
You have attempted of activities on this page.