
// Solutions to Midterm Test 1 CompSci 6 Fall 2005


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

// Part A

// Methods and variables that are declared private can only be 
// accessed by other methods within the class.  Instance variables
// should be declared private so that other classes cannot change
// them to invalid values.  Methods may be made private if they
// will only ever be used within the class.
//
// Methods and variables that are declared protected can be accessed
// by other methods within the class or its subclasses.  Instance
// variables should be declared protected so that only subclasses can
// change them.  Methods may be made protected if they will only ever 
// be used within the class or its subclasses.
//
// Methods and variables that are declared public can be accessed
// by any other methods within the program.  Instance variables 
// should not be declared public because it increases the dependencies
// between classes.  Methods should be declared public since they are
// the primary means of using a class.

// Part B

// An instance variable is declared within a class and is available
// to all methods within the class.  It is used to hold the class'
// state.
//
// A parameter is declared in a method header (between parentheses)
// and is available only within that method.  It is used to pass
// information from one object to another when a method is called.
//
// A local variable is declared within a method body and is available
// only within that method.  It is used to temporarily hold a value
// that will be used again before the end of the method.


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

// option 1 - correct
// option 2 - correct
// option 3 - incorrect
//  does not work for the year 2000


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

// Part A

// The variable lastDigit is never updated within in the loop,
// so the currentDigit is always being compared to the least
// significant digit.  In other words, the function is actually
// answering the question: is the least significant digit the
// one with the largest value.
//
// To fix the logic error, add the following line to the code
// as the second to the last line in the loop:
//    lastDigit = number % 10;







// Part B

public int countAscending (Scanner input)
{
    int count = 0;

    while (input.hasNext())
    {
        if (isAscending(input.nextInt()))
        {
            count++;
        }
    }

    return count;
}


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

public class MarkClosest extends Mover
{
    public void move (Dimension bounds)
    {
        int minDistance = Integer.MAX_VALUE;

        for (Shape current : myMovers)
        {
            int distance = getCenter().distance(current.getCenter());
            if (current != this && distance < minDistance)
            {
                minDistance = distance;
                myClosest = current;
            }
        }
    }
}


//------------------------------------------------------------
// Question 5
//

// Part A

// An abstract class is one that cannot be instantiated, usually
// because it has an abstract method, a method that does not have 
// an implementation.  Instantiating such a class might cause such
// a method to be called.
//
// A constructor should initialize all instance variables to a
// reasonable state so that any method can be called safely. As
// parameters, width and height do not exist beyond the call to
// the constructor.  By storing those values in instance variables,
// those values are remembered as long as the class exists, 
// allowing other methods within the class access to them.
//
// The methods getWidth() and getHeight() provide public access
// to the private values that represent the width and height of
// the shape.  Because the actual instance variables are private,
// the shape's dimensions may not be changed once it has been
// created, making it immutable.

// Part B

// The syntax error is that myHeight is a private instance variable 
// of the superclass, so is not valid for the subclass to use it in
// its area method.
// 
// The logic error is that myWidth is declared in the sublcass, 
// shadowing the superclass definition and making it legal to use
// within the subclass; but since it is never set in the constructor 
// its value when area is called is 0.

// Part C

public class Rectangle extends Shape
{
    public Rectangle (double width, double height)
    {
        super(width, height);
    }

    public double area ()
    {
        return getWidth() * getHeight();
    }
}

// Part D

public ArrayList<Shape> readShapes (Scanner input)
{
    final String CIRCLE = "circle";
    final String RECT = "rect";
    ArrayList<Shape> result = new ArrayList<Shape>();

    while (input.hasNext())
    {
        String type = input.next();
        if (type.equals(CIRCLE))
        {
            result.add(new Circle(input.nextInt()));
        }
        else if (type.equals(RECT))
        {
            result.add(new Rectangle(input.nextInt(), input.nextInt()));
        }
    }

    return result;
}
