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.
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!!!
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 pass the necessary arguments. When methods are in different classes, you use an object to call the method with dot notation and pass the necessary arguments. If the method returns a value, make sure you are storing or using 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 pass appropriate arguments.
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!
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.
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.
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.
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;
}
}
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.
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.
Complete the getScore method below. 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.