# Trio Solution

Trio, the fourth free response question on the 2014 AP Computer Science Exam, was checking that you understood how Java interfaces worked by having you implement an arbitrary interface called MenuIteminto a class called Trio.

Before we look at the solution, let’s step back and talk about interfaces, abstract classes and inheritance in general.

Over the past few years College Board has included questions on the AP CompSci exam where you had to work with interfaces or abstract classes. Implementing interfaces and extending abstract classes are something that you must understand going in to the exam if you want to score a 5. Fortunately it’s not that difficult of a concept. A little tough to get your head wrapped around, but once you’re there these typically are some of the easiest problems.

## Trio: The Problem

You’re given the following MenuIteminterface

public interface MenuItem {
String getName();
double getPrice();
}

What this tells us is that any class that implements MenuItem needs to contain both a getName and a getPrice method. That’s enough to let us know what methods the Trioclass that we’ll be implementing needs.

We’re also told that we need to create a constructor that has parameters for a Sandwich, Salad, and Drink; in that order. Each of these three classes also implement MenuItemso we know what methods they have.

And since we’re passing in values to a constructor, we also need instance variables in the class. The name of the instance variables doesn’t matter, but they should be private and match the types of data coming in the constructor.

Here’s where we are with a finished constructor and instance variables.

public class Trio implements MenuItem {
private Sandwich sand;
private Drink dri;

public Tri(Sandwich s, Salad sa, Drink d) {
sand = s;
sal = sa;
dri = d;
}
}

This sets up the class so that we can create an instance and store the parts of the order. Our code still will not build though since we haven’t implemented the required methods.

## Implementing the methods

Next step is building the two methods.

The problem states that getNameshould return the name of the sandwich, salad and drink separated by a / character and then the word “Trio”. Since all of the classes implement MenuItemwe know that they all have a getName() method, so we’ll use that.

public String getName() {
return sand.getName() + "/" + sal.getName() + "/" + dri.getName() + " Trio"
}

The key to this part is that you knew enough about polymorphism to get that all the classes had a getNamemethod.

The second part, getPricehas a little more logic because you need to figure out the sum of the two most expensive items in the Trio.

What I did was use Math.min()to figure out the lowest price and then subtract that from the sum of all three parts.

public double getPrice() {
double min = Math.min(sand.getPrice(), Math.min(sal.getPrice(), dri.getPrice()));
return sand.getPrice() + sal.getPrice() + dri.getPrice() - min;
}

I also had students create an array of prices, sort the array, and add positions 1 and 2. I thought that was a very clever solution.

## Full Trio Class

And here’s the whole thing, all in one place.

public class Trio implements MenuItem {
private Sandwich sand;
private Drink dri;

public Trio(Sandwich s, Salad sa, Drink d) {
sand = s;
sal = sa;
dri = d;
}

public String getName() {
return sand.getName() + "/" + sal.getName() + "/" + dri.getName() + " Trio";
}
public double getPrice() {
double min = Math.min(sand.getPrice(), Math.min(sal.getPrice(), dri.getPrice()));
return sand.getPrice() + sal.getPrice() + dri.getPrice() - min;
}
}

Want to stay in touch and keep up to date with the latest posts @ CompSci.rocks?