//------------------------------------------------------------ // Question 1 // // Part A // A constructor should initialize all instance variables to a // reasonable state so that any method can be called safely. // // There are several differences between a constructor and other // methods: // - it must have the same name as the class // - it does not specify a return value // - it cannot be called directly // - it is guaranteed to be called once and only once, before // any other method is called // Part B // A class represents an object in an abstract way, specifying // the common attributes of all objects of a specific kind. An // instance represents a specific object whose attributes have // specific values. // // For example: // - a building is a class, the Duke Chapel and the LSRC are instances // - a student is a class, you are an instance //------------------------------------------------------------ // Question 2 // //Part A public boolean allDifferent (Color c) { int r = c.getRed(); int g = c.getGreen(); int b = c.getBlue(); return (r != b) && (b != g) && (g != r); } // Part B public int max3 (int a, int b, int c) { return Math.max(a, Math.max(b, c)); } //------------------------------------------------------------ // Question 3 // // Part A // There are not enough parentheses to complete the expression based // on the number of open parentheses. // Line 4 should read: // int hrSum = ((min1 + min2) + ((sec1 + sec2) / 60)) / 60; // Part B // Last one shows second do not always add correctly. // Line 2 should read: // int secSum = (sec1 + sec2) % 60; //------------------------------------------------------------ // Question 4 // // Part A public Rectangle (Point topLeft, Point bottomRight) { myTopLeft = new Point(topLeft.x, topLeft.y); return new Dimension(bottomRight.x - topLeft.x, bottomRight.y - topLeft.y); } // Part B public void translate (int xChange, int yChange) { myTopLeft.x += xChange; myTopLeft.y += yChange; // OR: // myTopLeft.translate(xChange, yChange); } // Part C 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 D public Rectangle union (Rectangle other) { Rectangle result = new Rectangle(new Point(getLeft(), getTop()), new Point(getRight(), getBottom())); result.includePoint(new Point(other.getLeft(), other.getTop())); result.includePoint(new Point(other.getRight(), other.getBottom())); return result; // OR: // 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(new Point(left, top), new Point(right, bottom)); } //------------------------------------------------------------ // Question 5 // // Part A public class Target extends Mover { public Target (Point center, Dimension size, Point velocity, Color color) { myCenter = new Point(center.x, center.y); mySize = new Dimension(size.width, size.height); myVelocity = new Point(velocity.x, velocity.y); myColor = color; } public void paint (Graphics pen) { Color color = myColor; pen.setColor(color); pen.fillOval(myCenter.x - size.width / 2, myCenter.y - size.height / 2, size.width, size.height); color = color.brighter(); pen.setColor(color); pen.fillOval(myCenter.x - size.width / 3, myCenter.y - size.height / 3, 2 * size.width / 3, 2 * size.height / 3); color = color.brighter(); pen.setColor(color); pen.fillOval(myCenter.x - size.width / 6, myCenter.y - size.height / 6, size.width / 3, size.height / 3); } public void update (Dimension bounds) { if (myCenter.x - mySize.width / 2 >= 0 && myCenter.x + mySize.width / 2 < bounds.width && myCenter.y - mySize.height / 2 >= 0 && myCenter.y + mySize.height / 2 < bounds.height) { getSize().width *= getVelocity().x; getSize().height *= getVelocity().y; } } }