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.
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.
- 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!
- We will try to always point out if an answer is wrong. We won't always point out when the answer is right.
- 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.
Questions, comments, and answers at Student113
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 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 Sp2005: 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 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();
this.getContentPane().add(panel1);
JLabel label = new JLabel("This is panel 1!");
panel1.add(label);
/* Put in another panel with two buttons in it */
JPanel panel2 = new JPanel();
this.getContentPane().add(panel2);
JButton button1 = new JButton("Make a sound");
panel2.add(button1);
JButton button2 = new JButton("Make a picture");
panel2.add(button2);
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();
this.getContentPane().add(panel1,BorderLayout.NORTH);
JLabel label = new JLabel("This is panel 1!");
panel1.add(label);
/* Put in another panel with two buttons in it */
JPanel panel2 = new JPanel();
this.getContentPane().add(panel2,BorderLayout.SOUTH);
JButton button1 = new JButton("Make a sound");
panel2.add(button1);
JButton button2 = new JButton("Make a picture");
panel2.add(button2);
this.pack();
this.setVisible(true);
}
}
Questions, comments, and answers at FinalExam Review Sp2005: Compare the GUIs
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
import java.util.LinkedList;
/**
* 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;
LinkedList agents = simulation.getAgents();
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
// Your class.
/**
* 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?
Questions, comments, and answers at FinalExam Review Sp2005: Explain It in Your Words
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?
Questions, comments, and answers at FinalExam Review Sp2005: Interpret the UML
Links to this Page