Week 8: exam review
2013-03-03
Exam 1 (on Wednesday, March 6) will cover all the review material, plus
design and implementation of classes and interfaces. This corresponds roughly to
book chapters 1-7, plus parts of 8.
Example problems
- Add an
equals
method to the class below that
represents a fraction. The method should return true if
the two fractions represent the same number.
public class Fraction {
// Represent the number n/d.
public final int n; // numerator
public final int d; // denominator
public Fraction(int n, int d) {
this.n = n;
this.d = d;
}
}
- What does the following code print?
int x = 5;
for (int i = 0; i < 5; i++) {
x = x + i;
if (x % 2 == 0) {
System.out.println(x);
}
}
- Write a program that reads user input until the user enters
a blank line, and the prints the longest line entered.
- Consider the
Callable
inteface defined below:
public interface Callable {
public int call();
}
Implement this interface in a class called Inc
so that
the output of the following program is the numbers 1 through 10:
Callable f = new Inc();
for (int = 0; i < 10; i++) {
System.out.println(f.call());
}
-
Define a class that has a method called randomString
that takes
a non-negative integer as input and returns a String
made up of
random letters in the range a-z.
The standard library
Random
class could be helpful. Another useful fact: in Java, computing
(char)('a' + 1)
results in 'b'
.