Tips for beginning Java programmers

Four days before finishing this applet we were completely clueless about Java, and we learned mostly by trial and error (compiler errors especially!). Here are some tips to help you avoid making the same mistakes we did.

Think it out first

Think out your program thoroughly ahead of time. Decide what it should look like, how it should act. Keep its operation relatively simple.

Modular design

Decide what objects you will need and how they will interact. Try to keep your design as modular as possible: try to limit communication between your objects to simple function calls and such. Data hiding makes it much easier to modify your program and correct bugs later on (if, like us, you are new to object-oriented programming, check out some of these links). >Starting small
Write and test your objects one at a time. Start with a simple applet then, once you've got it working, add functions, variables, etc. If you have to, write blank classes with which your applet can interact. Once it's working, write the code for the next class and get it working. This will make your life much simpler than if you try to write everything at once and then pray for it to work (rest assured, it won't!).

Compiling

When your code is typed in and you want to compile, don't forget to check whether you've imported the appropriate parts of the Java API. Also, remember this (it caused me alot of grief): any superclass must have a constructor with an empty argument list which is called by the constructor of its subclasses. The JDK compiler doesn't explain this error very well. Beware!

Debugging

The JDK (Java Developer's Kit) comes with a debugger so complicated to use it makes income tax returns look like child's play. There is an alternative: once your applet has compiled, run it under Netscape. From the Options menu, choose Show Java Console. If your applet throws any uncaught exceptions, they will show up there along with the line number in your code at which they occurred. From within your code, you can also post information to the Java console using System.out.println(String message) and System.err.println(String message). Use this to output to send yourself information on the current state of execution, current values of variables, etc.

Paint, Repaint

When you want to draw something on the screen, always go through the paint method (i.e. overload it). The system will then send you the appropriate graphics context to let you do your drawing. If you try to find the graphics context elsewhere, you may run into NullPointerExceptions. When you want to tell your object to run its paint method, call repaint() .This will call update which will in turn call paint once it has a peer for the given object.

Documentation

Despite the gigaBytes of information floating around on the Web about java, I've found that the best is the stuff published by JavaSoft (Sun). They have a Tutorial , the language specification and the API specification . Add bookmarks in your Web browser and consult these frequently. They are very useful.

Checking out at other applets

Before deciding exactly how you want your applet to look and function, browse around and look at what other people have done. Because alot of applet writers publish their code, later on, when you're writing your own code, you can look at how they've implemented a particular structure to get an idea of how to make the best use of the API. Some of the best applets can be found on Gamelan.


Back to the ADT_Demo applet page