Standard Libraries

Standard libraries.

Below is a table of the input and output libraries that we use throughout the textbook and beyond.

1.5 read numbers and text from standard input
1.5 write numbers and text to standard output
1.5 draw geometric shapes in a window
1.5 create, play, and manipulate sound
1.5 create, play, and manipulate stereo sound
1.5 play MIDI notes
2.2 generate random numbers
2.2 compute statistics
2.2 read and write 1D and 2D arrays
3.1 process one color image
3.1 process color images
3.1 process grayscale images
3.1 read numbers and text from files and URLs
3.1 write numbers and text to files
3.1 draw geometric shapes
3.1 support keyboard and mouse events in Draw
3.2 measure running time (wall clock)
3.2 measure running time (CPU) read bits from standard input write bits to standard output read bits from files and URLs write bits to files

Using the standard libraries.

The file stdlib.jar bundles together all of our standard libraries into one file. To access the libraries, you must add stdlib.jar to your Java classpath. There are many ways to do so (and many opportunities to make mistakes). Here are the two recommended ways: Here are some alternatives:

Standard input and standard output. and are libraries for reading in numbers and text from standard input and printing out numbers and text to standard output. Our versions have a simpler interface than the corresponding Java ones (and provide a few tecnical improvements). reads in a sequence of real numbers from standard input and prints their average on standard output.
% java Average
10.0 5.0 6.0 3.0 7.0 32.0
3.14 6.67 17.71
Average is 10.05777777777778 and are object-oriented versions that support multiple input and output streams, including reading from a file or URL and writing to a file. reads in data from the URL specified on the command line and save it in a file with the same name.
% java Wget ""
% more codes.csv
United States,USA,00

Binary standard input and standard output. and are the analogs for binary data. reads a binary file from standard input and writes it to standard output.
% java Copy < mandrill.jpg > copy.jpg
% diff mandrill.jpg copy.jpg

Standard drawing. is an easy-to-use library for drawing geometric shapes, such as points, lines, and circles. draws a right triangle and a circumscribing circle.

  % java RightTriangle
right triangle and circumscribing circle illustrates how to produce an animation using standard drawing. is an object-oriented versions that support drawing in multiple windows.

Standard audio. is an easy-to-use library for synthesizing sound. reads in a frequency and duration from the command line, and it sonifies a sine wave of the given frequency for the given duration.
% java Tone 440.0 3.0

Image processing. is an easy-to-use library for image processing. takes the name of a picture file and two integers (width w and height h) as command-line arguments and scales the image to w-by-h.

% java Scale mandrill.jpg 298 298
% java Scale mandrill.jpg 200 200
% java Scale mandrill.jpg 200 400

Q + A

Q. Can I use your code in my project?

A. Our library stdlib.jar is released under the GNU General Public License, version 3 (GPLv3). If you wish to license the code under different terms, please contact us to discuss.

Q. If I use a named package to structure my code, the compiler can no longer access the libraries in stdlib.jar. Why not?

A. The libraries in stdlib.jar are in the "default" package. In Java, you can't access classes in the default package from a named package. If you need to use our libraries with a named package, you can use algs4.jar.

Warning: if you are taking our Princeton or Coursera course, you must use stdlib.jar to facilitate grading.