Midterm Exam 2 Review-Spring 2007
This is the first draft of the exam review. It is mostly complete however a few extra questions will be added in the next few days
Below are questions like those I plan to ask on Midterm REMOVED (Mar. 9). The exam will consist of 4 or 5 questions like these.
Please do try these questions! Post your answers, questions, comments, concerns, and criticisms on the pages for each question. Those comments, questions, etc. can also be about each others' answers! If someone posts an answer that you don't understand, ask about it! If you see a question here that you know the answer to, don't keep it to yourself – help your fellow students!
I will be reading your answers. Here are the ground rules for the interaction.
- If you don't post, neither will I. I will not be posting official solutions to these problems at all! If one of you gets it right, terrific!
- I will try to always point out if an answer is wrong. I won't always point out when the answer is right.
- I am glad to work with you toward the right answer. I will give hints, and I'm glad to respond to partial guesses.
Unscramble the code
Below are some code segments. The lines of code are scrambled, and some of the curly braces are removed. Put them back in the right order.
a. From SoundBranch.java
(This comment is right - it's meant to give you a headstart.)
/**
*Collect all the sound from our children FIRST,* then collect from my getNext().
**/
else
return childSound;
childSound = new Sound(1); /*A default nearly-empty sound*/
node=(CollectableNode) this.getNext();
CollectableNode node;
if (children != null)
Sound childSound;
if (this.getNext() != null)
childSound=childSound.append(node.collect());
childSound = children.collect();
public Sound collect(){
b.From Picture.java (a complete method)
for (int x=0; x < getWidth(); x++)
topPixel = getPixel(x,(mirrorPoint - y));
public void mirrorHorizontalBottomToTop()
bottomPixel = getPixel(x,(mirrorPoint + y));
for (int y=1; y < mirrorPoint; y++)
topPixel.setColor(bottomPixel.getColor());
int mirrorPoint = (int) (getHeight() / 2);
Pixel topPixel = null;
Pixel bottomPixel = null;
New
c. From SongPhrase.java
Phrase myPhrase = new Phrase();
static public Phrase riff2()
return myPhrase;
double[] phrasedata = {JMC.D4,JMC.EN,JMC.C4,JMC.EN,JMC.E4,JMC.EN,JMC.G4,JMC.EN};
myPhrase.addNoteList(phrasedata);
New
d. From Sound.java
/**
* Method to reverse a sound.
**/
Sound target = new Sound(getLength());
sampleValue = this.getSampleValueAt(srcIndex);
int sampleValue;
public Sound reverse()
target.setSampleValueAt(trgIndex,sampleValue);
return target;
for (int srcIndex=0,trgIndex=getLength()-1; srcIndex < getLength();srcIndex++,trgIndex--)
Questions, comments, answers at Midterm exam 2 review Spring2007: Unscramble the Code
Write insertAfterNext
Write the method insertAfterNext which takes an input node and inserts it after the current node’s next. If A’s getNext is B (A->B) and B’s getNext is D (A-B-D), and we call A.insertAfterNext(C), then the result should be A-B-C-D.
NOTE: Be sure to consider the possibility that this.getNext() is null! If this’s getNext is null, just insert the input after this. So, if A’s getNext is null, and we call A.insertAfterNext(C), we should get A-C.
You may found the the insertAfter code from the class LLNode:
/***Insert the input node after this node.*@param node element to insert after this.
**/
public void insertAfter(LLNode node){
// Save what "this" currently points at
LLNode oldNext = this.getNext();
this.setNext(node);
node.setNext(oldNext);
}
Questions, comments, answers atMidterm exam 2 review Spring2007: Write insertAfter
Draw the Object Structures
Draw the object structures (e.g., boxes and arrows) for the data structures resulting from these methods.
(Note: You can use any notation we used in class to draw the data structure for B.)
A. From SoundListTest.java
public void setUp4(){
FileChooser.setMediaPath("/Users/guzdial/cs1316/MediaSources/");
Sound s = null; // For copying in sounds
s = new Sound(FileChooser.getMediaPath("is.wav"));
root = new SoundElement(s);
s = new Sound(FileChooser.getMediaPath("snap-tenth.wav"));
SoundElement one = new SoundElement(s);
root.repeatNext(one,4);
s = new Sound(FileChooser.getMediaPath("corkpop-tenth.wav"));
SoundElement two = new SoundElement(s);
root.insertAfter(two);
s = new Sound(FileChooser.getMediaPath("clap-q.wav"));
SoundElement three = new SoundElement(s);
two.last().repeatNext(three,3);
root.playFromMeOn();
}
B. From SoundTreeExample.java
public void setUp3() {
FileChooser.setMediaPath("/Users/guzdial/cs1316/MediaSources/");
Sound clap = new Sound(FileChooser.getMediaPath("clap-q.wav"));
Sound chirp = new Sound(FileChooser.getMediaPath("chirp-2.wav"));
Sound rest = new Sound(FileChooser.getMediaPath("rest-1.wav"));
Sound snap = new Sound(FileChooser.getMediaPath("snap-tenth.wav"));
Sound clink = new Sound(FileChooser.getMediaPath("clink-tenth.wav"));
Sound clave = new Sound(FileChooser.getMediaPath("clave-twentieth.wav"));
Sound gong = new Sound(FileChooser.getMediaPath("gongb-2.wav"));
Sound guzdial = new Sound(FileChooser.getMediaPath("guzdial.wav"));
Sound is = new Sound(FileChooser.getMediaPath("is.wav"));
root = new SoundBranch();
SoundNode sn;
SoundBranch branch1 = new SoundBranch();
sn = new SoundNode(guzdial.append(is).append(snap));
branch1.addChild(sn);
sn = new SoundNode(clink.append(snap).append(clave));
branch1.addChild(sn);
sn = new SoundNode(guzdial.append(is).append(is));
branch1.addChild(sn);
root.addChild(branch1);
scaledBranch = new ScaleBranch(2.0);
sn = new SoundNode(clink.append(clave).append(gong));
scaledBranch.addChild(sn);
sn = new SoundNode(rest.append(chirp).append(clap));
scaledBranch.addChild(sn);
root.addChild(scaledBranch);
SoundBranch branch2 = new SoundBranch();
sn = new SoundNode(clap.append(is).append(snap));
branch2.addChild(sn);
sn = new SoundNode(clink.append(snap).append(clave));
branch2.addChild(sn);
sn = new SoundNode(chirp.append(is).append(is));
branch2.addChild(sn);
scaledBranch = new ScaleBranch(0.5);
sn = new SoundNode(guzdial.append(is).append(gong));
scaledBranch.addChild(sn);
scaledBranch.addChild(branch2);
root.addChild(scaledBranch);
SoundBranch branch3 = new SoundBranch();
sn = new SoundNode(clap.append(clap).append(clave));
branch3.addChild(sn);
sn = new SoundNode(snap.append(snap).append(clave));
branch3.addChild(sn);
sn = new SoundNode(snap.append(snap).append(clave));
branch3.addChild(sn);
root.addChild(branch3);
root.playFromMeOn();
}
Questions, comments, answers at Midterm exam 2 review Spring2007: Draw the Object Structures
What's My Data Structure?
We've talked about several data structures in this class: Arrays, trees, linked lists. Match the data structure to the statement below.
A. "Right, so it takes a while to insert or delete in my middle, but you can't possibly get to a specific piece of me faster!"
B. "Insert and delete into the middle are really fast with me, but I don't DO complexity – no hierarchy for me!"
C. "Organization chart? Sentence diagrams? Verses and chorus in a song? Structure of a scene? I'm your structure!"
Questions, comments, answers atMidterm exam 2 review Spring2007: What's My Data Structure?
Make a Scene
Below is some code that uses PositionedSceneElements to make a scene of a tree, a dog, another tree, and another dog.
public class PSETest {
public static void main(String [] args) {
FileChooser.setMediaPath("D:/cs1316/mediasources/");
Picture dog = new Picture(FileChooser.getMediaPath("dog-blue.jpg"));
PositionedSceneElement dognode = new PositionedSceneElement(dog);
PositionedSceneElement dognode2 = new PositionedSceneElement(dog);
Picture tree = new Picture(FileChooser.getMediaPath("tree-blue.jpg"));
PositionedSceneElement treenode = new PositionedSceneElement(tree);
PositionedSceneElement treenode2 = new PositionedSceneElement(tree);
treenode.setNext(dognode); dognode.setNext(treenode2);
treenode2.setNext(dognode2);
Picture bg = new Picture(400,400);
treenode.drawFromMeOn(bg);
bg.show();
}
}
Write another version that has a dog scaled to 0.25 of its original size, then three houses ("house-blue.jpg"), then three trees, all composed onto the "jungle.jpg" background.
Questions, comments, answers at Midterm exam 2 review Spring2007: Make a Scene
Understanding Inheritance New
Consider the following LLNode and DrawableNode classes:
- LLNode is an abstract class that implements all of the basic functionality of a linked list.
- DrawableNode is a class that knows how to draw itself and also has linked list functionality.
abstract public class LLNode {
/*The next branch/node/whatever to process*/
public LLNode next;
/*Constructor for LLNode just sets next to null*/
public LLNode() {
next = null;
}
/**
*Methods to set and get next elements* @param nextOne next element in list
*/
public void setNext(LLNode nextOne) {
this.next = nextOne;
}
public LLNode getNext() {
return this.next;
}
/*** Method to remove node from list, fixing links
*appropriately* @param node element to remove from list.
*/
public void remove(LLNode node) {
/* assume you have working code here */
}
/*** Insert the input node after this node
*@param node element to insert after this
**/
public void insertAfter(LLNode node) {
/* assume you have working code here */
}
} //end of LLNode class
public class DrawableNode {
public DrawableNode next;
/* Constructors */
public DrawableNode() {
next = null;
}
/* Methods */
public void setNext(DrawableNode nextOne) {
this.next = nextOne;
}
public Drawable getNext() {
Return this.next;
}
public void remove(DrawableNode node) {
/* Assume working code */
public void insertAfter(DrawableNode node) {
/* Assume working code */
}
}// end of DrawableNode class
Rewrite the DrawableNode class to have it extend the LLNode class. You are to retain in the new version of DrawableNode only the code that is the essence of handling pictures, removing all functionality that is to be handled by LLNode.
Questions, comments, answers at Midterm exam 2 review Spring2007: Understanding inheritance
Links to this Page