CSci 1302 Problem Solving and Algorithm Development II --
Tips on how to get your program to work correctly
The sooner you start following these simple rules, the less time you
will spend in front of a computer screen trying to get your program to
work. So please read carefully and follow.
- Start thinking about your problem set assignment as soon as
possible. Draw diagrams, write pseudocode and/or method descriptions,
but don't start coding right away. The first approach (or two) to a
problem is often incorrect or inefficient. By catching it before you
start writing a program you can save yourself a few hours a week. And
if your first approach is right, you will have a much better idea
of what you are doing by the time you start coding.
- When you are ready to write a program, don't try to write the
whole program on paper. Programs on paper often have tons of
programming mistakes and are impossible to debug. Write down the
detailed description of your program (in English or in pseudocode or
in diagrams), check it to make sure that it makes sense, and start
writing code (see below).
- Create a file with the class name (or open the file that you are
supposed to modify). Sketch out your program by writing comments for
each method or a
part of the program (don't write any code yet!)
- Use incremental approach to programming: write a line or
code, compile, test. This way you know exactly where the mistake is:
it's in the newly added line of code!
If you are writing a Java class, start by writing
stubs for all methods (the initial version of a method that has the
right type, but doesn't do anything. Remember to return a dummy value
from a method that has to return a value). Compile and test the class
with the stubs.
- If you get 55 compiler errors for your program (or any other
number of errors greater or equal than 1):
- Don't panic!
- Start from the top one. Chances are the other 54 errors are
induced by the first one (or, more likely, all 55 errors are induced
by 3 mistakes that you made in a recently added 2 lines of code).
- If the top error doesn't make sense, check the curly braces (use
jEdit to do this: click on the opening brace and it will show you the
matching closing one).
- If you don't understand what's causing an error, look in the book
or read the APIs: this may give you a hint.
- If nothing helps, try commenting out suspected lines of code and
see if the error messages go away or change. Then uncomment the lines
one-by-one, read the error messages.
- Once you have discovered an error, stop and think of how you
should fix it. Don't rush into correcting your code: you will make it
worse by randomly trying to get it to compile. Fix the mistake only
when you understand the problem.
- When working on your program, take breaks, especially if
you get stuck. Have dinner or a snack, take a walk (the best!), call a
friend. It's amazing how a mistake that was escaping you for an hour
is just staring you in the face when you come back after a 10-minute
- If at any time of working on your program you feel that the
program is too large and messy, rewrite it right away! Don't consider
it a waste of time: if you don't, it will only get larger and messier,
and, chances are, you will still have to rewrite it later, only it
will be more painful. When rewriting a program:
- Save the file with a different name, for instance
MyClassVersion1.java instead of MyClass.java.
- Open a new file MyClass.java.
- Copy the parts of the program that you are happy with, as if you
are doing incremental programming; compile and test them as often as
- Rewrite the parts that you are not happy with. Look at the old
code, and see what you can clear or optimize. Creating helper methods
might be helpful. Sometimes you might even want to write additional
classes. Compile and test the new file often!
- If your program doesn't seem to react to any changes you make...
- Save your file, recompile, and run (maybe your forgot to save or
to recompile?) Make sure that the compilation was successful,
otherwise you are running the old code (the result of the last
- If this doesn't help, you might be running a different program
with the same name. Check your path to make sure that you are in
the correct directory. Do ls -l, look at the last modification
time on java files (maybe you are modifying a file in a different
- If this doesn't help, remove all the .class files, recompile the
program, and run it again. This should help. If it doesn't, ask the
instructor or the TA (but see advice for applets below).
- If you are working with an applet, quit all the appletviewer
windows, recompile the program, and start the appletviewer again. If
you are viewing an applet in a browser, empty the browser cache
(Tools -> Internet Options -> General -> Delete Files on Internet Explorer),
save and recompile the program, and open the files in the browser
again. Quitting the browser before re-displaying the applet is not
necessary, but you might want to do it just in case.
- Write comments as you are writing your code (or, better yet,
before!), not afterwards.
- If you have Java questions, ask in class, at the lab, post your
questions on Wiki, ask your instructor or a TA, or other
students. The more you ask, the more you learn!
The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.