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.
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:
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.
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 */
}
}
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.
}
Activity15.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 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.
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.
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.
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.
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.
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.