Trio free response problem from the 2014 AP Computer Science A Exam.
Trio is #4 from the from the 2014 AP Computer Science A Free Response problems.
http://media.collegeboard.com/digitalServices/pdf/ap/ap14_frq_computer_science_a.pdf
Trio class
public class Trio implements MenuItem { private Sandwich sandwich; private Salad salad; private Drink drink; public Trio(Sandwich sand, Salad sal, Drink dr) { sandwich = sand; salad = sal; drink = dr; } public String getName() { return sandwich.getName() + "/" + salad.getName() + "/" + drink.getName() + " Trio"; } public double getPrice() { MenuItem cheapest = sandwich; if(salad.getPrice() < cheapest.getPrice()) cheapest = salad; if(drink.getPrice() < cheapest.getPrice()) cheapest = drink; return sandwich.getPrice() + salad.getPrice() + drink.getPrice() - cheapest.getPrice(); } }
Hi Mr. Horn,
Instead of creating private instance variables for each of the item types, I simply created “private double price” and “private String name” and generated the names and prices in the constructor; therefore all I had in the method bodies were return statements. Is this okay?
Yes.
In the getPrice method, I didn’t use a long series of if statements like you did. Instead I wrote if(drink.getPrice == 1.25) return 2.75; else return 3.5; Does that work and receive full credit?
No. Those numbers were just an example. The code for the class needs to work for any prices.
for getName(), I created sepreate Strings (s1,s2,s3)to get the names for the food and then I did return s1 + “/” + s2 + “/” + s3 + ” Trio”;
That works.