Introduction to Programming Using Java, Fifth 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 5.1. You should be able to compile these files and use them. Note however that some of these examples depend on other source files, such as TextIO.java and MosaicPanel.java, that are not built into Java. These are classes that I have written. Links to all necessary non-standard source code files 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 Eclipse, you can simply add any required source code files to your project.

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; the HTML markup does not appear when the page is displayed in a Web browser.)

Note that many of these examples require Java version 5.0 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. Many of them use my non-standard class, TextIO, for both input and output. For the programs that use TextIO, one of the files TextIO.java or TextIO.class must be available when you compile the program, and TextIO.class must be available when you run the program. There is also a GUI version of TextIO; you can find information about it at the end of this web page.

The programs listed here are stand-alone applications, not applets, but I have written applets that simulate many of the programs. These "console applets" appear on Web pages in the on-line version of this textbook. They are based on TextIOApplet.java, which provides the same methods as TextIO, but in an applet instead of in a stand-alone application. It is straightforward to convert a TextIO program to a TextIOApplet applet. See the comments in the TextIOApplet.java file for information about how to do it. One example of this can be found in the file Interest3Console.java.


Part 2: Graphical Examples from the Text

The following programs use a graphical user interface. The majority of them can be run both as stand-alone applications and as applets. (If you have downloaded the web site for this book, note that most of the jar files for Chapter 6 and Chapter 12 are executable jar files which can be be run as applications.)


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. Many of these are older applets that will work with Java 1.1 or even Java 1.0. They are not meant as examples of good programming practice for more recent versions of Java.

  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, from Section 2.6 is a simple animation that shows copies of the string "Java!" in various sizes and colors appearing and disappearing. This is an old applet that depends on an old animation framework named SimpleAnimationApplet.java
  3. MovingRects.java shows a simple animation of rectangles continuously shrinking towards the center of the applet. This is also a programming example in Section 3.8. It depends on SimpleAnimationApplet2.java.
  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
  5. SymmetricBrighten.java, a subclass of the previous example that makes a symmetric pattern, from the end of Section 5.7. Depends on MosaicCanvas.java and RandomBrighten.java.
  6. TrackLines.java, an applet with lines that track the mouse, from Section 6.8.
  7. KaleidoAnimate.java, from Section 7.5, shows moving kaleidoscopic images.
  8. SimpleCA.java, a Cellular Automaton applet, from the end of Section 8.4. This applet depends on the file CACanvas.java For more information on cellular automata see http://math.hws.edu/xJava/CA/.
  9. TowersOfHanoiGUI.java, from Section 9.5, graphically shows the solution to the Towers of Hanoi problem with 10 disks.
  10. LittlePentominosApplet.java, from Section 10.5, solves pentominos puzzles using a simple recursive backtracking algorithm. This applet depends on MosaicPanel.java. For a much more complex Pentominos applet, see http://math.hws.edu/xJava/PentominosSolver/.
  11. Maze.java, an applet that creates a random maze and solves it, from Section 11.6.
  12. The applet at the end of Section 12.5 is the same Mandelbrot program that is discussed as an example in that section, with source files in the directory edu/hws/eck/mdb.

Part 4: Required Auxiliary Files

This section lists some 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 potentially useful in other programming projects. All of them are also referred to above, along with the programming examples that depend on them.


David Eck, December 2006