Tuesday, August 07, 2007

Data acquisition

I have been using Java a good bit at work recently, so I am even more convinced that it can be used many places that I had been using C and C++. In particular, with the addition of third party classes, I am able to do a lot of the serial-based data acquisition and control that I had been doing with LabVIEW. There is nothing at all wrong with LabVIEW except it costs a fortune. And it isn't possible for me to distribute source code with it to work with others who do not have LabVIEW available.

The libraries that I use for serial port communication are RXTX. These are very useful, and fairly easy to use. The documentation at the site is a little scattered, but Googling RXTX gives some good hits and examples.

I will post a little something about this later. Right now, I am not happy with the way my source code looks in HTML, so I have not been posting much in the way of code or tutorials. I think I will have this worked out quickly, though.

Tuesday, July 03, 2007

Tutorial Outline

Here's what I plan to do as an introduction to Java:

First, we'll run through some simple, text-base applications. This will introduce the basic structures and data types in the language, and will serve to introduce object-oriented programming. Object orientation is where much of the magic and power comes from in Java. Object oriented programming can be a little weird at first.

As quickly as possible, we'll get into the graphics and networking capabilities of Java. If you have little programming experience, it will be difficult to appreciate just how easy Java makes this. In fact, it is nothing short of astounding what you can do with just a little practice.

We'll work through a couple of games that I have put together as examples- Craps and Go Fish. We will create classes of object that will encapsulate the behavior of dice and cards. With these classes, we'll construct our games. Then, with these same classes, you will be able to write your own examples, like, for instance, Yahtzee and Poker, or whatever sounds interesting. I'll post suggested approaches, and answer questions if people send me them.

These examples will teach you a lot about the Java language. Enough, in fact, that you will be able to use it to write programs to do things that you want done. Games are non-trivial, fun ways of learning programming languages.

Then, if anyone is actually paying any attention, and I am not just farting into the great Chinook, we can pick a scientific application and write some software to achieve some goal. If not, I'll just figure out something cool to do, and post that.

Sunday, July 01, 2007

Good Java Book and online tutorial

It won't be possible for me to cover everything here, and there are two very good resources available on the web that I want to have us able to refer to. Sort of the textbooks for this tutorial.

First is Sun's Java Tutorial. This is exhaustive, and exhausting. It covers most everything, I guess, but it doesn't tell much of a story, which I hope I will be able to do here. There is the pervasive stench of marketing, which I detest. Nevertheless, it is a fundamental source to look to. I especially think the really high level perspective it offers is good, like here. I won't suggest going through this tutorial, however, because I personally don't like it. There is something cloying and cutesy about Sun's presentation that makes me unable to stomach their tutorial straight through, though I acknowledge that it is a great resource in small bursts.

For a tutorial that you can actually follow without gagging, I like David J. Eck's Introduction to Programming Using Java, fifth edition. It is free to download, so the price is right. More than that, though, I found it very easy to read, free from Sun hype, and its examples are very illuminating.

Java- getting started

If this were a computer science course, I would probably prattle on about how you need to understand how use Java from the command line, with no fancy development environment to provide you with hints or shortcuts. Since the people I am talking to are not CS students, and might not know what a 'command line' or an 'integrated development environment' is, I'm going to cut to the chase and tell you what to get and where to get it to begin the process of learning and using Java.

An integrated development environment (IDE) is something like a text editor, but that will watch your programming for mistakes, and that will invoke the mechanisms by which your code is translated into something the computer can run. I will cover a lot of this later, but I sincerely believe that getting yourself set up with some good tools right off the bat will provide both context and motivation for learning. To some extent, it does provide tools that can become crutches. No matter to us. We want to get something interesting to happen quickly. We can go back and determine in as much detail as we want how the guts of the system work, but for the kind of thing a scientist is likely to want to do with Java, this is not immediately necessary, and may never become so. So let's dive in.

Step 1: Download and install the Java Developer Kit (JDK)
If you have a Mac with OS X, skip this step. Your computer came with Java installed. There might be an update to the JDK 5.0 version that you need to download from Apple, but probably not.

For Linux and Windows, download the JDK from Sun for your OS. Note that JDK 5.0 is not the latest and greatest- it is, however, the highest version that is available on the Mac as well as the other platforms. Apple has been tight-lipped about why they are not up-to-date, but some say it is because they are waiting for the Leopard version of OS X to release this. I don't know or care; the Java 5 system is fine for learning.

Keep track of where stuff gets put by the installer. Everything should be pretty well spelled out in the Sun documentation, but one never knows.

I am counting on you to do this. There are just too many permutations of OS and machines and directory structures possible to give an account for everyone. On the other hand, I have done this for my Linux and Windows boxes, and just followed the Sun instructions. Nothing bad happened, so far as I can tell.

Step 2: Download and install the Eclipse IDE for Java

There are lots of IDEs available, many totally free. Eclipse is my personal favorite, but NetBeans is fine, and I quit caring a long time ago, once I realized that Eclipse is friendly enough for my needs, and that it doesn't stymie me. My opinion might change if I were writing thousands of lines of code to run CitiBank or something, but I would probably have already jumped off a bridge if that were the case, so the point is moot. Get Eclipse for now, so that we can all work together. If you want to pick something else later, I'll look the other way.

Go to the Eclipse website and download the Eclipse IDE for Java Developers. Do whatever they tell you to do to get set up.

Step 3: Create a "Hello, World" project

The first thing we want to do is confirm that the system we have set up will work as a coherent whole. So we need to create a test project and make sure we can get something to run.

Start the Eclipse IDE. Go to File, New, Project. You should see something like the following:

Make sure you are creating a new Java project and click next to see:

I gave the project the name My First Project. Do whatever you like. Naming can be critical, but it isn't yet. Click finish to create the project. When you do, you will see a little file folder appear, but there will be nothing in it. So go back up to File, New and make a new Class. Classes are the workhorses of Java. We'll get to that.

I am calling my class "HelloWorld". Notice that there is a gripe message telling me that use of the default package is discouraged. I don't care at the moment so I'll ignore this. Look down at the options below the class name. Under "Which method stubs would you like to create?" I have checked public static void main(String args[]). This statement looks a bit involved, but suffice to say that this is the entry point for execution in the program we are writing. There is much more to say, but clicking this option saves a good bit of typing in the short run. We'll dissect this later.

Clicking finish pops open the following file:

Notice the line that says //TODO Autogenerated method stub. The double slash tells the system that this is a comment. Commenting code is a great thing to begin doing as soon as you start learning to program. Explaining to yourself what you are doing, or confessing to yourself that you do not know exactly how stuff works, is a great self-teaching tool, and it also reminds you many days, months, or years later what you were up to.

Replace the comment with System.out.println(Hello, World); as shown in this pic:

Now save this, by clicking the disk icon or by the File Save route. If you see a red x along side a line of your code, it means there is an error, and the thing won't run without fixing it. Likely errors here are few, but could be failure to capitalize correctly, or leaving off the final semicolon. Make sure these are all fixed, and the file is saved, then go to the menu item Run and choose Run... and a dialog will appear- pick Java application and HelloWorld as the main class:

Once this is done, click the Run button. The line "Hello World" should appear in the tab marked "Console" at the bottom of the IDE:

Whew! A lot of work for a little reward, and probably plenty of uncertainty and confusion to go with it. However, if this worked, you are indeed set up and ready to roll, and we will jump into learning Java for real starting next time. Without these things in place, your life would be a lot harder.

New Direction

This blog is going to focus, at least for the near term, on two goals that are, more or less, in keeping with the spirit of the blog's initial purpose. First, I am going to present an idiosyncratic tutorial on Java, the programming language, aimed at scientific types who are not programmers- chemists are my imagined audience, but who knows who, if anyone, might look in.

The second goal is to present, again for those who are technical, but out of the know, a tutorial on some aspects of electronics. I want to cover things that can't be found readily in books or on the web, at least not in the way that I hope to get it across. My approach will be fundamental, but practical. I want to cover the science behind, for example, transistors, while still offering enough in the way of simplified models to allow the reader to design something with a transistor.

Both of these are likely greatly over-ambitious. We'll see. Both electronics and Java program are subjects that, at the right level of abstraction, give a person a lot of power to explore and discover. For scientists, they are tools that are so readily available that it is sort of tragic that they are not better known. I don't have the time or space to teach everything, but I hope I can get people started, and that I can enable them to use the phenomenal set of readily available tools there for the taking.

Wednesday, February 14, 2007

Bicycles for the Mind

I am working on a Mac right now. I am not certain that I would describe it as a bicycle for my mind, so much as a mental giant robotic exoskeleton like in Gundam Wing. The power multiplication that a computer provides is far beyond what we hoped in the early days of computing, due in no small part to the internet.

In this segment, as Jobs sort of muses about the limitations of humans, and how the computer helps free us from these limitations, I am struck by his graph of animal speeds. It reminds me how novel it was to be able to graph something back in the early '80s without having to get out graph paper and a ruler. Just a decade earlier, there were not even pocket calculators. (Calculators must be skateboards, and slide rules pogo sticks, for the mind, to torture a metaphor.)

The power to analyze, and plot, and massage data has grown exponentially since those early, naive days. With a few mouse clicks, I can solve problems that would have once taken hours to write down properly. I can calculate the structures of molecules, the behavior of transistors, the flow of heat, and the extent and shape of magnetic fields, and plot all this in 2 or 3 dimensions, in full color, animated, all before lunch. And I can access data and algorithms from literally every corner of the world at any time of day. Plotting a handful of points seems sort of precious by comparison. Nevertheless, Steve Jobs was right in thinking that this sort of thing would change the world.