comp 170 study guide 2 Dordal Exam: Wed, Nov 14, 2007 Here is a summary of the important sections covered. You will need to be familiar with loops (while and for) and ArrayLists from chapters 4 and 5. Chapter 5 also introduced HashMaps; while these may appear on the exam, they would come with a brief summary of the important methods. Sets, also in chapter 5, will not be on the exam. Testing methods described in chapter 6 won't be on the exam, though you might be asked how you could informally test a certain class. In chapter 7, you should be familiar with the world-of-Zuul system, and the design strategies discussed there: * avoid duplicated code * prefer "loose" coupling of classes * avoid "implicit" coupling * responsibility-driven design * localizing change And finally: * cohesion: what it means, class cohesion v method cohesion, for readability v for reuse Chapter 7 questions tend to be kind of general: "how might this class design be improved" or "which design is better". Chapters 8 and 9 are on INHERITANCE and POLYMORPHISM: * inheritance to avoid code duplication (DoME example) * inheritance hierarchies * inheritance and constructors; use of super() * inheritance & assignment: base_class_var = subclass_var * polymorphic collections (eg collections of DoME Items) * Wrapper class Integer and the idea of "autoboxing" and ArrayList --- * polymorphic methods: must be defined in base class AND parent class1. base-class method is the one that's run "searching the chain" for a method * subclass method can simply inherit parent version override parent version, replacing it override parent version, calling parent version with modification (conditionally, with params, doing something before/after) use of super.methodname() * methods inherited from Object: toString() and equals() * accessing parent-class fields * what kinds of problems can be solved using inheritance avoiding certain if-elseif tests of object's type allows containers to contain related objects (a Picture might be an ArrayList of (different) Shapes) allows later programs to "hook into" features being set up now (Object.toString, as used by println()) Good study exercises (short ones): 9.12-9.17, on p 277 (p 265 in 2nd ed) ==================================================================== Tentative outline for the exam: * A problem on basic inheritance, similar to #1 below * A problem involving design issues as in Chapter 7 * A problem involving loops & ArrayLists * A problem involving design or implementation in the Zuul world * One more problem ==================================================================== Sample problems 1. (This is *the* classic exercise in inheritance) Suppose we have class Base { public int x; public Base(int x) { this.x = x; } public int f() {return x;} public int g() {return x*x;} } class Deriv extends Base { private int y; public Deriv(int x, int y) {super(x); this.y = y;} public int f() {return x + y;} public int g() {return 3*super.g()} } (a). Suppose we have the following: Base b1 = new Base(4); Base b2 = new Deriv(4,5); Deriv d1 = new Deriv(4,6); What is the output of the following? System.out.println(b1.f()); System.out.println(b2.f()); System.out.println(d1.f()); (b). In class Deriv, the method f returns x+y. This is legal because x is public in Base. Most likely, however, x would be private in Base. Assuming it was, how would you change Deriv so that it still compiled, and f() still worked the same way? (c). What is the output of the following? System.out.println(b1.g()); System.out.println(b2.g()); System.out.println(d1.g()); ============== 2. Each of the following situations involves a design problem. Discuss the problem, and suggest a better design. (a). Class CD with methods getTitle, getArtist, getComment, print Class Video with methods getTitle, getDirector, getComment, print (b). InitSimulator(int n): create simulator and run n generations of simulation HINT: this method does two different things; could they be separated? (c). A class with public data fields ============== 3. You have a HashSet strset. You want to convert to an ArrayList format. To this end, you create an empty ArrayList ArrayList strlist = new ArrayList(); (a). Give a for-each loop that copies each element of strset to strlist. (b). Upon reading the javadocs files, you discover two things: (i). HashSet derives from superclass Collection (ii).ArrayList has a method addAll(Collection). Use these facts to copy strset to strlist in one statement. ============= 4. Suppose you want to implement in the Zuul game a StoneRoom, which behaves as follows: You have entered a room with a huge stone in the center. It looks like it might be covering something. MOVE STONE You are too weak and hungry to move the stone. EAT COOKIE Wow! That made you feel stronger! MOVE STONE Filled with a burst of strength, you move the stone. A moneybag is revealed. Describe changes that need to be made to getShortDescription, getLongDescription, and respond. For the last, describe which new command words the respond method will handle, and when super.respond() is invoked. 5. Do the same as 4, but for the DragonRoom: GO NORTH The passage is littered with bones. A dragon's roar splits the air. I hope you have a weapon! << go back and GET SWORD...>> GO NORTH You have entered the lair of the dragon. His tail flicks behind you and the exit door latches closed. GO SOUTH the dragon won't let you out ATTACK DRAGON The dragon is injured! ATTACK DRAGON The dragon dies! GO SOUTH The passage is littered with bones. GO NORTH You have entered the lair of the dragon. His dead body lies before you. Exits: south north GO NORTH You have entered the dragon's treasury 6. Suppose that, in my version of the game presented in class, when you return Pepe the snake the grateful lab director tells you "from now on, you will be able to carry twice as much as before", referring to your weight limit for carrying Items in the game. The weight limit would be part of the Game class. Describe the problem we would have implementing this in the ZooRoom class, and discuss one possible solution. 7. Suppose we have the following class Rectangle. Derive from it a class Square, and include the implementation of the constructor. class Rectangle { private int xPos, yPos; private int height, width; public Rectangle(int x, int y, int h, int w) { xPos = x; yPos = y; height = h; width = w; } public void draw() {... not shown ...} public int getX() { return xPos;} // getY() similar public int getWidth() {return width;} // getHeight similar } (b). Suppose someone now adds a doubleWidth method to Rectangle: public void doubleWidth() { width = 2*width; } What problem does this present for your class Square? Can you think of a way to avoid this? 8. Consider classes representing the following. For each group, show likely inheritance relationships. Arrows representing inheritance are drawn starting at the derived class and pointing to the base class: (a). Boat, Airplane, Vehicle, Truck, Car, LandVehicle (b). Student, Undergrad, GradStudent, Applicant, Person (c) Bird, FlyingBird, Penguin, Hawk, Sparrow