Skip to main content
Logo image

Section 5.9 Free Response Questions

Section 5.9.1 Free Response - Trio A

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 https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year
 1 
https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year
.
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.
Figure 5.9.1. UML class diagram showing MenuItem is implemented by Sandwich, Salad, Drink, and Trio.
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.
Figure 5.9.2. Example objects
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).

Subsection 5.9.1.1 Try and Solve It

Activity 5.9.1.
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.

Section 5.9.2 Trio Student Solution 1

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 https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year
 1 
https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year
.
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.
Figure 5.9.1. UML class diagram showing MenuItem is implemented by Sandwich, Salad, Drink, and Trio.
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.
Figure 5.9.2. Example objects
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).

Subsection 5.9.2.1 Grading Rubric

Below is the grading rubric for the Trio class problem.
Figure 5.9.3. The grading rubric for the Trio class problem.

Subsection 5.9.2.2 Practice Grading

The following is the first sample student response.
Figure 5.9.4. The first sample student response to the Trio class problem.
Apply the grading rubric shown above as you answer the following questions.
Apply the Grading Rubric
Activity 5.9.1.
Should the student earn 1 point for the correct declaration of the Trio class?
  • This declares the class correctly as public class Trio implements MenuItem
  • What do you think is wrong with the class declaration?
Activity 5.9.2.
Should the student earn 1 point for declaring the private instance variables (sandwich, salad, and drink or name and price)?
  • All instance variables are declared private (sand, sal, and dri) and are of the appropriate type (Sandwich, Salad, and Drink)
  • What do you think is wrong with the instance variables declaration?
Activity 5.9.3.
Should the student earn 1 point for declaring the constructor correctly?
  • This solution declares the constructor as public Trio(Sandwich a, Salad b, Drink c)
  • What do you think is wrong with the constructor declaration?
Activity 5.9.4.
Should the student earn 1 point for correctly initializing the appropriate instance variables in the constructor?
  • This solution initializes the private instance variables (sand, sal, and dri) correctly with the values from the parameters (a,b, and c).
  • What do you think is wrong with the initialization of the instance variables in the constructor?
Activity 5.9.5.
Should the student earn 1 point for correctly declaring the methods in the MenuItem interface (getName and getPrice)?
  • This solution contains correct declarations for public String getName() and public double getPrice().
  • To implement an interface the class must have a getName and getPrice method as defined by the MenuItem interface.
Activity 5.9.6.
Should the student earn 1 point for correctly constructing the string to return from getName and making it available to be returned?
  • This solution doesn’t include the "Trio" at the end of the name so it loses this point.
  • While the name is mostly correct, it is missing the word "Trio" at the end which means it loses this point.
Activity 5.9.7.
Should the student earn 1 point for returning a constructed string from getName?
  • This solution does return the constructed string.
  • Even though the string is not correct it was constructed and returned.
Activity 5.9.8.
Should the student earn 1 point for correctly calculating the price and making it available to be returned from getPrice?
  • This solution does compute the price correctly.
  • There are only 3 possibilities for which is the cheapest item and this correctly deals with the 3 cases.
Activity 5.9.9.
Should the student earn 1 point for returning the calculated price in getPrice?
  • This solution does return the calculated price.
  • What do you think is wrong with the return statement?
Activity 5.9.10.

Section 5.9.3 Trio Student Solution 2

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 https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year
 1 
https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year
.
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.
Figure 5.9.1. UML class diagram showing MenuItem is implemented by Sandwich, Salad, Drink, and Trio.
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.
Figure 5.9.2. Example objects
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).

Subsection 5.9.3.1 Grading Rubric

Below is the grading rubric for the Trio class problem.
Figure 5.9.3. The grading rubric for the Trio class problem.

Subsection 5.9.3.2 Practice Grading

The following is the second sample student response.
Figure 5.9.4. The start of the second sample student response to the Trio class problem.
Figure 5.9.5. The end of the second sample student response to the Trio class problem.
Apply the grading rubric shown above as you answer the following questions.
Apply the Grading Rubric
Activity 5.9.1.
Should the student earn 1 point for the correct declaration of the Trio class?
  • This declares the class correctly as public class Trio implements MenuItem
  • What do you think is wrong with the class declaration?
Activity 5.9.2.
Should the student earn 1 point for declaring the private instance variables (sandwich, salad, and drink or name and price)?
  • Remember that all instance variables should be declared private so that the class controls access to the variables.
  • The student did not make the instance variables private, so the student does not get this point.
Activity 5.9.3.
Should the student earn 1 point for declaring the constructor correctly?
  • This solution declares the constructor as public Trio(Sandwich s, Salad sa, Drink d)
  • What do you think is wrong with the constructor declaration?
Activity 5.9.4.
Should the student earn 1 point for correctly initializing the appropriate instance variables in the constructor?
  • This solution initializes the instance variables (sandwich, salad, and drink) correctly with the values from the parameters (s, sa, and d).
  • What do you think is wrong with the initialization of the instance variables in the constructor?
Activity 5.9.5.
Should the student earn 1 point for correctly declaring the methods in the MenuItem interface (getName and getPrice)?
  • This solution contains correct declarations for public String getName() and public double getPrice().
  • To implement an interface the class must have a getName and getPrice method as defined by the MenuItem interface.
Activity 5.9.6.
Should the student earn 1 point for correctly constructing the string to return from getName and making it available to be returned?
  • Look at what getName is supposed to return.
  • 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.
Activity 5.9.7.
Should the student earn 1 point for returning a constructed string from getName?
  • This solution does return the constructed string, even if the string is not completely correct.
  • Even though the string is not correct it was constructed and returned.
Activity 5.9.8.
Should the student earn 1 point for correctly calculating the price and making it available to be returned from getPrice?
  • What if b is equal to c but both are greater than a?
  • This does not always compute the price correctly (when b is equal to c and they are both greater than a, it should return b+c, not a+b).
Activity 5.9.9.
Should the student earn 1 point for returning the calculated price in getPrice?
  • This solution does return the calculated price, even if that price is not always correct.
  • This point is earned if the student attempted to calculate the price and returned what was calculated.
Activity 5.9.10.

Section 5.9.4 Trio Student Solution 3

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 https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year
 1 
https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year
.
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.
Figure 5.9.1. UML class diagram showing MenuItem is implemented by Sandwich, Salad, Drink, and Trio.
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.
Figure 5.9.2. Example objects
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).

Subsection 5.9.4.1 Grading Rubric

Below is the grading rubric for the Trio class problem.
Figure 5.9.3. The grading rubric for the Trio class problem.

Subsection 5.9.4.2 Practice Grading

The following is the third sample student response.
Figure 5.9.4. The start of the second sample student response to the Trio class problem.
Apply the grading rubric shown above as you answer the following questions.
Apply the Grading Rubric
Activity 5.9.1.
Should the student earn 1 point for the correct declaration of the Trio class?
  • This declares the class correctly as public class Trio implements MenuItem
  • What do you think is wrong with the class declaration?
Activity 5.9.2.
Should the student earn 1 point for declaring the private instance variables (sandwich, salad, and drink or name and price)?
  • Do you see any instance variables declared here?
  • The student did not declare any instance variables.
Activity 5.9.3.
Should the student earn 1 point for declaring the constructor correctly?
  • This solution declares the constructor as public Trio(Sandwich sandwich, Salad salad, Drink drink)
  • What do you think is wrong with the constructor declaration?
Activity 5.9.4.
Should the student earn 1 point for correctly initializing the appropriate instance variables in the constructor?
  • This solution doesn’t have any instance variables declared and doesn’t try to use the parameter values.
  • There is no attempt to set the instance variables (which haven’t been declared) to the parameter values.
Activity 5.9.5.
Should the student earn 1 point for correctly declaring the methods in the MenuItem interface (getName and getPrice)?
  • To implement an interface the class must have both a getName and getPrice method.
  • This class is missing both the interface methods.
Activity 5.9.6.
Should the student earn 1 point for correctly constructing the string to return from getName and making it available to be returned?
  • While the toString method exists and correctly creates the name string, it is not called by a getName method.
  • Since there is no getName method this point can not be awarded.
Activity 5.9.7.
Should the student earn 1 point for returning a constructed string from getName?
  • While the toString method exists and correctly creates and returns the name string, it is not called by a getName method.
  • Since there is no getName method this point can not be awarded.
Activity 5.9.8.
Should the student earn 1 point for correctly calculating the price and making it available to be returned from getPrice?
  • While there is a method that calculates the price correctly, it is the wrong method.
  • There is no getPrice method so the student can not earn this point.
Activity 5.9.9.
Should the student earn 1 point for returning the calculated price in getPrice?
  • While there is a method that calculates the price correctly and returns it, it is the wrong method.
  • There is no getPrice method so the student can not earn this point.
Activity 5.9.10.
You have attempted of activities on this page.