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

Study Guide Final Exam Spring 2011

What’s the tree look like?


Below is the beginning of the TreeNode.java class
public class TreeNode {
  private String data;
  private TreeNode left;
  private TreeNode right;
  
  public TreeNode(String something){
    data = something;
    left = null;
    right = null;
  }
  
  // Accessors
  public String getData() {return data;}
  public void setData(String something){data = something;}
  
  public TreeNode getLeft() {return left;}
  public TreeNode getRight() {return right;}
  
  public void setLeft(TreeNode newleft){left = newleft;}
  public void setRight(TreeNode newright){right = newright;}
  
  // Insert in order
  public void insert(TreeNode newOne){
    if (this.data.compareTo(newOne.data) > 0){
      if (this.getLeft() == null)
      {this.setLeft(newOne);}
      else
      {this.getLeft().insert(newOne);}}
    else {      
      if (this.getRight() == null)
      {this.setRight(newOne);}
      else
      {this.getRight().insert(newOne);}
    }
  }


Imagine that you then executed:
> TreeNode node1 = new TreeNode("Mark")
> node1.insert(new TreeNode("Barb"))
> node1.insert(new TreeNode("Alan"))
> node1.insert(new TreeNode("Matthew"))
> node1.insert(new TreeNode("Katie"))
> node1.insert(new TreeNode("Jenny"))
> node1.insert(new TreeNode("Nancy"))
> node1.insert(new TreeNode("Walt"))
> node1.insert(new TreeNode("Lana")) 

Draw the tree with node1 at its root.

Stacks and Queues

A. STACK
1. For your stack, insertion occurs at the ____________ (first/last).

2. For your stack, removal occurs at the ____________ (first/last).

3. Implement the following methods for a stack according to your specifications for insertion and removal (Assume the linkedlist of elements in the stack is called elements and is already available to you):
     a. public boolean isEmpty()




     b. public Object pop()




     c. public void push(Object item)






B. QUEUE
1. For your queue, insertion occurs at the ____________ (first/last).

2. For your queue, removal occurs at the ____________ (first/last).

3. Implement the following methods for a queue according to your specifications for insertion and removal (Assume the linkedlist of elements in the queue is called elements and is already available to you):
     a. public boolean isEmpty()




     b. public Object pop()




     c. public void push(Object item)

REMOVEDked List methods


TRACING THE CODE
Consider the following code:
public void remove(PositionedSceneElement node){
   //Question 1 
   if (node==this){
      System.out.println("I can't remove the first node from the list.");
      return;
    } 
    //Question 2
    PositionedSceneElement current = this;
    //Question 3
    while (current.getNext() != null){
     //Question 4 
     if (current.getNext() == node){
        current.setNext(node.getNext());
        node.setNext(null);
        return;
      }
      //Question 5
      current = current.getNext();
    }
  }

1. What are the requirements to enter the if-statement? What occurs in the if-statement? Why is this if-statement even necessary?

2. What is occurring in the line directly below the comment? Why is this necessary?

3. What are the conditions to enter the while-loop and under what conditions will the while-loop exit?

4. What are the requirements to enter the if-statement? What occurs in the if-statement?

5. What is occurring in the line directly below the comment? Why is this necessary?


B. WRITING A METHOD
Consider the following code:
public class Element {
  private Element next;
  public Element getNext() {return this.next;}
  public void setNext(Element next) {this.next = next;}
    
  public int length() {return 0;}

  public static void main(String[]args){
    Element node1 = new Element();
    Element node2 = new Element();
    Element node3 = new Element();
    Element node4 = new Element();
    node1.setNext(node2);
    node2.setNext(node3);
    node3.setNext(node4);
    node4.setNext(node1);
    System.out.println(node1.length());
  }
}
Rewrite the length() method so that when called it will print out the correct number of elements in the list. Remember that you do not know how many elements will be in the list so you cannot use a for-loop. You also cannot alter the linked list structure in any way.

public int length()

Picture methods


Consider the following code:
public void grayscale(){
    Pixel pixel = null;
    int intensity = 0;
    Pixel[] pixels = this.getPixels();
  
    for (int i = 0; i < pixels.length; i++){
      pixel = pixels[i];
      intensity = (int) ((pixel.getRed() + pixel.getGreen() + 
                     pixel.getBlue()) / 3);
      pixel.setColor(new Color(intensity,intensity,intensity));
      
    }
  }
Write a Picture class method called public static int countThatColor(Picture picture, Color color, int range) that counts all the colors in an inputted picture that fall within a range of the inputted color. Also write a main method that will test your countThatColor method.

public static int countThatColor(Picture picture, Color color, int range)

From Increase to Reverse


Below is the code from Sound for increasing the volume of a sound.
  /**
   * Increase the volume of a sound
   **/
  public void increaseVolume(double factor){
    SoundSample [] samples = this.getSamples();
    SoundSample current = null;
    
    for (int i=0; i < samples.length; i++) {
      current = samples[i];
      current.setValue((int) (factor * current.getValue()));
    }
  }


Write the method to reverse a sound.


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


Inheritance


public abstract class Car {
    private double mpg;

    public abstract void drive();

    public void repair() { System.out.println("Car’s repair"); }

}//end class Car

public class Toyota extends Car {
    public void drive() { System.out.println("Toyota’s drive"); }

}//end class Toyota

public class Avalon extends Car {
    public void drive() { System.out.println("Avalon's drive"); }

    public void zoom() { System.out.println("Avalon's zoom"); }

}//end class Avalon

public class Camry extends Toyota {
    public void drive() { System.out.println("Camry's drive"); }

    public void washMe() { System.out.println("Camry's washMe");}

}//end class Camry



Given the above classes decide whether the following segments of code will compile or not (be sure to consider both lines):

Car a = new Camry();
a.repair();

Avalon b = new Car();
b.drive();

Car c = new Car();
c.drive();

Car d = new Camry();
d.drive();

Toyota e = new Toyota();
e.repair();

Car f = new Avalon();
f.zoom();

Car g = new Avalon();
((Avalon)g).zoom();

Car h = new Camry();
((Avalon)h).zoom();


Given
Toyota i = new Camry();
i.drive();

What is the output?

Properties of Data Structures


Data structures that we discussed this semester include arrays, matrices, linked lists, circular linked lists, trees, graphs, stacks, queues, and event queues.

One or more data structures match each of the following statements or descriptions. List the data structures for which the statement is true:
A. Can change size dynamically to match the size of the data.
B. Is good for representing hierarchy.
C. Is particularly hard to insert into the middle of.
D. Can take a while to find an element in this data structure.
E. Is good for representing a line of people waiting for a ferris wheel ride.
F. Is good for representing animation states of a character.
G. Is used for representing the samples in a sound.
H. Is used for representing the pixels in a picture.

Finding Max for Two Structures


A. Write a void method called findMax(int[]array) that takes an array of integers as a parameter and it finds and prints both the index and the value of the largest element in the array. Your method should print out a String like the following example:
The largest value is 3000 found at array index position 5.


B. Write a void method called findMax(IntNode head) that takes a linked list of integers as a parameter and it finds and prints both the index and the value of the largest element in the list. Assume that IntNode inherits from LLNode, and each IntNode contains a int instance variable named value, which you should access through the public int accessor getValue(). Your method should print out a String like the following example:
The largest value is 3000 found in the list is at position 5.


Link to this Page