Computer Science II - Juniata College



Computer Science IISpring 2013, Wednesday, 2/13/13100 pointsName ________KEY__________________Miscellaneous, basic Java concepts. Matching. Place the letter in the blank that best matches the statement.[10 pts] __D_ The source code filename of the class Test1.__B_ Where Test1 compiled bytecodes are found.__G__ The header line when Test1 is a superclass of Test1a .__F__ The automatic recovery of memory used by objects no longer accessible through any references._J___ and __M__ are Java primitive types.__P__ Java operation/method to check string equality.__K__ All classes are subclass of this.__R__ The method called upon to return a summary of an object’s contents; usually overridden.__N__ Used to compare if two objects’ address are the same.java.Test1Test1.classTest1.exeTest1.javaDefragmentationGarbage collectionpublic class Test1a extends Test1public subclass Test1a of Test1stringintobjectmainTypeboolean==.println().equals().strcmp().toString()Number the steps of the software development cycle (Waterfall method) from 1 to 6 in correct order.[5 pts]__5__ Testing__2__ Analysis__4__ Implementation__6__ Maintenance__1__ Requirements identification ___3_ DesignObject-oriented programming design principles. (True/False)[10 pts]_F__ An Abstract Data Type exposes its data structure organization and hides its operations.__T_ Reusability is a quality software goal in object-oriented class design.__F_ A void method that returns an object through a parameter can be called as part of a cascade of method calls.__F_ The this instance reference in a method can always be omitted.__T_ A method of a subclass may called its parent’s class method using a super prefix. __T_ The information hiding principle says the user of a class need not know the details of its implementation while the implementer generally does not know how the class will be used.__T_ A precondition defines what must initially hold for a method to correctly carry out its task.__T_ A class may define multiple methods with the same name as long as the parameter set is different._F__ A protected instance variable is like a private variable, but is actually inaccessible except by getters and setters._T__ A variable designated as static in a class makes it a class variable of which there is only one instance.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 fastSounds with soundClips.length elements and 2) use a for loop to initialize each element of fastSounds to be half the length of the Sound objects in the soundClips array using the scale(factor) method (it returns a Sound object). [9]Sound [] fastSounds = new Sound[soundClips.length];for (int i=0; i<soundClips.length; i++){ fastSounds[i] = soundClips[i].scale(0.5);}b. Give a Java code segment to compute and print the average length of the objects in the soundClips array using the getLength() method. [9]double total = 0;for (int i=1; i<soundClips.length; i++){ total += soundClips[i].getLength}System.out.println(“The average length is “ + (total/soundClips.length));Fill in Java code in the method makeNegative() below to modify a Picture object that generates the photographic negative of the image. This is done by subtracting each of the current RGB values from 255.[10 pts]public __void____ makeNegative() { Pixel [] pixels = _this . getPixels(); for(Pixel pix : __pixels_ ){__pix_.setRed (__255__ _-__ pix.__getRed___());__ pix _.setGreen (___255_____ _-__ pix.___getGreen____());__ pix _.setBlue (___255_____ __-_ pix.___getBlue___()); }}What would a second call to makeNegative() do on the Picture object?It would exactly restore the original image with no loss of clarityComplete the method fadeToRight() that fades the image uniformly from left to right such that on each horizontal line the leftmost pixel is unchanged, and the middle pixel is halfway to black and the rightmost pixel is black, all three values of RGB are 0. And the pixels in between are adjusted as smoothly as possible. The adjustment factor calculation based on the horizontal x value has been done for you.[15 pts]public void fadeToRight(){ Pixel temp; double factor; int width = this.___getWidth__(); for( _int_ y = 0; y < this.getHeight(); _y++__){ for( __ int ___ x = 0; x < __ width __ ; _x++__){ temp = this.getPixel(x,y); factor = 1.0 – (double) _x_ / (width-1); temp.setRed( (_int_)(temp.__ getRed __() * factor )); temp._setGreen__ ((__int __)(temp._getGreen___() * factor )); temp._setBlue_ ((__int __)(temp.__getBlue__() * factor )); } }}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; for (int i=0; i < N; i++) { myTurtles[i] = new Turtle(myWorld); }// initially centered; facing north for (int i=0; i < N; i++) { head = ((int) (360 * Math.random())); s = myTurtles[i]; s.turn(head); s.penDown(); s.forward(100); } for (int j=0; j < N; j++) { s = myTurtles[j]; p = myTurtles[(j+1)%N]; s.turnToFace(p); s.forward(50); } }}Four lines of length 100 drawn outward from a center point, then each have a 50 unit length drawn towards one of the other turtles (before or after it draws its 50 unit line)b. Are the assignments p=myTurtles[i-1] and s = myTurtles[i] using shallow copying or deep copying? ____ Explain the differences between these two types of object “copying”.[10 pts]shallow copying. A shallow copy merely tracks the address of the object. A deep copy creates an entirely new copy of the object, separate addresses and separate instance variable.Below is a code segment to put together a sound collage/mashup. Answer the questions about this code below.[12 pts]FileChooser.setMediaPath("C:/DrJava/MediaSources/"); Sound snap = new Sound(FileChooser.getMediaPath("snap-tenth.wav")); Sound drum = new Sound(FileChooser.getMediaPath("drumroll-1.wav")); Sound clink = new Sound(FileChooser.getMediaPath("clink-tenth.wav")); Sound clap = new Sound(FileChooser.getMediaPath("clap-q.wav")); Sound drumRev = drum.reverse().scale(0.5); Sound soundA = snap.append(clink).append(clink).append(clap).append(drumRev); Sound soundB = clink.append(clap).append(clap).append(drum).append(snap).append(snap); Sound collage = soundA.append(soundB).append(soundB) .append(soundA).append(soundA).append(soundB);collage.play(); From the code, what would the method header be for the reverse() method? [2]public Sound reverse(){From the code, what would the method header be for the append() method? [2]public Sound append (Sound aSnd){After the code executes, how many Sound objects are accessible through variables? [2] ___8____While the code executed, how many Sound objects were temporarily created then discarded? [2] ___12___Snap is 0.1 seconds, drumroll is 1.0 second, clink is 0.1 seconds and clap is 0.25 seconds. What’s the total time for the collage Sound object? Draw a tree to prove/derive your answer. [4]Collage =8.55|--------------------------------------------| | | | | |A=1.05x3 B B A A B=1.8x3| |---------------------------------- ----------------------------------------| | | | | | | | | | |sp=.1 ck=.1 ck cp=.25 dv=.5 ck cp cp d=1 sp sp=0.1 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download