The following is a free response question from 2014. It was question 4 on the exam. You can see all the free response questions from past exams at Past AP CSA Exams.
Question 4. The menu at a lunch counter includes a variety of sandwiches, salads, and drinks. The menu also allows a customer to create a βtrio,β which consists of three menu items: a sandwich, a salad, and a drink. The price of the trio is the sum of the two highest-priced menu items in the trio; one item with the lowest price is free. Each menu item has a name and a price. The four types of menu items are represented by the four classes Sandwich, Salad, Drink, and Trio. All four classes implement the following MenuItem interface.
Interfaces are no longer on the AP CSA exam, but you can just treat an interface like a superclass. Just substitute the word class instead of interface below and your TrioClass can extend MenuItem instead of implementing the interface.
public interface MenuItem
{
/**
* @return the name of the menu item
*/
String getName();
/**
* @return the price of the menu item
*/
double getPrice();
}
The following diagram shows the relationship between the MenuItem interface and the Sandwich, Salad, Drink, and Trio classes.
For example, assume that the menu includes the following items. The objects listed under each heading are instances of the class indicated by the heading.
The menu allows customers to create Trio menu items, each of which includes a sandwich, a salad, and a drink. The name of the Trio consists of the names of the sandwich, salad, and drink, in that order, each separated by β/β and followed by a space and then βTrioβ. The price of the Trio is the sum of the two highest-priced items in the Trio; one item with the lowest price is free. A trio consisting of a cheeseburger, spinach salad, and an orange soda would have the name "Cheeseburger/Spinach Salad/Orange Soda Trio" and a price of $4.00 (the two highest prices are $2.75 and $1.25). Similarly, a trio consisting of a club sandwich, coleslaw, and a cappuccino would have the name "Club Sandwich/Coleslaw/Cappuccino Trio" and a price of $6.25 (the two highest prices are $2.75 and $3.50).
Write the Trio class (near the end of the code below) that implements the MenuItem interface (which is like extending a class). Your implementation must include a constructor that takes three parameters representing a sandwich, salad, and drink. The main method has code to test the result.
The following is a free response question from 2014. It was question 4 on the exam. You can see all the free response questions from past exams at Past AP CSA Exams.
Question 4. The menu at a lunch counter includes a variety of sandwiches, salads, and drinks. The menu also allows a customer to create a βtrio,β which consists of three menu items: a sandwich, a salad, and a drink. The price of the trio is the sum of the two highest-priced menu items in the trio; one item with the lowest price is free. Each menu item has a name and a price. The four types of menu items are represented by the four classes Sandwich, Salad, Drink, and Trio. All four classes implement the following MenuItem interface.
public interface MenuItem
{
/**
* @return the name of the menu item
*/
String getName();
/**
* @return the price of the menu item
*/
double getPrice();
}
The following diagram shows the relationship between the MenuItem interface and the Sandwich, Salad, Drink, and Trio classes.
For example, assume that the menu includes the following items. The objects listed under each heading are instances of the class indicated by the heading.
The menu allows customers to create Trio menu items, each of which includes a sandwich, a salad, and a drink. The name of the Trio consists of the names of the sandwich, salad, and drink, in that order, each separated by β/β and followed by a space and then βTrioβ. The price of the Trio is the sum of the two highest-priced items in the Trio; one item with the lowest price is free. A trio consisting of a cheeseburger, spinach salad, and an orange soda would have the name "Cheeseburger/Spinach Salad/Orange Soda Trio" and a price of $4.00 (the two highest prices are $2.75 and $1.25). Similarly, a trio consisting of a club sandwich, coleslaw, and a cappuccino would have the name "Club Sandwich/Coleslaw/Cappuccino Trio" and a price of $6.25 (the two highest prices are $2.75 and $3.50).
The following is a free response question from 2014. It was question 4 on the exam. You can see all the free response questions from past exams at Past AP CSA Exams.
Question 4. The menu at a lunch counter includes a variety of sandwiches, salads, and drinks. The menu also allows a customer to create a βtrio,β which consists of three menu items: a sandwich, a salad, and a drink. The price of the trio is the sum of the two highest-priced menu items in the trio; one item with the lowest price is free. Each menu item has a name and a price. The four types of menu items are represented by the four classes Sandwich, Salad, Drink, and Trio. All four classes implement the following MenuItem interface.
public interface MenuItem
{
/**
* @return the name of the menu item
*/
String getName();
/**
* @return the price of the menu item
*/
double getPrice();
}
The following diagram shows the relationship between the MenuItem interface and the Sandwich, Salad, Drink, and Trio classes.
For example, assume that the menu includes the following items. The objects listed under each heading are instances of the class indicated by the heading.
The menu allows customers to create Trio menu items, each of which includes a sandwich, a salad, and a drink. The name of the Trio consists of the names of the sandwich, salad, and drink, in that order, each separated by β/β and followed by a space and then βTrioβ. The price of the Trio is the sum of the two highest-priced items in the Trio; one item with the lowest price is free. A trio consisting of a cheeseburger, spinach salad, and an orange soda would have the name "Cheeseburger/Spinach Salad/Orange Soda Trio" and a price of $4.00 (the two highest prices are $2.75 and $1.25). Similarly, a trio consisting of a club sandwich, coleslaw, and a cappuccino would have the name "Club Sandwich/Coleslaw/Cappuccino Trio" and a price of $6.25 (the two highest prices are $2.75 and $3.50).
This solution doesnβt include the "/" between the sandwich and salad and between the salad and the drink and is also missing the "Trio" at the end of the name, so it loses this point.
The following is a free response question from 2014. It was question 4 on the exam. You can see all the free response questions from past exams at Past AP CSA Exams.
Question 4. The menu at a lunch counter includes a variety of sandwiches, salads, and drinks. The menu also allows a customer to create a βtrio,β which consists of three menu items: a sandwich, a salad, and a drink. The price of the trio is the sum of the two highest-priced menu items in the trio; one item with the lowest price is free. Each menu item has a name and a price. The four types of menu items are represented by the four classes Sandwich, Salad, Drink, and Trio. All four classes implement the following MenuItem interface.
public interface MenuItem
{
/**
* @return the name of the menu item
*/
String getName();
/**
* @return the price of the menu item
*/
double getPrice();
}
The following diagram shows the relationship between the MenuItem interface and the Sandwich, Salad, Drink, and Trio classes.
For example, assume that the menu includes the following items. The objects listed under each heading are instances of the class indicated by the heading.
The menu allows customers to create Trio menu items, each of which includes a sandwich, a salad, and a drink. The name of the Trio consists of the names of the sandwich, salad, and drink, in that order, each separated by β/β and followed by a space and then βTrioβ. The price of the Trio is the sum of the two highest-priced items in the Trio; one item with the lowest price is free. A trio consisting of a cheeseburger, spinach salad, and an orange soda would have the name "Cheeseburger/Spinach Salad/Orange Soda Trio" and a price of $4.00 (the two highest prices are $2.75 and $1.25). Similarly, a trio consisting of a club sandwich, coleslaw, and a cappuccino would have the name "Club Sandwich/Coleslaw/Cappuccino Trio" and a price of $6.25 (the two highest prices are $2.75 and $3.50).
Section5.9.5Retired FRQ - Inheritance - NumberGroup - Part B
Part b. A range represents a number group that contains all (and only) the integers between a minimum value and a maximum value, inclusive. Write the Range class, which is a NumberGroup. The Range class represents the group of int values that range from a given minimum value up through a given maximum value, inclusive. For example, the declaration NumberGroup range1 = new Range(-3, 2); represents the group of integer values -3, -2, -1, 0, 1, 2.
Write the complete Range class. Include all necessary instance variables and methods as well as a constructor that takes two int parameters. The first parameter represents the minimum value, and the second parameter represents the maximum value of the range. You may assume that the minimum is less than or equal to the maximum. Write the contains method which returns true or false if a given int argument is within the range set up by the constructor.
Section5.9.6Retired FRQ - Inheritance - NumberGroup - Part C
Part c. The MultipleGroups class represents a collection of NumberGroup objects and is a NumberGroup. The MultipleGroups class stores the number groups in the instance variable groupList (shown below), which is initialized in the constructor.
Write the MultipleGroups method contains. The method takes an integer and returns true if and only if the integer is contained in one or more of the number groups in groupList.
Write the method contains below in the class MultiGroups. (Note that the original AP Question involved a NumberGroup interface which has been replaced with inheritance here).
Section5.9.7Retired FRQ - Arrays - ArrayTester - Part A
The following is a free response question from 2018. It was question 4 on the exam. You can see all the free response questions from past exams at Past AP CSA Exams.
Question 4. This question involves reasoning about arrays of integers. You will write two static methods, both of which are in a class named ArrayTester.
public class ArrayTester
{
/**
* Returns an array containing the elements of column c of arr2D in the same
* order as they appear in arr2D. Precondition: c is a valid column index in
* arr2D. Postcondition: arr2D is unchanged.
*/
public static int[] getColumn(int[][] arr2D, int c)
{
/* to be implemented in part (a) */
}
/**
* Returns true if and only if every value in arr1 appears in arr2.
* Precondition: arr1 and arr2 have the same length. Postcondition: arr1 and
* arr2 are unchanged.
*/
public static boolean hasAllValues(int[] arr1, int[] arr2)
{
/* implementation not shown */
}
/** Returns true if arr contains any duplicate values; false otherwise. */
public static boolean containsDuplicates(int[] arr)
{
/* implementation not shown) */
}
/**
* Returns true if square is a Latin square as described in part (b); false
* otherwise. Precondition: square has an equal number of rows and columns.
* Precondition: square has at least one row.
*/
public static boolean isLatin(int[][] square)
{
/* to be implemented in part (b) */
}
}
Part a. Write a static method getColumn, which returns a one-dimensional array containing the elements of a single column in a two-dimensional array. The elements in the returned array should be in the same order as they appear in the given column. The notation arr2D [r][c] represents the array at row r and column c.
A two-dimensional square array of integers is a Latin square if the following conditions are true. 1) The first row has no duplicate values. 2) All values in the first row of the square appear in each row of the square. 3) All values in the first row of the square appear in each column of the square.
The ArrayTester class provides two helper methods: containsDuplicates and hasAllValues. The method containsDuplicates returns true if the given one-dimensional array arr contains any duplicate values and false otherwise. The method hasAllValues returns true if and only if every value in arr1 appears in arr2. You do not need to write the code for these methods.
Complete method isLatin below. Assume that getColumn works as specified, regardless of what you wrote in part (a). You must use getColumn, hasAllValues, and containsDuplicates appropriately to receive full credit.