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.
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:
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.
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.
}
Activity15.3.1.
Write the static method numberOfLeapYears, which returns the number of leap years between year1 and year2, inclusive.
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.
}
Activity15.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.
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. */
}
Activity15.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.
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.
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.
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.
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.