Pre-quiz 2-Fall 2007
Q1. Who's your grading TA?
Q2. Answer the following questions by circling either True or False.
- (a) A subclass can access any variables in the parent class whether they are defined public, private, or protected.
________True________False___________
- (b)A subclass can override any method in its super class.
________True________False___________
- (c)REMOVEDked lists are nice because they have dynamic size.
________True________False___________
Q3. What is a static method and why would you ever want one?
Q4. The following is the implementation of the weave() method defined on the class SongNode:
/**
*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
if (current == null) // Did we actually get to the end early?
break; // Leave the loop
}
}
The following commands in DrJava:
Welcome to DrJava.
> SongNode node1 = new SongNode();
> node1.setPhrase(SongPhrase.riff1());
> SongNode node2 = new SongNode();
> node2.setPhrase(SongPhrase.riff2());
> node1.repeatNext(node2,3);
Result in a linked list that can be drawn as
Draw the result of the following commands in DrJava:
Welcome to DrJava.
> SongNode node1 = new SongNode();
> node1.setPhrase(SongPhrase.riff1());
> SongNode node2 = new SongNode();
> node2.setPhrase(SongPhrase.riff2());
> SongNode node3 = new SongNode();
> node3.setPhrase(SongPhrase.riff3());
> node1.repeatNext(node3,5);
> node1.insertAfter(node2);
> node1.weave(node2,1,4);
Questions? Comments? Post them on Pre-Quiz 2-Fall 2007 Questions
Link to this Page