CSE190L Key to Final, Spring 2007 handout #30
1. One possible solution appears below.
public abstract class IntTutor {
private int problems;
private int correct;
private int max;
private Scanner console;
public IntTutor(int max) {
problems = 0;
correct = 0;
this.max = max;
console = new Scanner(System.in);
}
private int getRandom() {
return (int) (Math.random() * (max + 1));
}
protected abstract int compute(int num1, int num2);
protected abstract String prompt(int num1, int num2);
public void giveProblems(int num) {
for (int i = 0; i < num; i++) {
int num1 = getRandom();
int num2 = getRandom();
int answer = compute(num1, num2);
String prompt = prompt(num1, num2);
System.out.print(prompt);
int response = console.nextInt();
if (response == answer) {
System.out.println("Correct!");
correct++;
} else
System.out.println("No, the answer was " + answer);
}
problems += num;
}
public void showScore() {
System.out.println(correct + " out of " + problems);
}
}
public class AddTutor extends IntTutor {
public AddTutor() {
super(100);
}
public int compute(int num1, int num2) {
return num1 + num2;
}
public String prompt(int num1, int num2) {
return num1 + " + " + num2 + " = ";
}
}
public class GCDTutor extends IntTutor {
public GCDTutor() {
super(25);
}
private int gcd(int x, int y) {
if (y == 0)
return x;
else
return gcd(y, x % y);
}
public int compute(int num1, int num2) {
return gcd(num1, num2);
}
public String prompt(int num1, int num2) {
return "gcd(" + num1 + ", " + num2 + ") = ";
}
}
2. One possible solution appears below.
frame.setLayout(new GridLayout(1, 3, 3, 3));
frame.getContentPane().setBackground(Color.BLACK);
JPanel panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
panel1.add(up, BorderLayout.NORTH);
panel1.add(down, BorderLayout.SOUTH);
frame.add(panel1);
panel1.setBackground(Color.YELLOW);
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
JPanel panel3 = new JPanel();
panel3.add(lower);
panel2.add(panel3, BorderLayout.SOUTH);
panel3 = new JPanel();
panel3.add(upper);
panel2.add(panel3, BorderLayout.CENTER);
panel3.setBackground(Color.CYAN);
frame.add(panel2);
JPanel panel4 = new JPanel();
panel4.setLayout(new GridLayout(3, 1));
panel4.add(one);
panel4.add(two);
panel4.add(three);
frame.add(panel4);
3. One possible solution appears below.
public class Quadratic {
private double a, b, c;
public Quadratic(double a, double b, double c) {
if (a == 0)
throw new IllegalArgumentException();
this.a = a;
this.b = b;
this.c = c;
}
public Quadratic add(Quadratic other) {
return new Quadratic(a + other.a, b + other.b, c + other.c);
}
public double getA(){
return a;
}
public double getB(){
return b;
}
public double getC(){
return c;
}
public String toString() {
String result = a + "x^2";
if (b != 0)
result += " + " + b + "x";
if (c != 0)
result += " + " + c;
return result;
}
public boolean equals(Object o) {
if (!(o instanceof Quadratic))
return false;
else {
Quadratic other = (Quadratic) o;
double ratio = a / other.a;
return b == other.b * ratio && c == other.c * ratio;
}
}
public int hashCode() {
return new Double(b / a).hashCode() * 31
+ new Double(c / a).hashCode();
}
}
4. One possible solution for the iterator method and inner class appears below:
public Iterator iterator() {
return new IntSetIterator();
}
private class IntSetIterator implements Iterator {
public int index;
public int lastIndex;
public IntSetIterator() {
index = -1;
findNext();
lastIndex = -1;
}
private void findNext() {
do {
index++;
} while (index < values.length && !values[index]);
}
public boolean hasNext() {
return index < values.length;
}
public Integer next() {
if (!hasNext())
throw new NoSuchElementException();
lastIndex = index;
findNext();
return lastIndex;
}
public void remove() {
if (lastIndex == -1)
throw new IllegalStateException();
values[lastIndex] = false;
lastIndex = -1;
}
}