                    ## Final Exam Review Sp2005

Below are questions like those we plan to ask on the Final Exam (Monday May 2 at 2:50 pm). The exam will consist of 5-8 questions like these.

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.

## 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.

Questions, comments, and answers at FinalExam Review Sp2005: From Increase to Reverse

## From Decrease to Gray

The below method decreases read for the target picture, as in myPic.decreaseRed().

``` /**
* Method to decrease the red by half in the current picture
*/
public void decreaseRed()
{
Pixel pixel = null; // the current pixel
int redValue;       // the amount of red

// get the array of pixels for this picture object
Pixel[] pixels = this.getPixels();
// start the index at 0
int index = 0;

// loop while the index is less than the length of the pixels array
while (index < pixels.length)
{
// get the current pixel at this index
pixel = pixels[index];
// get the red value at the pixel
redValue = pixel.getRed();
// set the red value to half what it was
redValue = (int) (redValue * 0.5);
// set the red for this pixel to the new value
pixel.setRed(redValue);
// increment the index
index++;
}
}
```

Recall that a grayscale results from setting R, G, and B all to the same value, and that averaging red, green, and blue is a reasonable way of computing luminance.

Write grayscale() to change the picture to grayscale.

## 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.

Questions, comments, and answers at FinalExam Review Sp2005: Properties of Data Structures

## 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.`

Questions, comments, and answers at FinalExam Review Sp2005: Finding Max for Two Structures

## And a One and a Two

Below is the code from SongNode for weave. Write a new method weaveTwo which takes two input SongNode instances (one and two) and an int named count. For count times, insertAfter one copy of one and two copies of two.

```  /**
* Weave the input phrase count times every skipAmount nodes
* @param nextOne node to be copied into the list
* @param count how many times to copy
* @param skipAmount how many nodes to skip per weave
*/
public void weave(SongNode nextOne, int count, int skipAmount)
{
SongNode current = this; // Start from here
SongNode copy; // Where we keep the one to be weaved in
SongNode oldNext; // Need this to insert properly
int skipped; // Number skipped currently

for (int i=1; i <= count; i++)
{
copy = nextOne.copyNode(); // Make a copy

//Skip skipAmount nodes
skipped = 1;
while ((current.next() != null) && (skipped < skipAmount))
{
current = current.next();
skipped++;
};

if (current.next() == null) // Did we actually get to the end early?
break; // Leave the loop

oldNext = current.next(); // Save its next
current.insertAfter(copy); // Insert the copy after this one
current = oldNext; // Continue on with the rest
}
}

```

Questions, comments, and answers at FinalExam Review Sp2005: And a One and a Two

## Draw the Tree

Below is a method from SoundTreeExample. Draw the data structure that it defines.

```    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 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(guzdial.append(is).append(is));

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

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

SoundBranch branch2 = 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));

root.playFromMeOn();
}
```

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

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

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

// 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));
hhouses.addChild(vhouses); // Yes, a VBranch can be a child of an HBranch!

// Make the monster
hero = new MoveBranch(400,300);
BlueScreenNode heronode = new BlueScreenNode(monster.scale(0.75).flip());

//Assemble the base scene
sceneRoot = new Branch();
}

```

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

## Compare the GUIs

A. Draw the tree described by the below two classes.
B. Explain the difference in what the user sees from creating instances of these two classes. Relate it to trees and rendering.

EXAMPLE ONE:
```/**
* A GUI that has various components in it, to demonstrate
* UI components and layout managers (rendering)
**/
import javax.swing.*; // Need this to reach Swing components
import java.awt.*; // Need this to reach FlowLayout

public class GUItreeFlowed extends JFrame {

public GUItreeFlowed(){
super("GUI Tree Flowed Example");

this.getContentPane().setLayout(new FlowLayout());
/* Put in a panel with a label in it */
JPanel panel1 = new JPanel();
JLabel label = new JLabel("This is panel 1!");

/* Put in another panel with two buttons in it */
JPanel panel2 = new JPanel();
JButton button1 = new JButton("Make a sound");
JButton button2 = new JButton("Make a picture");

this.pack();
this.setVisible(true);
}
```

EXAMPLE TWO:
```/**
* A GUI that has various components in it, to demonstrate
* UI components and layout managers (rendering)
**/
import javax.swing.*; // Need this to reach Swing components
import java.awt.*; // Need this to reach BorderLayout

public class GUItreeBordered extends JFrame {

public GUItreeBordered(){
super("GUI Tree Bordered Example");

this.getContentPane().setLayout(new BorderLayout());
/* Put in a panel with a label in it */
JPanel panel1 = new JPanel();
JLabel label = new JLabel("This is panel 1!");

/* Put in another panel with two buttons in it */
JPanel panel2 = new JPanel();
JButton button1 = new JButton("Make a sound");
JButton button2 = new JButton("Make a picture");

this.pack();
this.setVisible(true);
}
}

```

## Slow! Sick People Crossing!

Below is the code for the class PersonAgent from the DiseaseSimulation example. REMOVEDge it so that, when people get sick, they slow down by 75%.
```import java.awt.Color; // Color for colorizing

/**
* PersonAgent -- Person as a subclass of Agent
**/
public class PersonAgent extends Agent {

public boolean infection;

/**
* Initialize, by setting color and making move fast
**/
public void init(Simulation thisSim){
// Do the normal initializations
super.init(thisSim);

// Make it lightGray
setColor(Color.lightGray);

// Don't need to see the trail
setPenDown(false);

// Start out uninfected
infection = false;

// Make the speed large
speed = 100;
}

/**
* Count infected
**/
public int infected() {
int count = 0;
PersonAgent check;

for (int i = 0; i<agents.size(); i++){
check = (PersonAgent) agents.get(i);
if (check.infection) {count++;}
}

return count;
}
/**
* Become infected
**/
public void infect(){
this.infection = true;
this.setColor(Color.red);

// Print out count of number infected
System.out.println("Number infected: "+infected());
}

/**
* How a Person acts
**/
public void act()
{
// Is there a person within infection range of me?
PersonAgent closePerson = (PersonAgent) getClosest(20,
simulation.getAgents());

if (closePerson != null) {
// If this person is infected, and I'm not infected
if (closePerson.infection && !this.infection) {
// I become infected
this.infect();
}
}

// Run the normal act() -- wander aimlessly
super.act();
}

////////////////////////////// Constructors ////////////////////////
// Copy this section AS-IS into subclasses, but rename Agent to

/**
* Constructor that takes the model display (the original
* position will be randomly assigned)
* @param modelDisplayer thing that displays the model
* @param thisSim my simulation
*/
public PersonAgent (ModelDisplay modelDisplayer,Simulation thisSim)
{
super(randNumGen.nextInt(modelDisplayer.getWidth()),
randNumGen.nextInt(modelDisplayer.getHeight()),
modelDisplayer, thisSim);
}

/** Constructor that takes the x and y and a model
* display to draw it on
* @param x the starting x position
* @param y the starting y position
* @param modelDisplayer the thing that displays the model
* @param thisSim my simulation
*/
public PersonAgent (int x, int y, ModelDisplay modelDisplayer,
Simulation thisSim)
{
// let the parent constructor handle it
super(x,y,modelDisplayer,thisSim);
}

}
```

Questions, comments, and answers at FinalExam Review Sp2005: Slow! Sick People Crossing!

## Explain It in Your Words

A. What's the difference between discrete event and continuous simulations? When would you use each? For each, give an example of a kind of question that you could ask with one but not with the other.

B. Why did we use trees to create animations?

C. Why do we consider it "hard" to insert and delete into the middle of an array? Is it easier or harder with a matrix?

## Interpret the UML

Below is the UML class diagram for the Factory discrete event simulation. A. By what name does a Resource know its queue of agents waiting for the resource?

B. When an instance of Distributor wants to get the object representing the factory, what expression does it evaluate? (Yes, it all is here in the diagram.)

C. If a Truck wanted to check the position of all trucks and distributors, how would it get at all of those objects? What expression would it evaluate?

D. By what name does FactorySimulation access an instance of FrameSequence?