comp 170 / isom 370 Final Exam Study Guide Dordal Exam: Monday, May 8, 2006, 4:15 pm Here is a summary of the important sections covered. Chapter 4: ArrayLists, Loops, Iterators Chapter 5: HashMaps (general information) Chapter 7: Basics of world-of-Zuul, basics of design Chapter 8: Inheritance Chapter 9: Inheritance & Polymorphism Chapter 10: Fox & Rabbit, Interfaces Chapter 11: Basics of GUIs, how inheritance & interfaces help ==================================================================== Tentative outline for the exam: * A problem on basic inheritance, similar to #1 below * A problem on inheritance in practice * A problem involving loops & ArrayLists * Iterators * Interfaces & Abstract classes ==================================================================== Sample problems 1. Here's another variation on inheritance Suppose we have class Base { public int x; public Base(int x) { this.x = x; } public int f() {return x + g();} public int g() {return 100;} } class Deriv extends Base { public Deriv(int x) {super(x);} public int g() {return 6660;} } Suppose we have the following: Base b1 = new Base(4); Base b2 = new Deriv(5); Deriv d1 = new Deriv(6); (a). What is the output of the following? System.out.println(b1.f()); System.out.println(b2.f()); System.out.println(d1.f()); (b). What is the output of the following? System.out.println(b1.g()); System.out.println(b2.g()); System.out.println(d1.g()); ============== 2. Suppose we have a simulator that handles planes and cars. class Plane has a Position fly(Weather w, int t) method that gives the position after time increment t; class Car has method Position drive(Traffic tr, int t) (a). Can you give a common base class for Plane and Car that has a single abstract method for fly() and drive()? If so, give the base class definition; if not, explain why not. (b). Can you give an Interface that both Plane and Car, as separate classes, could implement? ============== 3.(a). Convert the following loop to a for-loop using iterators. The variable "items" is an ArrayList. Class Items has two child classes, Red and Green. for(int i = 0; i< items.size(); i++) { Item itm = (Item) items.get(i); if (itm instanceof Red) { Red r = (Red) itm; r.printRed(); } else { itm.printItem(); } } (b). Now convert it to a while loop using iterators. (c). How could inheritance be used to avoid the use of "instanceof"? ============= 4. Consider the following class: public class Foo { int x; public Foo(int xx) { x = xx;} public int f() {return x;} public int g() {return 0;} } (a). Assume the intent is for derived classes always to implement class-specific versions of g(). Why can't we leave the body of g() blank (that is, {} )? (b). Rewrite Foo as an abstract class. (c). Rewrite Foo as an interface, to the extent possible. Just leave out parts that don't translate (like the field x). ============= 5. Consider the MyShape class from lab 9. A simplified and abbreviated version of MyShape might be: public class MyShape { private int xPosition; private int yPosition; private String color; public MyShape(int x, int y) {...} public int getXPos() {return xPosition; } public int getYPos() {return yPosition; } public void moveRight() {moveHorizontal(20);} public void moveLeft () {moveHorizontal(-20);} public void moveUp() {moveVertical(-20);} public void moveDown() {moveVertical( 20);} public void moveHorizontal(int distance) {...} public void moveVertical(int distance) {...} public void draw() {} public String getColor() {return color;} public void setColor(String c) { color = c;} } (a). What changes would have to be made to make MyShape an abstract class? Why might you want to do this? (b). What changes would have to be made to make MyShape an interface? The four (private) fields would have to go away, as interfaces cannot contain data. Would anything replace these? Would any part of the interface serve as an indication that classes implementing the interface would likely have these fields? Where would these fields have to be moved to? ============= 6. In the ImageViewer project: (a). Why is class Filter an abstract class? (b). The following inheritance relationships are used (aside from Filter): ImagePanel: public class ImagePanel extends JComponent OFImage: public class OFImage extends BufferedImage Explain the purpose of these inheritance relationships, and in what ways they simplify the project. (c). Look at class Filter. Why do you think it was *not* implemented as an interface? What problem comes up if you try to convert it to an interface? ============= 7. Recall the ImageViewer project. Suppose the Undo button is implemented by setting the image field currentImage to prevImage. Also, before each filter application, and before each button application, we save currentImage via prevImage = currentImage. (a). Explain why this works for the rotate, smaller, and larger buttons, but *fails* for the filters. (b). What do you have to do to get it to work for the filters? ============= 8. Consider a simple graphics-item container class, MyContainer. It is to have an ArrayList field called "panes", of items of type Pane. Panes implement the following interface: public interface Paintable { public void paint(int x, int y); } An outline of MyContainer might be: public class MyContainer { private ArrayList panes; ... } (a). Implement a method void add(Pane p) to add new Panes to the MyContainer. (b). Implement a method Pane get(int i) in MyContainer to get the ith Pane. (c). Implement a paint() method for MyContainer. The first Pane is to be painted at coordinates (x,y)=(0,0); the second at coordinates (40,40), the third at (80,80), incrementing both x and y by 40 for each successive pane. Note that MyContainer.paint() and Pane.paint(int x, int y) are different; you assume the latter and are to write the former. +------------------+ | | | +------------------+ | | | | | +------------------+ | | | | | | | | | | | | | | | | +---| | | | | | +---| | | | +------------------+| (d). write a declaration of class Pane that implements the interface Paintable. ============= 9. In the ImageViewer class, suppose we define undoButton = new JButton("Undo"); (a). Add an ActionListener to undoButton, using either an anonymous or named inner class. (b). We added all four buttons to something called "toolbar", not to contentPane directly. Why?