Change Contents of the Bubble
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Final Review-Spring 2007

Below are questions like those we plan to ask on the Final Exam (Monday the 31st at 2:50 pm). The exam will consist of 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!

We will be reading your answers. Here are the ground rules for the interaction.
  1. If you don't post, neither will we. We will not be posting official solutions to these problems at all! If one of you gets it right, terrific!
  2. We will try to always point out if an answer is wrong. We won't always point out when the answer is right.
  3. We are glad to work with you toward the right answer. We will give hints, and I'm glad to respond to partial guesses.

Definition Time (Sorry this is a data structures course after all)


Data Structures
Graph
Tree
Binary Trees
REMOVEDked List
Circular REMOVEDked List
Array
Queue
Stack

Questions, comments, answers at Final exam review Spring2007: Definition Time

Writing methods on the Picture class

The following is the clearBlue() method of the class Picture.
/*** Method to clear the blue from the picture (set
 *the blue to 0 for all pixels*/
public void clearBlue()
{
  Pixel[] pixels = this.getPixels();
  Pixel pixel = null;
  int index = 0;

  while(index < pixels.length)
  {
    pixel = pixels[index];
    pixel.setBlue(0);
    index++;
  }
}


Write a method to draw vertical stripes (leaving a gap for the original picture between every stripe) down the picture. Have every-other stripe be red, and the others be green.

Public void vertical_stripes()
{

}



Questions, comments, answers at Final exam review Spring2007:Writing methods on the Picture class

Draw the Tree


Below is a method from SoundTreeExample. Draw the data structure that it defines. BE SURE TO LABEL children versus next nodes!

    public void setUp2() {
    FileChooser.setMediaPath("D:/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);
    
    scaledBranch = new ScaleBranch(0.5);
    sn = new SoundNode(guzdial.append(is).append(gong));
    scaledBranch.addChild(sn);
    root.addChild(scaledBranch);

    SoundBranch branch2 = new SoundBranch();
    sn = new SoundNode(clap.append(clap).append(clave));
    branch2.addChild(sn);
    sn = new SoundNode(snap.append(snap).append(clave));
    branch2.addChild(sn);
    sn = new SoundNode(snap.append(snap).append(clave));
    branch2.addChild(sn);
    root.addChild(branch2);
    
    root.playFromMeOn();
  }


Questions, comments, and answers at FinalExam Review Spring2007: Draw the Tree

Draw a Wolf Tree


Below is the set-up code from WolfAttackMovie. Draw the data structure that it defines.

  public void setUp(){
    FileChooser.setMediaPath("D:/cs1316/mediasources/");
    Picture wolf = new Picture(FileChooser.getMediaPath("dog-blue.jpg"));
    Picture house = new Picture(FileChooser.getMediaPath("house-blue.jpg"));
    Picture tree = new Picture(FileChooser.getMediaPath("tree-blue.jpg"));
    Picture monster = new Picture(FileChooser.getMediaPath("monster-face3.jpg"));

    //Make the forest
    MoveBranch forest = new MoveBranch(10,400); // forest on the bottom
    HBranch trees = new HBranch(50); // Spaced out 50 pixels between
    BlueScreenNode treenode;
    for (int i=0; i < 8; i++) // insert 8 trees
    {treenode = new BlueScreenNode(tree.scale(0.5));
      trees.addChild(treenode);}
    forest.addChild(trees);
    
    // Make the cluster of attacking "wolves"
    wolfentry = new MoveBranch(10,50); // starting position
    VBranch wolves = new VBranch(20); // space out by 20 pixels between
    BlueScreenNode wolf1 = new BlueScreenNode(wolf.scale(0.5));
    BlueScreenNode wolf2 = new BlueScreenNode(wolf.scale(0.5));
    BlueScreenNode wolf3 = new BlueScreenNode(wolf.scale(0.5));
    wolves.addChild(wolf1);wolves.addChild(wolf2);wolves.addChild(wolf3);
    wolfentry.addChild(wolves);
    
    // Make the cluster of retreating "wolves"
    wolfretreat = new MoveBranch(400,50); // starting position
    wolves = new VBranch(20); // space them out by 20 pixels between
    wolf1 = new BlueScreenNode(wolf.scale(0.5).flip());
    wolf2 = new BlueScreenNode(wolf.scale(0.5).flip());
    wolf3 = new BlueScreenNode(wolf.scale(0.5).flip());
    wolves.addChild(wolf1);wolves.addChild(wolf2);wolves.addChild(wolf3);
    wolfretreat.addChild(wolves);
    
    // Make the village
    MoveBranch village = new MoveBranch(300,450); // Village on bottom
    HBranch hhouses = new HBranch(40); // Houses are 40 pixels apart across
    BlueScreenNode house1 = new BlueScreenNode(house.scale(0.25));
    BlueScreenNode house2 = new BlueScreenNode(house.scale(0.25));
    BlueScreenNode house3 = new BlueScreenNode(house.scale(0.25));
    VBranch vhouses = new VBranch(-50); // Houses move UP, 50 pixels apart
    BlueScreenNode house4 = new BlueScreenNode(house.scale(0.25));
    BlueScreenNode house5 = new BlueScreenNode(house.scale(0.25));
    BlueScreenNode house6 = new BlueScreenNode(house.scale(0.25));
    vhouses.addChild(house4); vhouses.addChild(house5); vhouses.addChild(house6);
    hhouses.addChild(house1); hhouses.addChild(house2); hhouses.addChild(house3);
    hhouses.addChild(vhouses); // Yes, a VBranch can be a child of an HBranch!
    village.addChild(hhouses);
    
    // Make the monster
    hero = new MoveBranch(400,300);
    BlueScreenNode heronode = new BlueScreenNode(monster.scale(0.75).flip());
    hero.addChild(heronode);
    
    //Assemble the base scene
    sceneRoot = new Branch();
    sceneRoot.addChild(forest);
    sceneRoot.addChild(village);
    sceneRoot.addChild(wolfentry);
  }



Questions, comments, and answers at FinalExam Review Spring2007: Draw a Wolf Tree

Understanding inheritance


Given the following code:

public class Superhero {
  public String name;
  public int strength;
  public Superhero() {
    name = “Batman”;
    strength = 10;
  }
  public void introduce() {
    System.out.print(“Hello.  I am “ + name + “.  And “);
    if(strength < 15) {
      System.out.println(“I am weak.”);
    } else {
      System.out.println(“I am strong.”);
    }
}


public class  NinjaTurtle extends Superhero {
  public NinjaTurtle(int i) {
    super();
    String[] turtles = {“Leonardo”, “Donatello”, “Raphael”, “Michaelangelo”};

    name = turtles[i];
    strength = 25;
  }
  public void introduce() {
    System.out.println(“I’m “ + name);
  }
}


What will the output of the following statements be?

> Superhero batman = new Superhero();
> Superhero leo = new NinjaTurtle(0);
> leo.introduce();
> batman.introduce();

When you create a method in a subclass that overrides a method from the superclass, how do you invoke the superclass method's behavior? What constraints are there for doing this in a constructor of a subclass?

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) {
    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 atFinal exam review Spring2007:Understanding inheritance

Make a Super Truck


Below is the code for the Truck class from our FactorySimulation. Let’s imagine that our company has just bought a whole new fleet of super trucks that can carry 50% as much (20-30 items per load) and takes 1/3 less time to travel (average of two days per trip). What would you change in the Truck class to describe the new super trucks? Be specific—cross out and rewrite lines below so that this class will work as the new super truck.

import java.awt.Color; // For color

/**
 * Truck -- delivers product from Factory
 * to Warehouse.
 **/
public class Truck extends DEAgent {

  /////// Constants for Messages
  public static final int FACTORY_ARRIVE = 0;
  public static final int WAREHOUSE_ARRIVE = 1;

  ////// Fields /////
  /**
   * Amount of product being carried
   **/
  public int load;

  //// METHODS ///
  /** A new load is between 10 and 20 on a uniform distribution */
  public int newLoad(){
    return 10+randNumGen.nextInt(11);
  }

  /** A trip distance averages 3 days */
  public double tripTime(){
    double delay = randNumGen.nextGaussian()+3;
    if (delay < 1)
      // Must take at least one day
    {return 1.0+((DESimulation) simulation).getTime();}
    else {return delay+((DESimulation) simulation).getTime();}
  }
  /**
   * Set up the truck
   * Start out at the factory
   **/
  public void init(Simulation thisSim){
    // Do the default init
    super.init(thisSim);
    this.setPenDown(false); // Pen up
    this.setBodyColor(Color.green); // Let green deliver!

    // Show the truck at the factory
    this.moveTo(30,350);
    // Load up at the factory, and set off for the warehouse
    load = this.newLoad();
    ((DESimulation) thisSim).addEvent(
              new SimEvent(this,tripTime(),WAREHOUSE_ARRIVE));
  }

  /**
   * Process an event.
   * Default is to do nothing with it.
   **/
  public void processEvent(int message){
    switch(message){
      case FACTORY_ARRIVE:
        // Show the truck at the factory
        ((DESimulation) simulation).log(this.getName()+"\t Arrived at factory");
        this.moveTo(30,350);
        // Load up at the factory, and set off for the warehouse
        load = this.newLoad();
        ((DESimulation) simulation).addEvent(
              new SimEvent(this,tripTime(),WAREHOUSE_ARRIVE));
        break;
      case WAREHOUSE_ARRIVE:
        // Show the truck at the warehouse
        ((DESimulation) simulation).log(this.getName()+"\t Arrived at warehouse with load \t"+load);
        this.moveTo(50,50);
        // Unload product -- takes zero time (unrealistic!)
        ((FactorySimulation) simulation).getFactory().add(load);
        load = 0;
        // Head back to factory
        ((DESimulation) simulation).addEvent(
              new SimEvent(this,tripTime(),FACTORY_ARRIVE));
        break;
    }
  }

// CONSTRUCTORS GO HERE, BUT THEY’VE BEEN REMOVED FOR THE EXAM.
}


Questions, comments, and answers at FinalExam Review Spring2007: Make a Super Truck

Binary Trees are Cute

Below is the code for TreeNode:

TreeNode.java

Write the preorder, inorder, and postorder traversals after the following Strings have been inserted in order:

MarkG
Joel
MarkS
Brian
Kristin
Sarah
Derek
Rory
Ricardo
Sam

Questions, comments, answers at Final exam review Spring2007: Binary Trees are Cute

Short Answer


What is the difference between a Discrete Simulation and a Continuous Simulation?

How is the Event Queue used in a Discrete Simulation?

What is meant by a method being declared to be static?

Often we see references to variables that are in all caps. An example would be SOUTH in the BorderLayout class. What does this typically mean?

What is the point of UML?

What are the different types of relationships between classes and how are they designated in UML?

What is the advantage to a binary search tree ( think "big oh" O() )? What is the perfect Binary Search Tree?

We used random number generators to help control the execution of continuous and discrete event simulations. How were they used and why? What kinds of distributions did we discuss and how are they distinguished?

Questions, comments, answers at Final exam review Spring2007: Short Answer


Links to this Page