Introduction to Programming Using Java
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. Almost all of these programs use the standard output object, System.out, for output. Many of them use my non-standard class, TextIO, for input. 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. Since TextIO is defined in a package named textio, this means that TextIO.java and/or TextIO.class must be in a directory named textio, which must be in the same directory as the program. There is also a GUI version of TextIO; you can find information about it at the end of this web page.
EnumDemo.java, from Section 2.3, a very simple first demonstration of enum types. The enum types used in this program are defined in the same file as the program. An alternative version, SeparateEnumDemo.java, uses the same enums, but the enums are defined in separate files, Day.java and Month.java
PrintSquare.java, from Section 2.4, reads an integer typed in by the user and prints the square of that integer. This program depends on TextIO.java. The same is true for almost all of the programs in the rest of this list.
ThreeN1.java, from Section 3.2, outputs a 3N+1 sequence for a given stating value.
ComputeAverage.java, from Section 3.3, computes the average value of some integers entered by the user.
AverageNumbersFromFile.java, from Section 3.7, finds the sum and the average of numbers read from a file. Demonstrates the use of try..catch statements with TextIO.
BirthdayProblem.java, from Section 3.8, demonstrates random access to array elements using the "birthday problem" (how many people do you have to choose at random until two are found whose birthdays are on the same day of the year).
ThreeN2.java, from Section 4.4, is an improved 3N+1 program that uses subroutines and prints its output in neat columns.
RollTwoPairs.java, from Subsection 5.2.2, uses PairOfDice.java to simulate rolling two pairs of dice until the same total is rolled on both pairs.
TryStatementDemo.java, from Section 8.3, a small demo program with a try..catch statement that includes autoclosing of a resource.
LengthConverter3.java, from Section 8.3, is a revision of LengthConverter2.java that uses exceptions to handle errors in the user's input.
SimpleParser1.java, SimpleParser2.java, and SimpleParser3.java, from Section 9.5, are three programs that parse and evaluate arithmetic expressions input by the user. SimpleParser1 only handles fully parenthesized expressions. SimpleParser2 evaluates ordinary expressions where some parentheses can be omitted. SimpleParser3 constructs expression trees to represent input expressions and uses the expression trees to evaluate the expressions.
WordListWithTreeSet.java, from Section 10.2, makes an alphabetical list of words from a file. A TreeSet is used to eliminate duplicates and sort the words.
ReverseFileWithScanner.java, from Section 11.2, shows how to read and write files in a simple command-line application. ReverseFileWithResources.java is a version that uses the "resource" pattern in try..catch statements.
DirectoryList.java, from Section 11.2, lists the contents of a directory specified by the user; demonstrates the use of the File class.
DateClient.java and DateServer.java, from Section 11.4, are very simple first examples of network client and server programs.
CLChatClient.java and CLChatServer.java, from Section 11.4, demonstrate two-way communication over a network by letting users send messages back and forth; however, no threading is used and the messages must strictly alternate.
Part 2: Graphical Examples from the Text
The following sample programs use a graphical user interface. All of these programs use JavaFX as the GUI toolbox.
RandomMosaicWalk2.java is a version of the previous example, modified to use a few named constants. From Section 4.8.
GrowingCircleAnimation.java, from Section 5.3, shows an animation of growing, semi-transparent circles. Requires CircleInfo.java. Used as a simple example of programming with object.
RandomCards.java, from Section 6.2, shows 5 cards selected at random from a deck. Depends on the files Deck.java Card.java, and the image resource file Cards.png
SimpleTrackMouse.java, from Section 6.3, shows information about mouse events as the user moves and clicks with the mouse.
OwnLayoutDemo.java, from Section 6.5, shows how to lay out the components in a Pane, which allows you to do your own layout.
SimpleCalc.java, from Section 6.5, lets the user add, subtract, multiply, or divide two numbers input by the user. A demo of text fields, buttons, and layout with nested subpanels.
Complex.java and FullName.java are examples of record classes, from Section 7.4. RecordDemo.java is a simple program that tests the two record classes.
Life.java, from Section 7.6, implements John H. Conway's game of life and is an example of using 2D arrays. This program depends on MosaicCanvas.java.
TrivialEdit.java, from Section 11.3, lets the user edit short text files. This program demonstrates reading and writing files and using file dialogs.
SimplePaintWithFiles.java, from Section 11.3, demonstrates saving data from a program to a file in both binary and character form. The program is a simple sketching program based on SimplePaint2.java.
BackgroundComputationDemo.java, from Section 12.2, demonstrates using a thread running at a lower priority to perform a lengthy computation in the background. (The program computes a visualization of a small piece of the Mandelbrot set, but the particular computation that is done is not important.)
MultiprocessingDemo1.java, from Section 12.2, is a modification of the previous example that uses several threads to perform the background computation. This speeds up the computation on multi-processor machines.
GUIChat.java, from Section 12.4, is a simple GUI program for chatting between two people over a network. It demonstrates using a thread for reading data from a network connection.
netgame.common, from Section 12.5, is a package that defines a framework for networked games. This framework is used in several examples: A chat room, defined in package netgame.chat; a tic-tac-toe game, defined in package netgame.tictactoe; and a poker game, defined in package netgame.fivecarddraw.
TransformDemo.java,from Section 13.2, demonstrates applying various transforms, such as scale and rotate, to the drawing that is done in canvas. Uses the image resource file face-smile.png.
ToolPaint.java, from Section 13.2, is a little paint program that illustrates pixel manipulation and the use of a transparent overlay canvas for some drawing operations. This program requires SimpleDialogs.java.
WebBrowser.java, from Section 13.4, is a simple web browser based on JavaFX's WebView control. This program shows how to manage multiple windows in a JavaFX application. It requires BrowserWindow.java, a subclass of Stage that does most of the work, and on SimpleDialogs.java.
The Mandelbrot program from Section 13.5, which computes and displays visualizations of the Mandelbrot set, is defined by several classes in the package edu.hws.eck.mdbfx. The source code files can be found in the directory edu/hws/eck/mdbfx.
SimpleGraphicsStarter.java and SimpleAnimationStarter.java are small programs that you can edit to make very simple pictures and animations These programs were used in Section 3.9 and in some of the exercises for Chapter 3.
Mosaic.java contains subroutines for opening and controlling a window that contains a grid of colored rectangles. It depends on MosaicCanvas.java. It is used in several examples and exercises in Chapter 4.
netgame.common is a package that defines a framework for networked games, which is discussed in detail in Section 12.5. The netgame packages also includes several examples.
PokerRank.java can be used to assign ranks to hands of cards in poker games. The cards are defined in the class PokerCard.java. There is also a PokerDeck.java All of these classes are part of the package netgame.fivecarddraw, which is discussed in Subsection 12.5.4, but these classes can be used independently of the netgame framework.