AP CSA FRQ Practice
I will practice my FRQs here.
The APCalendar class contains methods used to calculate information about a calendar. You will write two 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 in part (a) */ }
/** 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 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.
/** Returns the number of leap years between year1 and year2, inclusive.
-
Precondition: 0 <= year1 <= year2 */
public static int numberOfLeapYears(int year1, int year2)
public static int numberOfLeapYears(int year1, int year2){
int numyear = 0;
if(isLeapYear(int year)){
numyear ++;
}
}
return numyear;
I was a bit confused about how to go through each year and how to reference this when checking whether each year is a leap year.
Correct Answer:
//keep track of number of leap years
//loop through year 1 to year 2
public static int numberOfLeapYears(int year1, int year2)
{
int count = 0;
for (int y = year1; y <= year2; y++) //loop through the years as long as they are less than or equal to year 2 -> keep adding to years
{
if (isLeapYear(y)) //if the year is a leap year
{
count++; //increase count variable by 1
}
}
return count; //return count (the number of times a leap year was found)
}
Score: 3/5
- Initialize numeric variable (numyear)
- Loop through each necessary year in range
- Calls isLeapYear on valid year in range
- Update count based on result of isLeapYear
- Return count of leap years
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.
public static int dayOfWeek(int year, int day)
{
int countday1 = firstDayOfYear(year);
int countDay = dayOfYear(month, day);
int = (countday1 + countDay) % 7;
}
Answer:
public static int dayOfWeek(int month, int day, int year)
{
int startDay = firstDayOfYear(year); //find first day of year
int nthDay = dayOfYear(month, day, year); //find day of year
int returnDay = (startDay + nthDay - 1) % 7; // the start and last day get added and -1 (because of Sunday) and /7 in order to find which day of the week it is
return returnDay; //the returned number will tell us how many days away it is from the start day
}
Score: 3/5
Need to add integer for month number Need to return value Wrong calculations