Skip to main content
Logo image

Section 15.2 FRQ 1A - Methods and Control - Part 2

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.2.1 If Statements and Conditions

If statements can be used to change the behavior of a method. The basic syntax is:
if(/* condition */) {
    this occurs if the condition is true
}
If statements can also have else statements. An else is not required. The basic syntax is:
if(/* condition */) {
    /* this occurs if the condition is true */
}
else  // note that the else does not have () or a condition
{
    /* this occurs if the condition is false */
}
Remember that conditional statements can include many things:
  • comparison of a variable to a value using <, >, <=, >=, ==, !=
  • multiple conditions combined with && (and), || (or), and/or ! (not)
  • calls to a method that returns a boolean value
  • calls to a method that returns an int or double value with a comparison operator
Examples:

Subsection 15.2.2 2025 FRQ: Dog Walker Part (a)

This FRQ requires you to call multiple methods and use an if statement.
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 two methods 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) {
        /* to be implemented in part (a) */
    }

    /* There may be instance variables, constructors, and methods that are not shown. */
}
Write the walkDogs method, which updates and returns the number of dogs this dog walker walks during the time specified by hour. Values of hour range from 0 to 23, inclusive.
A helper method, numAvailableDogs, has been provided in the DogWalkCompany class. The method returns the number of dogs available to be taken for a walk at a given hour. The dog walker will always walk as many dogs as the dog-walking company has available to walk, as long as the available number of dogs is not greater than the maximum number of dogs that the dog walker can handle, represented by the value of maxDogs.
Another helper method, updateDogs, has also been provided in the DogWalkCompany class. So that multiple dog walkers do not sign up to walk the same dogs, the walkDogs method should use updateDogs to update the dog-walking company with the number of dogs this dog walker will walk during the given hour. The parameters of the updateDogs method indicate how many dogs this dog walker will walk during the time specified by hour.
For example, if the dog-walking company has 10 dogs that need to be walked at the given hour but the dog walker’s maximum is 4, the updateDogs method should be used to indicate that this dog walker will walk 4 of the 10 dogs during the given hour. As another example, if the dog-walking company has 3 dogs that need to be walked at the given hour and the dog walker’s maximum is 4, the updateDogs method should be used to indicate that this dog walker will walk all 3 of the available dogs during the given hour.
The walkDogs method should return the number of dogs to be walked by this dog walker during the time specified by hour.
You must use numAvailableDogs and updateDogs appropriately to receive full credit.

Activity 15.2.1.

What is the return type of walkDogs?
  • void
  • walkDogs should return the number of dogs that are walked, so it should return something
  • int
  • walkDogs should return the number of dogs that are walked, so it should return an int
  • double
  • walkDogs should return the number of dogs that are walked, so it should return a whole number
  • boolean
  • walkDogs should return the number of dogs that are walked, so true/false wouldn’t work

Activity 15.2.2.

How do you call numAvailableDogs and store the result as a variable?
  • int dogsToWalk = numAvailableDogs(hour)
  • numAvailableDogs is a method in the DogWalkCompany class and needs to be called on an object
  • int dogsToWalk = this.numAvailableDogs(hour)
  • numAvailableDogs is a method in the DogWalkCompany class and cannot be called on this
  • int dogsToWalk = DogWalkCompany.numAvailableDogs(hour)
  • numAvailableDogs is a method in the DogWalkCompany class and needs to be called on an object
  • int dogsToWalk = company.numAvailableDogs(hour)
  • numAvailableDogs is a method in the DogWalkCompany class and needs to be called on an object (company)

Activity 15.2.3.

How do you determine how many dogs were walked?
  • it will always be the number of available dogs
  • it will always be the maximum number of dogs
  • if the maximum number of dogs is greater than the number of dogs available, the number of dogs walked will be the number available
  • if the number of dogs available is greater than the maximum number of dogs, the number of dogs walked wll be the maximum number of dogs

Activity 15.2.4.

How do you call updateDogs?
  • company.updateDogs(hour, dogsToWalk);
  • DogWalkCompany.updateDogs(hour, dogsToWalk);
  • this.updateDogs(hour, dogsToWalk);
  • updateDogs(hour, dogsToWalk);

Activity 15.2.5.

Complete method walkDogs. You must use numAvailableDogs and updateDogs appropriately to receive full credit.

Subsection 15.2.3 Nested If Statements

Nested if statements allow for multiple conditions to be tested. It is very important that you use curly braces to indicate which parts of an if statement are connected to other parts.
if(/* condition1 */) {
    if(/* condition1 */) {
        /* this occurs if both conditions are true */
    } else {
        /* this occurs if condition1 is true, but condition2 is false */
    }
}
else // note that the else does not have () or a condition
{ 
    if(/* condition3 */) {
        /* this occurs if condition1 is false but condition3 is true */
    } else {
        /* this occurs if both conditions are false */
    }
}
Note that the else is not always needed.
The following FRQ uses nested logic to determine the final score of a game.

Subsection 15.2.4 2022 FRQ: Game Part (a)

This question involves simulation of the play and scoring of a single-player video game. In the game, a player attempts to complete three levels. A level in the game is represented by the Level class.
public class Level {
    /** Returns true if the player reached the goal on this level and returns false otherwise */
    public boolean goalReached() {
        /* implementation not shown */
    }

    /** Returns the number of points (a positive integer) recorded for this level */
    public int getPoints() {
        /* implementation not shown */
    }

    // There may be instance variables, constructors, and methods that are not shown.
}
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 */
    }

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

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

Activity 15.2.6.

Write the getScore method, which returns the score for the most recently played game. Each game consists of three levels. The score for the game is computed using the following helper methods.
  • The isBonus method of the Game class returns true if this is a bonus game and returns false otherwise.
  • The goalReached method of the Level class returns true if the goal has been reached on a particular level and returns false otherwise.
  • The getPoints method of the Level class returns the number of points recorded on a particular level. Whether or not recorded points are earned (included in the game score) depends on the rules of the game, which follow.
The score for the game is computed according to the following rules.
  • Level one points are earned only if the level one goal is reached. Level two points are earned only if both the level one and level two goals are reached. Level three points are earned only if the goals of all three levels are reached.
  • The score for the game is the sum of the points earned for levels one, two, and three.
  • If the game is a bonus game, the score for the game is tripled.
Figure 15.2.1.
Complete the getScore method.

Subsection 15.2.5 Random Behavior

The Math.random() method allows for interesting random behavior.
Using Math.random() in a condition allows something to happen a certain percentage of the time. For example, if (Math.random() < 0.10) will cause something to happen 10% of the time.
Math.random() can also be used to generate a random number in a specific range. There are some important things to keep in mind.
  • Math.random() returns a random double (decimal) between 0 and 0.999999. It will never return 1.
  • Math.random()*range returns a random double between 0 and range, not including range.
  • Math.random()*range + min returns a random number from min to (min+range), exclusive.
  • (int) (Math.random()*range + min) returns a random integer from min to (min+range), exclusive.
The 2024 FRQ Feeder uses random numbers in both of these ways to make interesting things happen.

Subsection 15.2.6 2024 FRQ: Feeder Part (a)

This question simulates birds or possibly a bear eating at a bird feeder. The following Feeder class contains information about how much food is in the bird feeder and simulates how much food is eaten. You will write two methods of the Feeder class.
public class Feeder {
    /**
     * The amount of food, in grams, currently in the bird feeder; initialized in the constructor
     * and always greater than or equal to zero
     */
    private int currentFood;

    /**
     * Simulates one day with numBirds birds or possibly a bear at the bird feeder, as described in
     * part (a) Precondition: numBirds > 0
     */
    public void simulateOneDay(int numBirds) {
        /* to be implemented in part (a) */
    }

    /**
     * Returns the number of days birds or a bear found food to eat at the feeder in this
     * simulation, as described in part (b) Preconditions: numBirds > 0, numDays > 0
     */
    public int simulateManyDays(int numBirds, int numDays) {
        /* to be implemented in part (b) */
    }
    // There may be instance variables, constructors, or methods that are not shown.
}
Write the simulateOneDay method, which simulates numBirds birds or possibly a bear at the feeder for one day. The method determines the amount of food taken from the feeder on this day and updates the currentFood instance variable. The simulation accounts for normal conditions, which occur 95% of the time, and abnormal conditions, which occur 5% of the time.
Under normal conditions, the simulation assumes that on any given day, only birds visit the feeder and that each bird at the feeder consumes the same amount of food. This standard amount consumed is between 10 and 50 grams of food, inclusive, in 1-gram increments. That is, on any given day, each bird might eat 10, 11, . . . , 49, or 50 grams of food. The amount of food eaten by each bird on a given day is randomly generated and each integer from 10 to 50, inclusive, has an equal chance of being chosen.
For example, a run of the simulation might predict that for a certain day under normal conditions, each bird coming to the feeder will eat 11 grams of food. If 10 birds come to the feeder on that day, then a total of 110 grams of food will be consumed.

Activity 15.2.7.

Write the simulateOneDay method, which simulates numBirds birds or possibly a bear at the feeder for one day. The method determines the amount of food taken from the feeder on this day and updates the currentFood instance variable. The simulation accounts for normal conditions, which occur 95% of the time, and abnormal conditions, which occur 5% of the time.
Under normal conditions, the simulation assumes that on any given day, only birds visit the feeder and that each bird at the feeder consumes the same amount of food. This standard amount consumed is between 10 and 50 grams of food, inclusive, in 1-gram increments. That is, on any given day, each bird might eat 10, 11, . . . , 49, or 50 grams of food. The amount of food eaten by each bird on a given day is randomly generated and each integer from 10 to 50, inclusive, has an equal chance of being chosen.
For example, a run of the simulation might predict that for a certain day under normal conditions, each bird coming to the feeder will eat 11 grams of food. If 10 birds come to the feeder on that day, then a total of 110 grams of food will be consumed.
If the simulated food consumed is greater than the amount of food in the feeder, the birds empty the feeder and the amount of food in the feeder at the end of the day is zero.
Under abnormal conditions, a bear empties the feeder and the amount of food in the feeder at the end of the day is zero.
The following examples show possible results of three calls to simulateOneDay.
  • Example 1: If the feeder initially contains 500 grams of food, the call simulateOneDay(12) could result in 12 birds eating 20 grams of food each, leaving 260 grams of food in the feeder.
  • Example 2: If the feeder initially contains 1,000 grams of food, the call simulateOneDay(22) could result in a bear eating all the food, leaving 0 grams of food in the feeder.
  • Example 3: If the feeder initially contains 100 grams of food, the call simulateOneDay(5) could result in 5 birds attempting to eat 30 grams of food each. Since the feeder initially contains less than 150 grams of food, the feeder is emptied, leaving 0 grams of food in the feeder.
You have attempted of activities on this page.