Office Hours: Monday-Friday from 10-11am or by appointment
public void init() - executed once when method starts up
Usually includes all code for layout and initialization of object creation
public void start() - executed after init & every time return to page containing applet
public void paint(Graphics g) - called after init and start to draw
applet.
Called automatically anytime applet needs to be repainted
public void stop() - called to stop applet (usually not necessary)
public void destroy() - called when applet being removed from memory
- used to release or destroy any resources in use.
Calling paint problematic since usually don't have Graphics object in hand. Instead call repaint(); which invokes "update" method (passing it a Graphics object).
The update method erases any drawing done previously and then calls paint with the Graphics object.
Omit method calls to super, show, or setSize. Note that default layout manager for Applet is FlowLayout (rather than BorderLayout used by Frame) so may have to explicitly setLayout if desire layout different from FlowLayout.
If no interaction with environment then can consider start() as the main program and call other methods from there. If applet works via interaction with environment then often omit start() or just have it reinitialize state so can start over.
Have I Got a DEAL for You!
Suppose someone wants to sell you a program that will enable you to stop wasting lots of time in your programs. They will sell you a program that will analyze any other program (before you run it) and tell you whether or not it will stop. Should you buy the program (or try to write it yourself)?
You try out the program on lots of your programs and it seems to work properly. Then you get the inspiration to try it out on one last program. Here is the program:
public class Debunk { /* CharlatanType contains method halts, which takes a filename as input and returns true iff the program contained in that file is a legal Pascal program that halts on empty input. */ public static void what(String fileName) { CharlatanType charlatan = new CharlatanType(); if (charlatan.halts(FileName)) { while (true){} } // else halt } public static void main(String[] args) { what("Debunk.java"); } }
Following the usual style in Java, we name the file in which this program is stored, Debunk.java.
We begin executing the program Debunk, but in a few moments smoke starts rising from the computer, and after a few more minutes the machine grinds to a halt showing the frowning Mac face, never to run a program again.
What happened?
Let's analyze the program:
When what("Debunk.java") is executed, the first thing that happens is that Halts("Debunk.java") is called.
It will either return true or false. Look at the cases:
1. Suppose halts("Debunk.java") returns true (meaning the program in Debunk.java will eventually halt). Then the program goes into an infinite loop, never halting. Thus the program in Debunk.java will never halt. CONTRADICTION!
2. Suppose halts("Debunk.java") returns true (meaning the program in Debunk.java will not halt). Then the program immediately halts. Thus the program in Debunk.java will eventually halt. CONTRADICTION
What went wrong? The only possibility is that we couldn't possibly have such a program! In other words, without even looking at the code of halts, we have proved that it could not possibly work the way it claimed to. Thus no one could write such a program.
There are many such programs that would be useful to have that are not possible to write. This is discussed further in CS361, Theory of Computation.
Exam: 2 1/2 hours. Comprehensive with extra attention to topics since last exam:
Graphs, Dictionary, Concurrency.
Questions will be similar to those of midterm exams.