Computer Science 112a Object Oriented Programming and Data Structures Ileana Streinu Fall 1997 Midterm examination October 9, 1995 Name: 112a-ax account: The exam is open book: you may consult your notes, books, even computer programs that you have written before. However you may not consult with your colleagues. All the work should be yours. The exam has two parts. Each should take you roughly one hour. Part 1 is to be done in class without the use of a computer (10:30-11:50). Part 2 involves writing and running Java applets. You will do this in in the Seelye 212 lab in the afternoon, 1:00 to 2:50. You will logon to hermite and type in, compile and debug three short Java applets. Name them according to the instructions and submit by turning the permissions on at 2:50pm. Do first the problems that you consider to be the easiest, then allow time for the most difficult ones. Show all your work: you may get partial credit. Add extra sheets if necessary. Part 1. (50 points) 1. (6 points) The following are parts of a Java program: final int circle = 1; final int square = 2; final int rectangle = 3; final int line = 4; ..... if (shape = 1) g.drawOval(10, 10, 5, 5); else if (shape = 2) g.drawRect(10, 10, 10, 10); else if (shape = 3) g. drawRect(10, 10, 20, 30); else g.drawLine(10, 10, 20, 20); Replace the if... else if... sequence with a switch statement, so that the program will still be doing the same thing. 2. (12 points) You are given the following Java applet (some details are missing, but you should be able to infer the global structure): import java.awt.*; import java.applet.*; public class myGizmo extends Applet{ int shape; Color myColor; Color yourColor; public void init() { myColor = Color.red; System.out.println(ÒInitializing...Ó); } ... void colorIt(Color myColor) { ... yourColor = myColor; System.out.println(ÒColoring....Ó); ... } void eraseColor() { ... Color myColor = yourColor; System.out.println(ÒErasing...Ó); ... } } (6 points) For each occurrence of the myColor variable, write next to it whether that is referring to: 1. an instance variable ; 2. a parameter to some method; 3. a local variable (and explain: local to what method?) (3 points) When is the init method executed? (One sentence suffices). (3 points) Where are printed the values in the System.out.println(...) statements? (Two words suffice!). 3.(6 points) Write down the global structure of a Java applet (instance variables and methods that have to be overridden) which contains a button. When the user clicks on the button, some graphics is drawn. You do not need to be too precise about syntax - I only want to see the global structure. Make sure you include all the methods needed to implement this sort of functionality, and no more than this. 4. (6 points) Extending the class hierarchy. The following example of a class definition in Java is taken directly from your textbook: you are familiar with it! class Motorcycle { String make; String color; boolean engineState; void startEngine() { if (engineState == true) System.out.println("The engine is already on."); else { engineState = true; System.out.println("The engine is now on."); } } void showAtts() { System.out.println("This motorcycle is a " + color + " " + make); if (engineState == true) System.out.println("The engine is on."); else System.out.println("The engine is off."); } public static void main (String args[]) { Motorcycle m = new Motorcycle(); m.make = "Yamaha RZ350"; m.color = "yellow"; System.out.println("Calling showAtts..."); m.showAtts(); System.out.println("--------"); System.out.println("Starting engine..."); m.startEngine(); System.out.println("--------"); System.out.println("Calling showAtts..."); m.showAtts(); System.out.println("--------"); System.out.println("Starting engine..."); m.startEngine(); } } Your task now is to define a subclass of the Motorcycle class (letÕs call it Harley) which: a. inherits from Motorcycle the instance variables. b. overrides the startEngine method with another one, which, in addition to what this one is already doing, prints a message such as ÒBRRRRRÓ indicating noise when the engine is started. 3. has a new method showOff, which prints a message ÒLook at me!Ó if the color of the motorcycle is ÒredÓ. You do not have to worry about details such as the public/private modifiers or syntax. I just want to see the structure of the new program. Write it in the space below. Do not include the main method, just the new class Harley. 5. (10 points) Write a short Java application where everything happens in the main method. There, do the following things: a. define a variable of type array of integers, named myIntegers. b. define a variable of the same type named mySquares. b. allocate space for them, in the amount of 20 units. c. use a for loop to initialize myIntegers to the first 20 integers from 1 to 20 (careful, there is a slight subtlety here!). d. now compute in mySquares the squares of these numbers (i.e. 1, 4, 9, 16, etc...). Use ultiplication x*x to get the square of a number. e. print (using System.out.println) the square values one after another. 6.(6 points) With reference to the previous problem. Expand now your program with a method that will copy one array into another one. This method should be called copyArray, should take two parameters, sourceArray and destArray, and should just copy - element by element - the contents of sourceArray into destArray. In the main method, use this method to copy the contents of the array myArray into a new one (which you will have to create!) called newArray. I am interested in the global structure, so do not worry about details such as how parameters are passed, for instance. 7. (6 points) What is the following Java applet doing? (A short sentence suffices!) import java.awt.*; import java.applet.*; public class whatDoing extends Applet{ public void init(){ setLayout(new GridLayout(3,2); add(new Button(ÒOneÓ)); add(new Button(ÒTwoÓ)); add(new Button(ÒThreeÓ)); add(new Button(ÒFourÓ)); add(new Button(ÒFiveÓ)); add(new Button(ÒSixÓ)); } } What would happen if you dropped the first statement setLayout(...)? (One sentence suffices.) Computer Science 112a Object Oriented Programming and Data Structures Ileana Streinu Fall 1997 Midterm examination October 9, 1995 Name: 112a-ax account: The exam is open book: you may consult your notes, books, even computer programs that you have written before. However you may not consult with your colleagues. All the work should be yours. Part 2. (50 points) Create a directory Midterm1 in your public_html directory. Make sure permissions are set up correctly. Create three subdirectories Pb1, Pb2 and Pb3, corresponding to the three problems I ask you to solve. At 2:50, turn on the permissions for the java source files: that is the method of submission. Do NOT touch these files after the exam period is over, or else they will not count. Pb1. (15 points) Work in a directory Pb1. Write a Java applet with two buttons on it, b1 and b2. When you click on b1, the text ÒHello!Ó is drawn on the screen. When you click on b2, the text ÒHello!Ó is printed on the Java console. Name your applet TwoButtons.java. Create an html file for it. To get full credit, the applet should work, but if you spend more than 30 minutes on this problem without making it work, I suggest that you include some comments in the java file about what its status is (compiles or not, and what the bug seems to be) and move on to the next problem. Pb2. (15 points) Work in a directory Pb2. Write a Java applet with two buttons on it, for opening and closing a window. The window should have a white background and two buttons on it, labeled 0 and 1. When you click on the buttons in the window, the appropriate character (0 or 1) is drawn on the window screen (but not one on top of the other: rather, one left and one right of the screen). Same comments as above: do what you can do in 40 minutes or so, then move on to the next problem. Pb3. (20 points) Work in directory Pb3. Copy into this directory the java file myGizmo.java which I have shown in class on Tuesday, from the address: http://cs.smith.edu/~112a/Lab5/V6/V6.3/myGizmo.java Perform the following modifications on it: a. Allow at most 25 graphical objects to be created. b. Include a new background color, yellow. Make sure you make all the relevant changes (i.e., in the Background menu and in the method(s) dealing with it). Wrap Up: turn on the permissions at 2:50 sharp, then leave. Have a nice October break! See you in a week. Ileana.