Change Contents of the Bubble
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

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.
  1. 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!
  2. I will try to always point out if an answer is wrong. I won't always point out when the answer is right.
  3. 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
(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().

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 = children.collect();
public Sound collect(){

b.From (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++)
    int mirrorPoint = (int) (getHeight() / 2);
    Pixel topPixel = null;
    Pixel bottomPixel = null;


c. From
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};


d. From
* Method to reverse a sound.
Sound target = new Sound(getLength());
sampleValue = this.getSampleValueAt(srcIndex);
int sampleValue;
public Sound reverse()
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();
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

 public void setUp4(){
    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);

    s = new Sound(FileChooser.getMediaPath("corkpop-tenth.wav"));
    SoundElement two = new SoundElement(s);

    s = new Sound(FileChooser.getMediaPath("clap-q.wav"));
    SoundElement three = new SoundElement(s);


B. From
    public void setUp3() {
    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));
    sn = new SoundNode(clink.append(snap).append(clave));
    sn = new SoundNode(guzdial.append(is).append(is));

    scaledBranch = new ScaleBranch(2.0);
    sn = new SoundNode(clink.append(clave).append(gong));
    sn = new SoundNode(rest.append(chirp).append(clap));

    SoundBranch branch2 = new SoundBranch();
    sn = new SoundNode(clap.append(is).append(snap));
    sn = new SoundNode(clink.append(snap).append(clave));
    sn = new SoundNode(chirp.append(is).append(is));

    scaledBranch = new ScaleBranch(0.5);
    sn = new SoundNode(guzdial.append(is).append(gong));

    SoundBranch branch3 = new SoundBranch();
    sn = new SoundNode(clap.append(clap).append(clave));
    sn = new SoundNode(snap.append(snap).append(clave));
    sn = new SoundNode(snap.append(snap).append(clave));


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) {

    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);

    Picture bg = new Picture(400,400);


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:

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) { = nextOne;

  public LLNode getNext() {

  /*** 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) { = nextOne;

  public Drawable getNext() {

  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