CSE190L Sample Midterm, Spring 2007 handout #13 1. Details of inheritance, 30 points. Assuming that the following classes have been defined: public class Frodo extends Bilbo { public void method1() { System.out.println("Frodo 1"); super.method1(); } public void method3() { System.out.println("Frodo 3"); } } public class Gandalf { public void method1() { System.out.println("Gandalf 1"); } public void method2() { System.out.println("Gandalf 2"); method1(); } } public class Bilbo extends Gandalf { public void method1() { System.out.println("Bilbo 1"); } } public class Gollum extends Gandalf { public void method3() { System.out.println("Gollum 3"); } } And assuming the following variables have been defined: Gandalf var1 = new Frodo(); Gandalf var2 = new Bilbo(); Gandalf var3 = new Gandalf(); Object var4 = new Bilbo(); Bilbo var5 = new Frodo(); Object var6 = new Gollum(); In the table below, indicate in the right-hand column the output produced by the statement in the left-hand column. If the statement produces more than one line of output, indicate the line breaks with slashes as in "a/b/c" to indicate three lines of output with "a" followed by "b" followed by "c". If the statement causes an error, fill in the right-hand column with either the phrase "compiler error" or "runtime error" to indicate when the error would be detected. Statement Output ------------------------------------------------------------ var1.method1(); ____________________________ var2.method1(); ____________________________ var3.method1(); ____________________________ var4.method1(); ____________________________ var5.method1(); ____________________________ var6.method1(); ____________________________ var1.method2(); ____________________________ var2.method2(); ____________________________ var3.method2(); ____________________________ var4.method2(); ____________________________ var5.method2(); ____________________________ var6.method2(); ____________________________ ((Bilbo)var1).method3(); ____________________________ ((Gandalf)var1).method2(); ____________________________ ((Frodo)var4).method1(); ____________________________ ((Gandalf)var6).method2(); ____________________________ ((Gandalf)var4).method1(); ____________________________ ((Frodo)var6).method3(); ____________________________ ((Frodo)var3).method3(); ____________________________ ((Frodo)var5).method3(); ____________________________ Problems 2 and 3 involve filling in the details of a particular GUI program. The program has the standard structure of a main class that launches a frame, a frame with a panel and a panel that draws a particular picture. The picture has a green background and includes circles of radius 10 drawn in red and lines drawn in blue. The frame includes radio buttons that allow the user to choose between drawing circles and drawing lines. In "drawing circles" mode each time the user clicks on a particular point, a red circle of radius 10 is drawn at that point (i.e., with that point as its center). In "drawing lines" mode a new line is drawn in blue for each pair of user clicks. In other words, the first time the user clicks while in this mode, nothing happens, but the second time the user clicks, a line is drawn between that pair of points. Nothing happens again on the third click while in this mode, but the fourth click again causes a blue line to be drawn between these two points, and so on. If the user leaves "drawing lines" mode and comes back to it later, this process should start fresh. For example, if the user is in "drawing lines" mode and clicks once, then switches into "drawing circles" mode, then switches back to "drawing lines" mode, the mouse click from the previous "drawing lines" mode should not be considered this second time around. 2, Drawing, 35 points. Define a panel class called ShapePanel. As mentioned above, it should have a green background and should draw various red circles and blue lines. The lines should be drawn on top of the circles. Your class must provide public methods that the frame can call to switch between "drawing circles" mode and "drawing lines" mode and to indicate that the user has clicked on a particular point in the panel. While in "drawing circles" mode each click should produce a red circle of radius 10 centered about the point where the user clicked. While in "drawing lines" mode each pair of clicks should produce a blue line drawn between the two points. Each time the user enters "drawing lines" mode the line-drawing process begins anew, as described above. Below is an example of what the panel should look after the user has drawn several circles and lines. 3. Controls, 35 points. Define a frame class called ShapeFrame that uses the ShapePanel class from problem #2. Look at the earlier example to see exactly what it should look like. You should define a frame with the given title and a width of 400 and a height of 300. It should close properly when the user clicks on the close box. As described previously, it should contain the shape panel and should include at the top a pair of radio buttons that allow the user to switch between "drawing circles" mode and "drawing lines" mode (the user should be in only one mode at any time). You should attach a mouse listener to the panel that tells it that the user has clicked on a new location.
Stuart Reges
Last modified: Mon Apr 30 10:28:55 PDT 2007