Introduction to Programming Using Java, Fourth Edition

Source Code


THIS PAGE CONTAINS LINKS to the source code for examples appearing in the free, on-line textbook Introduction to Programming Using Java, Version 4.0, which is available at http://math.hws.edu/javanotes/. You should be able to compile these files and use them. Note however that some of these examples depend on other classes, such as TextIO.class and MosaicFrame.class, that are not built into Java. These are classes that I have written. Links to the source code are provided below. To use examples that depend on my classes, you will need to compile the source code for the required classes and place the compiled classes in the same directory with the main class file. If you are using an integrated development environment such as CodeWarrior or JBuilder, you can simply add any required source code files to your project. See Appendix 2 for more information on Java programming environments and how to use them to compile and run these examples.

Most of the solutions to end-of-chapter exercises are not listed on this page. Each end-of-chapter exercise has its own Web page, which discusses its solution. The source code of a sample solution of each exercise is given in full on the solution page for that exercise. If you want to compile the solution, you should be able to cut-and-paste the solution out of a Web browser window and into a text editing program. (You can't cut-and-paste from the HTML source of the solution page, since it contains extra HTML markup commands that the Java compiler won't understand.)

Note that many of these examples require Java version 1.3 or later. Some of them were written for older versions, but will still work with current versions. When you compile some of these older programs with current versions of Java, you might get warnings about "deprecated" methods. These warnings are not errors. When a method is deprecated, it means that it should not be used in new code, but it has not yet been removed from the language. It is possible that deprecated methods might be removed from the language at some future time, but for now you just get a warning about using them.


Part 1: Text-oriented Examples

Many of the sample programs in the text are based on console-style input/output, where the computer and the user type lines of text back and forth to each other. Some of these programs use the standard output object, System.out, for output. Most of them use my non-standard class, TextIO for both input and output. The programs are stand-alone applications, not applets, but I have written applets that simulate many of the programs. These "console applets" appear on the Web pages that make up the text. The following list includes links to the source code for each applet, as well as links to the source code of the programs that the applets simulate. All of the console applets depend on classes defined in the files ConsoleApplet.java, ConsolePanel.java, and ConsoleCanvas.java. These three files, or the class files compiled from them, must be available when you compile any console applet. The class files must be available when the applet is used. (Being "available" means being in the same directory where you are compiling the program, or being in the same directory as the HTML file that uses the applet.) Most of the standalone programs depend on the TextIO class, which is defined in TextIO.java. Either TextIO.java or TextIO.class must be available when you compile the program, and TextIO.class must be available when you run the program. These programs and applets will work with Java 1.1, as well as with later versions.


Part 2: Graphical Examples from the Text


Part 3: End-of-Chapter Applets

This section contains the source code for the applets that are used as decorations at the end of each chapter. In general, you should not expect to be able to understand these applets at the time they occur in the text. Most of these are older applets will work with Java 1.1 or even Java 1.0.

  1. Moire.java, an animated design, shown at the end of Section 1.7. (You can use applet parameters to control various aspects of this applet's behavior. Also note that you can click on the applet and drag the pattern around by hand. See the source code for details.)
  2. JavaPops.java, and applet that shows multi-colored "Java!"s, from the end of Section 2.5. (This depends on SimpleAnimationApplet.java.)
  3. MovingRects.java, the sample animation applet from Section 3.7. (This depends on SimpleAnimationApplet2.java.) This is also listed above, as one of the graphical examples from the text.
  4. RandomBrighten.java, showing a grid of colored squares that get more and more red as a wandering disturbance visits them, from the end of Section 4.7. (Depends on MosaicCanvas.java.) (Another applet that shows an animation based on MosaicCanvas.java is MosaicStrobeApplet.java, the applet version of the solution to one of the exercises for Chapter 4.)
  5. SymmetricBrighten.java, a subclass of the previous example that makes a symmetric pattern, from the end of Section 5.6. Depends on MosaicCanvas.java and RandomBrighten.java.
  6. TrackLines.java, an applet with lines that track the mouse, from Section 6.6.
  7. KaleidaAnimate.java, an applet that shows symmetric, kaleidoscope-like animations, from Section 7.7. Depends on SimpleAnimationApplet.java.
  8. Maze.java, an applet that creates a random maze and solves it, from Section 8.5.
  9. SimpleCA.java, a Cellular Automaton applet, from the end of Section 9.4. This applet depends on the file CACanvas.java. For more information on cellular automata see http://math.hws.edu/xJava/CA/.
  10. TowersOfHanoi.java, an animation of the solution to the Towers of Hanoi problem for a tower of ten disks, from the end of Section 10.5.
  11. LittlePentominosApplet.java, the pentominos applet from the end of Section 11.5. This file defines two classes, LittlePentominosApplet and PentominosBoardCanvas. A pentomino is made up of five connected squares. This applet solves puzzles that involve filling a board with pentominos. If you click on the applet it will start a new puzzle. For more information see http://math.hws.edu/eck/xJava/PentominosSolver/ where you'll also find the big brother of this little applet.
  12. The applet at the end of Section 12.4 is the same Moire.java that was used at the end of Chapter 1.

Part 4: Required Auxiliary Files

This section lists many of the extra source files that are required by various examples in the previous sections, along with a description of each file. The files listed here are those which are general enough to be useful in other programming projects.


David Eck (eck@hws.edu), July 2002