Trio free response answer 6

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();
    }
}

6 thoughts on “Trio free response answer

  1. Reply Allan CHen May 9,2014 11:32 am

    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?

  2. Reply Alex Janssen May 10,2014 9:45 pm

    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?

    • Reply Brandon Horn May 11,2014 9:59 am

      No. Those numbers were just an example. The code for the class needs to work for any prices.

  3. Reply syed May 11,2014 9:54 am

    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”;

Leave a Reply