
// Solutions to Test 1 Review 3 CompSci 6 Fall 2005


//------------------------------------------------------------
// Question 1
//

public double average (String values)
{
    int count = 0;
    int sum = 0;

    Scanner scanner = new Scanner(values);
    scanner.useDelimiter(",");
    while (scanner.hasNext())
    {
        sum += scanner.nextInt();
        count++;
    }

    return (double)sum / (double)count;
}





//------------------------------------------------------------
// Question 2
//

// Part A
public int countOccurrences (String str, ArrayList<String> words)
{
    int count = 0;

    for (String current : words)
    {
        if (current.equals(str))
        {
            count++;
        }
    }

    return count;
}


// Part B
public String maxOccurring (ArrayList<String> words)
{
    String result = "";
    int maxCount = 0;

    for (String current : words)
    {
        int count = countOccurrences(current, words);
        if (count > maxCount)
        {
            maxCount = count;
            result = current;
        }
    }

    return result;
}







//------------------------------------------------------------
// Question 3
//

// Part A
public void translate (int xChange, int yChange)
{
    myTopLeft.translate(xChange, yChange);
    // OR:
    // myTopLeft.x += xChange;
    // myTopLeft.y += yChange;
}

// Part B
public void includePoint (Point pt)
{
    int right = getRight();
    int bottom = getBottom();
    myTopLeft.x = Math.min(pt.x, myTopLeft.x);
    myTopLeft.y = Math.min(pt.y, myTopLeft.y);
    mySize.width = Math.max(Math.max(right, pt.x) - myTopLeft.x, mySize.width);
    mySize.height = Math.max(Math.max(bottom, pt.y) - myTopLeft.y, mySize.height);
}

// Part C
public Rectangle union (Rectangle other)
{
    int left = Math.min(getLeft(), other.getLeft());
    int top = Math.min(getTop(), other.getTop());
    int right = Math.max(getRight(), other.getRight());
    int bottom = Math.max(getBottom(), other.getBottom());

    return new Rectangle(left, top, right - left, bottom - top);
}

// Part D
public Rectangle unionAll (ArrayList<Rectangle> rects)
{
    Rectangle result = rects.get(0);

    for (int k = 1; k < rects.size(); k++)
    {
        result = result.union(rects.get(k));
    }

    return result;
}


//------------------------------------------------------------
// Question 4
//

// Part A
public class PercentOff extends Coupon
{
    private double myPercentOff;

    public PercentOff (Date expiration, String productName, 
                       double percentOff)
    {
        super(expiration, productName);
        myPercentOff = percentOff;
    }

    protected boolean isMatch (GroceryProduct item, int numBought)
    {
        // not actually needed
        return super.isMatch(item, numBought);
    }

    protected double calculateDiscount (GroceryProduct item, int numBought)
    {
        return item.getPrice() * myPercentOff * numBought;
    }
}

// Part B
public class GetOneFree extends Coupon
{
    private int myNumNeeded;

    public GetOneFree (Date expiration, String productName, int numNeeded)
    {
        super(expiration, productName);
        myNumNeeded = numNeeded;
    }

    protected boolean isMatch (GroceryProduct item, int numBought)
    {
        return super.isMatch(item, numBought) && 
               (numBought >= myNumNeeded);
    }

    protected double calculateDiscount (GroceryProduct item, int numBought)
    {
        return item.getPrice();
    }
}
