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


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

// Part A
   // correct - conditional logic, > and < with no =
   // correct - opposite conditional logic, must have parens and =
   // incorrect - conditional logic of second part opposite
   //             one fix: change to || 100 < number

// Part B
   // correct - conditional logic, returns opposite
   // correct - searches array, compares ints using ==
   // incorrect - does not work for month zero, 
   //             one fix: add ":" to beginning of month string and
   //             search for ":" + month + ":"
   // correct - conditional logic


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

public class JukeBox
{
    // Part A: instance variables
    private int myNumPlayed;
    private ArrayList<String> mySongs;
    private Random myGenerator;


    // Part B: methods
    public JukeBox (Scanner input)
    {
        input.useDelimiter("\n");

        myNumPlayed = 0;
        myGenerator = new Random();
        mySongs = new ArrayList<String>();
        while (input.hasNext())
        {
            mySongs.add(input.next());
        }
    }

    public int numSongsPlayed ()
    {
        return myNumPlayed;
    }

    public String play ()
    {
        myNumPlayed++;
        return mySongs.get(myGenerator.nextInt(mySongs.size()));
    }
}


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

// Part A
public static void main (String[] main)
{
    Winner c1 = new CarWinner("Amanda", "Harris", "Mercedes");
    Winner c2 = new CruiseWinner c2("Michael", "Abernathy", "Hawaii");
 
    c1.PrintLetter();
    c2.PrintLetter();
}

// Part B
public class CarWinner extends Winner
{
    private String myCar;

    public CarWinner (String firstName, String lastName, String car)
    {
        super(firstName, lastName);
        myCar = car;
    }

    public void printGreeting ()
    {
        super.printGreeting();
        System.out.println("you've won a NEW CAR!");
    }

    public void printPrize ()
    {
        super.printPrize();
        System.out.println("in a " + myCar + " down Main Street!");
    }
}

// Part C
public class CruiseWinner extends Winner
{
    private String myDestination;

    public CruiseWinner (String firstName, String lastName, String destination)
    {
        super(firstName, lastName);
        myDestination = destination;
    }

    public void printGreeting ()
    {
        super.printGreeting();
        System.out.println("you've won a 7-DAY CRUISE!");
    }

    public void printPrize ()
    {
        super.printPrize();
        System.out.println("to beautiful " + myDestination +
                           " with your sweetheart!");
    }
}

// Part D
public int generateTicket (int numDigits)
{
    int result = 0;
    Random rand = new Random();

    // special case first value (cannot be zero)
    result = rand.nextInt(9) + 1;
    for (int k = 1; k < numDigits; k++)
    {
        result = result * 10 + rand.nextInt(10);
    }

    return result;
}






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

// Part A
public class Target extends Mover
{
    private int myNumOvals;

    public Target (Point center, Dimension size, Point velocity, Color color,
                   int numOvals)
    {
        super(center, size, velocity, color);
        myNumOvals = numOvals;
    }

    public void paint (Graphics pen)
    {
        Color color = getColor();
        for (int k = myNumOvals; k > 0; k--)
        {
            // calculate size
            Dimension size = new Dimension(getSize().width * k / myNumOvals,
                                           getSize().height * k / myNumOvals);
            // draw ring
            pen.setColor(color);
            pen.fillOval(getLeft(), getTop(), size.width, size.height);
            // update color
            color = color.brighter();
        }

        // OR:
        // Dimension size = new Dimension(getSize().width, getSize().height);
        // Dimension increment = new Dimension(size.width / myNumOvals,
        //                                     size.height / myNumOvals);
        // for (int k = 0; k < myNumOvals; k++)
        // {
        //     // calculate size
        //     size.width -= increment.width;
        //     size.height -= increment.height;
        //
        //     // draw ring
        //     pen.setColor(color);
        //     pen.fillOval(getLeft(), getTop(), size.width, size.height);
        //     // update color
        //     color = color.brighter();
        // }
    }

    public void move (Dimension bounds)
    {
        // imlementation not shown
    }
}

// Part B
public class Grower extends Mover
{
    public Grower (Point center, Dimension size, Point velocity, Color color)
    {
        super(center, size, velocity, color);
    }

    public void paint (Graphics pen)
    {
        // imlementation not shown
    }

    public void move (Dimension bounds)
    {
        if (getSize().width < bounds.width && getSize().height < bounds.height)
        {
            getSize().width *= getVelocity().x;
            getSize().height *= getVelocity().y;
        }
    }
}
