Consolation of Java, I

Classes in the same package have special access privileges with respect to one another and may be designed to work together.

You may need to get used to the idea that our [applet] is drawn by a method that is called by an outside agent at arbitrary times. How can we do anything useful with this? How do we control what gets done and when? These answers will be forthcoming. For now, just think about how you would structure applications that draw themselves on command.

We can use the repaint() method of the Applet class to request our applet be redrawn. repaint() causes the Java windowing system to schedule a call to our paint() method at the next possible time; Java supplies the necessary Graphics object.

– “Exploring Java”

Compiling with Allegro 4

I’m starving for something to chew on, smaller than a big project, a little larger than dry exercises, and I’m hoping graphics gives me some respite:

$ gcc `allegro-config --libs` `allegro-config --cflags` test.c

Maybe I am not creative enough? I look into books and cannot draw out the relevance. It is a begging stance to reject the buffet and do nothing instead. But I cannot do nothing; everything waits!

I guess maybe demonstrate mathematical concepts with graphics. That combines the need for ADTs, drawing and math.

Build applets from Netbeans

Right-click on your project and click Properties. Click on the Web Start node on the left side and checkmark the top option. Then choose the middle radio button at the bottom, specifying your applet class. Click OK. Build your project.

Go to your project’s dist\ directory and double-click the launch.jlnp file. Click Run at the prompt, and you should see your applet run in its own window.

It was a joy to write out the last example before the fourth chapter of “Exploring Java.” Everything was explained systematically and I felt comfortable “memorizing strings.” One of my roadblocks with Java was worrying about package paths; if they became obsolete, I would not have learned what was truly useful.

New tricks

“Exploring Java” helped me like Java. It’s an old book from O’Reilly. The first four chapters are a breeze, and the example programs demonstrate applets, threading, button and mouse dragging.

I made sure to draw every figure: the core Java class diagram; repaint() as a request; the hierarchy of objects. I paused before each entered line. It all felt like meditation.

This book placed Java in a context of post-C development. The author seemed like he was transitioning from procedural to object-oriented programming, so I could relate. The concepts were presented tersely but completely.

It’s been a while since I have been excited about building GUI-oriented apps. Let’s hope it continues.


If I ever look back on my notebooks, I would like to read them out: longform, blank lines between paragraphs, local coherence among groups of them. That is the sane approach, because flipping back to scrawled examples and hastily-written insights is not worth much. Sentences are a higher form of abstraction: even when it doesn’t make sense, at least the variables have longer names.

This forces me to the bare minimum of organizing thoughts to arranged sequences of words, a welcome friction. I might make time to rebuild forgotten knowledge by tracing a sentence stack, but seemingly all is worn and weary by scattered fragments, done one day and then a week later (or never after).

Embarrassingly, I rarely look back on my written notes. As we are so far admonished to rewrite and revise, it is the hardest chore to copy even one page to the next. Are not valuable ideas worth that labor?

Cognizance of our mortality

From “Data Structures Using C”:

Thus we see that information itself has no meaning. Any meaning can be assigned to a particular bit pattern, as long as it is done consistently. It is the interpretation of a bit pattern that gives it meaning. For example, the bit string 00100110 can be interpreted as the number 38 (binary), the number 26 (binary coded decimal), or the character ‘&’. A method of interpreting a bit pattern is called a data type.

This frees me from the necessity of defining domain objects, except that I allow a machine’s int to store many more or less than what I expected. Then it is a pleasant justification – design for the sake of bounds checking, further meditations on the form of data.

Fast “pointer” lookup with Access

I use an Access database to store name-value pairs. The key points to a specific file, so the lookup is much improved from looping through each file and searching. Both old and new versions are technically n-squared – but if Access query is a O(1) hashed lookup, it’s much better.

I hear horror stories of /compact erasing the whole database and network corruption being a when-not-if event. I’m fortunate this storage context is historical, so there are many readers and one writer. In that sense, it scales hugely.

The writer is a daily process that creates a new lookup database file. The end-user’s application will consult the latest database file, but will fall back to an older version in case of error. A file semaphore determines when the latest database is completely copied to the search location.

It sounds like a convoluted process to store things outside of a database file, but it sounds sane to me. For my data set, I can regenerate the entire database from scratch (given time). Space is only additional to the file sizes of the actual raw data. New data is appended and then moved.

|-2002-01-01.accdb     <- reap older database files later
|-latest.accdb         <- fresh and hot
|-(LOCK-datetime.lock) <- absent after latest is moved here

|-key123.txt <- linear search but faster than \
|-key456.txt <- looking at each file one by one
|-key999.txt <- old version worst-case: all files searched, not found

Parable of Input

One day, a program was written to count the number of characters in its input. It is said that the task to enter the data would never end:

$ ./chcnt
this sentence should have (4+8+6+4+4+11+4

Each word having lengths 4, 8, 6, and 4, and four spaces; 11 characters starting at the parentheses, and 4 to account for the characters in “11+4,” including itself. That is a reckoning of 41 characters, and the invisible newline (\n) is another.

Post-honeymoon hurry

It’s hard to imagine things at the machine level. I dip into the GNU Manuals and wrangle functions, treating C as a rapid scripting language to gain a vulgar familiarity. A sort of crash course via the standard library, if the work were a scripting language and not placed on the pedestal upon which I have it.

So getopt() to parse options, for starters. Slinging pointers around with abandon to slay the ritual of holy allocation. Which pieces will survive my predilections? It’s possible to develop bad habits in any language; intuition has kept me safe (so far).

If I were writing a rubric of success for this undertaking, it would certainly be this: presenting to you source code of created works, expressions. It would be like writing an essay as therapy.

Problems not my problems

It feels like C was for a different age, a different temporal context. I have to scrounge for meaning in individual char’s. I realized I could encode a variety of things in numbers, and the algorithms to handle them would be similar. That is, treat a string of input as either operations or objects, and each character has a meaning.

/* a: delete a sheet
   b: add a sheet
   c: create a pivottable
   d: select a previous sheet
   e: select the next sheet */
/* sample input: bcda */

Here’s one where I simulate the values without straying from K&R’s first chapter:

/* a: the value of the first field is HADDOCK   
   b: the value of the first field is CRIMSON
   c: the value of the first field is something else
   d: the value of the second field is FLASK
   e: the value of the second field is something else */
/* test cases:
   c,e */