We've provided a brief overview of the central topics in the second part of this class. It is in a sample quiz format, with six negative multiple choice questions. Be sure to read the Quiz II Handout for additional details about the quiz.
We say an object in Java is self-animating when a new thread of control comes into being with every new instance of that object. We should expect animated objects to have a run method, which contains an interactive control loop. Here is a class that should be familar from the Design Project:
public class Beeper implements Runnable { private Thread spirit; private boolean beeping = false; public class Beeper () { this.spirit = new Thread(this); this.spirit.start(); } public void alarm() { this.beeping = true; } public void snooze() { this.beeping = false; } public void beep() { // code to cause the system bell to go off. } public void run() { while (this.beeping) { this.beep(); } } }
Which one of the following statements is true?
Though there is a subtle difference between interactive control loops and event-driven programming, it is still useful to recognize the difference. Which of the following statements about the Documentation Project is true?
In the Documentation Project, you saw that the ButtonHandler viewed all of its buttons as instances of ButtonObj, when in actuality they were subclasses of ButtonObj. This ability to view a object as having several different types is called polymorphism. The code exploited polymorphism by making each button override the handleButton() method of their superclass. This permits very different operations to occur with a single method call; so that the ButtonHandler dispatch code was kept very simple, and each button took responsibility to handle the events.
When you find yourself writing pieces of code that are very similar, are of general usefulness, or see a design pattern crop up in several places, perhaps it is time to harness the power of abstraction. It is also a good idea when you want to abstract away the details of your code so that people using your code are presented with a simple and intuitive view of your work.
There are some Java keywords that can supplement our existing universe of objects: static, final, and abstract. Let us see an example that uses all three:
public class Library { // different sections of a library private static final int PERIODICALS = 0, REFERENCE = 1, NONFICTION = 2, FICTION = 3; // every library has a librarian public static final Librarian libby = new Librarian(); // Some libraries use Dewey Decimal, some allow more advanced searching // External classes need to provide the section they want to search. public abstract Book[] lookup(int section, String identifier); // get help public String askForHelp(String topic) { return libby.getAdvice(topic); } // just in case our librarian needs to retire public void setLibrarian(Librarian libby2) { this.libby = libby2; } }
Which one of the following is false?
Finite state machines are a powerful tool when used correctly. However, it is easy to miss a few states or transitions when translating a design from paper into Java code. Here is a code fragment from a state machine:
switch (state) { case 0: state = 3; break; case 1: state = 0; break; case 2: state = 1; break; case 3: state = 2; break; default: // do nothing }
Which one of the following is equivalent? Assume that state is an integer field; and remember that integers can take on any value in the range [-2^32, +2^32-1]
Inheritance refers to the relationships between superclasses and their subclasses. Overriding means replacing a method with a more specific method for a particular subclass. When overriding constructors, note that super(...) must always be the first line of active code. Be sure you know how to override methods. Work from the bottom up when figuring out which method will get called (start with the subclass, if no method by that name, look in the superclass, in no method by that name, look in its superclass...).
For loops, while loops, switch/case statements.
Here's an interesting class:
public class Mirror { public static void main(String[] args) { System.out.println(args[args.length]); } }
When we type at the command line:
javac Mirror.java java Mirror "hello world" "goodbye"
Which one of the following happens?
The ideal way to document your code is to provide an overview at the beginning of each class, and then comment each field and method, and those parts of the code that warrant an explanation. However, comments can sometimes be a source of trouble! Here is a story:
There was once a company that produced military-grade flight simulators. The day before a major demo, one of the engineers modified one of the many programs that constituted the demo software. When the night shift arrived, they fortunately had the foresight to check the demo one more time, and found that it crashed catastrophically. Gleefully, the night shift woke up the engineer for an explanation:
"Dude, what did you do? You broke the demo."
"No, couldn't be... I just added one comment!"
The members of the night shift laughed at this and decided to let the poor man sleep; they knew where the bug was now.
Here might be such a piece of code (messy on purpose):
import java.awt.*; // this is supposed to be an animate object that knows how to render polygons public class StealthGraphicsEngine implements Runnable { public Thread spirit; // thread object public Polygon[] polyList; // a list of polygons public String demoData /* ; hardcoded for now */ = "DEMO.3DS"; // public Graphics doubleBuffer; // we don't need this for the demo /* The constructor. // hey, should the constructor take a string which is the filename to load? */ public class StealthGraphicsEngine() { polyList = new PolygonList[1000]; // the demo only handles 1000 polygons // fill(polyList, demoData); /* this fills the array with demo data */ this.spirit = new Thread( // do I need to cast this to a Runnable? this); /* create a new thread of control and this.spirit.start(); * make sure that run() gets called */ } public void fill(Polygon[] plist, String filename) { // more code here } public void render(Polygon p) { // more code here } // this is the interactive control loop public void run() { while (true) { // infinite loop for (int i=0; i<polygonList.length; i++) { /* for every polygon */ render(polygonList[i]); // render it } } } }
Which line of code has been disabled, causing the catastrophe?
You can see how well you did on this sample quiz. Below are listed the correct answers. If you have questions, you can send us mail. Remember that eliminating a wrong answer gives you +2 points, and eliminating the correct answer gives you -10 points.
Since there are only six questions on this sample quiz, the absolute maximum score would be +60 and the absolute minimum would be -60. There will be fifteen questions on the actual quiz.
This course is a part of Lynn Andrea Stein's Rethinking CS101 project at the MIT AI Lab and the Department of Electrical Engineering and Computer Science at the Massachusetts Institute of Technology.
Last modified: Thu Jul 10 13:01:20 1997