Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS 240 Exam 1 Page 1 Computer Science II Spring 2012, Wednesday, 2/8/12 100 points Name _____________________________ 2/8/12 1. Miscellaneous, basic Java concepts. Fill in the blanks. [10 pts] The source code of the Java class Test1 should be found in a file called ________________________ and its compiled bytecodes are found in _______________________________. A subclass Test1a of Test1 would have a header line public class Test1a ____________ Test1 { All top level user defined Java classes inherit from the class called ___________. The automatic recovery of memory used by objects no longer accessible through any references is called ____________________________ Give two other Java primitive data types other than int: __________________ and ____________________. The method used to lexicographically compare Strings (i.e., determine order) is ___________________. The method inherited from that top level class that should be overridden to provide some status/value presentation for printing, usually, is called _________________. 2. Number the steps of the software development cycle (Waterfall method) from 1 to 6 in correct order. [5 pts] _____ Implementation _____ Design _____ Maintenance _____ Requirements identification _____ Testing _____ Analysis 3. Object-oriented programming design principles. (True/False) [10 pts] a. ____ An Abstract Data Type hides its data structure organization and exposes its operations. b. ____ Reusability is a quality software goal in object-oriented class design. c. ____ A method that returns an object can be called as part of a cascade of method calls. d. ____ The this instance reference in a method can always be omitted. e. ____ A constructor in a subclass must call its superclass constructor using super. f. ____ The information hiding principle prevents the user of a class from knowing the details of its implementation and the implementer generally does not know how the class is used. g. ____ A precondition defines what must initially hold for a method to correctly carry out its task. h. ____ A class may only define one method for each unique method name. i. ____ A protected instance variable is like a private variable, but accessible by the subclass. j. ____ A variable designated as static in a class makes it a class variable of which there is only one instance. CS 240 Exam 1 2/8/12 Page 2 4. Arrays. For this question just give segments of Java code; not entire methods or classes. [18 pts] a. Assume there exists an array of Sound objects called soundClips[ ]. Give a Java code segment to 1) create a second array of Sound objects called reversedSounds with soundClips.length elements and 2) use a for loop to initialize each element of reversedSounds to the reversed sound clips in the soundClips array using the reverse() method (it returns a Sound object). [9] b. Give a Java code segment to print the position/index of the longest soundClips object using the getLength() method. [9] 5. Fill in Java code in the method makeDarker() below to modify a Picture object that darkens the original. This is done by halving the red, green, blue levels. [15 pts] public _____________ makeDarker() { Pixel [] pixels = _______ . getPixels(); for(Pixel pix : _________ ){ ____.setRed ( pix.___________() __ ____.setGreen ( pix.___________() __ ____.setBlue ( pix.___________() __ ________ ); ________); ________); } } Would a call to makeLighter() that doubles the levels effectively restore the Picture object? CS 240 Exam 1 2/8/12 Page 3 6. Complete the method mirror() that flips the image as a mirror image. The pixels on a horizontal line are swapped. [10 pts] public void mirror(){ Pixel leftPix; //need to hold pixels to swap Pixel rightPix; Pixel temp; int width = this.___________(); for( _____ y = 0; y<this.getHeight(); y++){ //swap left half with right half for( ________ x = 0; x<this. ___________ / 2 ; x++){ leftPix = this.getPixel(x,y); rightPix = this.getPixel (width _____________ , y); //swap their colors temp = _____________________; rightPix. _____________ ( __________________. _____________()); leftPix.setColor(rightPix.getColor()); } } } 7. Below is a main program to draw something random with turtles. a. Draw a possible scenario from executing this code to the right. This is testing your code reading. [10 pts] public class SomethingByTurtles { private static final int N = 4; public static void main(String[] args){ World myWorld = new World(); Turtle [] myTurtles = new Turtle[N]; Turtle p,s; //temporary references int head = 0; for (int i=0; i < N; i++) { myTurtles[i] = new Turtle(myWorld); } for (int i=0; i < N; i++) { head += ((int) (80 * Math.random())+10); s = myTurtles[i]; s.turn(head); s.penDown(); s.forward(100); } for (int j=0; j < N-1; j++) { s = myTurtles[j]; p = myTurtles[j+1]; s.turnToFace(p); int dist =(int)(s.getDistance(p.getXPos(),p.getYPos())); s.forward(dist); s.forward(-dist); } } } CS 240 Exam 1 2/8/12 Page 4 b. The assignments s = myTurtles[j] and p = myTurtles[j+1] are using shallow copying? Why is this the preferred mode? What would be the effect of the program if deep copying were being used, if any? [10 pts] 8. Below is the SlowWalkingTurtle class. Answer the questions about this code listed below. [12 pts] class SlowWalkingTurtle extends Turtle { public SlowWalkingTurtle (ModelDisplay modelDisplay) { super(modelDisplay); } public void forward(int pixels) { super.forward(pixels); try { Thread.sleep(500); // Wait a half sec } catch (Exception e) { System.err.println(“Exception: “ + e.getMessage()); System.err.println(“Stack Trace is:”); e.printStackTrace(); } } } a. What is the constructor doing? b. Explain the super.forward(pixels) statement. c. Why is the Thread.sleep(500) in a try-catch block? d. What are the statements of the catch block imitating?