CMSI 281 Fall 2009 Answers to Quiz 1 PROBLEM 1 --------- // This is not efficient, but good enough for a quiz answer. public class Person { private int id; private String name; private Date birthdate; public Person(int id, String name, Date birthdate) { validate(name != null, "Name cannot be null"); validate(!name.trim().equals(""), "Name must have at least one non-space character"); validate(birthdate != null, "Birthdate cannot be null"); this.id = id; this.name = name; // Must make defensive copy: Dates are mutable!! this.birthdate = new Date(birthdate.getTime()); } public int getId() {return id;} public String getName() {return name;} public Date getBirthdate() {return birthdate;} private static void validate(boolean condition, String message) { if (!condition) throw new IllegalArgumentException(message); } } PROBLEM 2 --------- To the Polynomial interface add: Polynomial times(double factor); In the ArrayPolynomial class add: public Polynomial times(double factor) { ArrayPolynomial p = new ArrayPolynomial(); p.coefficients = new double[coefficients.length]; for (int i = 0; i < p.coefficients.length; i++) { p.coefficients[i] = factor * coefficients[i]; } return p; } PROBLEM 3 --------- I actually miswrote this question. I meant to say that each grade had three properties: (1) the number of grade points, (2) whether the grade is used in the computation of the GPA, and (3) whether the grades gives the student credit for completing the course. Here is the answer for what I meant to say: public enum Grade { A ("A", 4.0, true, true), A_MINUS ("A-", 3.7, true, true), B_PLUS ("B-", 3.3, true, true), B ("B", 3.0, true, true), B_MINUS ("B-", 2.7, true, true), C_PLUS ("C+", 2.3, true, true), C ("C", 2.0, true, true), C_MINUS ("C-", 1.7, true, true), D ("D", 1.0, true, true), F ("F", 0.0, true, false), CR ("CR", 0.0, false, true), NC ("NC", 0.0, false, false), W ("W", 0.0, false, false), I ("I", 0.0, false, false), NR ("NR", 0.0, false, false); private String name; private double points; private boolean inGpa; private boolean credit; private Grade(String name, double points, boolean inGpa, boolean credit) { this.name = name; this.points = points; this.inGpa = inGpa; this.credit = credit; } public String getName() {return name;} public double getPoints() {return points;} public boolean isInGpa() {return inGpa;} public boolean isCredit() {return credit;} public String toString() {return name;} } PROBLEM 4 --------- Both ways of constructing a complex number require two doubles. We cannot write two overloaded constructors that take exactly the same arguments. But we can write two separate factory methods. The class we want looks like this: public class Complex { private double re; private double im; private Complex(double re, double im) { this.re = re; this.im = im; } public static Complex fromCoordinates(double re, double im) { return new Complex(re, im); } public static Complex fromPhasor(double mag, double angle) { return new Complex(mag * Math.cos(angle), mag * Math.sin(angle)); } ... } PROBLEM 5 --------- public static int[] apply(int[] a, int x, Function f) { int[] result = new int[a.length]; for (int i = 0; i < a.length; i++) { result[i] = f.evaluate(x, a[i]); } return result; } Here's another way to do it: public static int[] apply(int[] a, int x, Function f) { int[] result = new int[a.length]; int i = 0; for (int y : a) { result[i++] = f.evaluate(x, y); } return result; } PROBLEM 6 --------- There are a number of correct answers for this problem; we'll go over some of them in class. PROBLEM 7 --------- public class Circle extends Ellipse { public Circle(double radius) { super(r, r); } public double getRadius() { return getRadius1(); } @Override public String toString() { return "[Circle with radius " + getRadius() + "]"; } } PROBLEM 8 --------- This is an interface for a pretty intelligent robot: instead how telling how far to move or how many degrees to turn on a particular axis, we tell it absolute positions to move to and absolute directions for it to orient itself in, and the robot is supposed to figure out how to get there itself. public interface FlameThrower { Point getPosition(); void setPosition(Point position); Vector getDirection(); void setDirection(Vector direction); boolean isOn(); void turnOn(); void turnOff(); double getFuelFlowRate(); void setFuelFlowRate(double fuelFlowRate); double fuelRemaining(); }