Write simple java and command line to compile and run
Solution
importjava.util.*;
interface Movers {
public void move();
}
class Cat implements Movers {
public void move() {
System.out.println(“runs like a cat”);
}
}
class Fish implements Movers {
public void move() {
System.out.println(“swims like a fish”);
}
}
class Exam {
public static void main(String[] args) {
Collection<Movers> movers = new ArrayList<Movers>();
movers.add(new Fish());
movers.add(new Cat());
for (Movers mover : movers) {
mover.move();
}
}
}
classExamException extends Exception {}
// answer for part (a)
void f2() throws ExamException {
throw new ExamException();
}
// answer for part (b)
void f1() {
try {
f2();
} catch (ExamException e) {
e.printStackTrace();
}
}