Java Code


Java programming environment. Here are instructions for setting up an IntelliJ-based Java programming environment for macOS, Windows, and Linux.

Standard libraries.

Here are the standard input and output libraries that we use throughout the textbook.

Programs in the textbook.

Below is a table of the Java programs in the textbook. Click on the program name to access the Java code; click on the reference number for a brief description; read the textbook for a full discussion. You can download all of the programs as introcs.jar and the data as introcs-data.zip; you can also download all of the programs and data as the IntelliJ project IntroCS.zip.


1 ELEMENTS OF PROGRAMMING
1.1.1 HelloWorld.java Hello, World
1.1.2 UseArgument.java using a command-line argument
1.2.1 Ruler.java string concatenation example
1.2.2 IntOps.java integer multiplication and division
1.2.3 Quadratic.java quadratic formula
1.2.4 LeapYear.java leap year
1.2.5 RandomInt.java casting to get a random integer
1.3.1 Flip.java flippling a fair coin
1.3.2 TenHellos.java your first while loop
1.3.3 PowersOfTwo.java computing powers of 2
1.3.4 DivisorPattern.java your first nested loops
1.3.5 HarmonicNumber.java harmonic numbers
1.3.6 Sqrt.java Newton's method
1.3.7 Binary.java converting to binary
1.3.8 Gambler.java gambler's ruin simulation
1.3.9 Factors.java factoring integers
1.4.1 Sample.java sampling without replacement
1.4.2 CouponCollector.java coupon collector simulation
1.4.3 PrimeSieve.java sieve of Eratosthenes
1.4.4 SelfAvoidingWalk.java self-avoiding random walks
1.5.1 RandomSeq.java generating a random sequence
1.5.2 TwentyQuestions.java interactive user input
1.5.3 Average.java averaging a stream of numbers
1.5.4 RangeFilter.java a simple filter
1.5.5 PlotFilter.java standard input-to-drawing filter
1.5.6 BouncingBall.java bouncing ball
1.5.7 PlayThatTune.java digital signal processing
1.6.1 Transition.java computing the transition matrix
1.6.2 RandomSurfer.java simulating a random surfer
1.6.3 Markov.java mixing a Markov chain
2 FUNCTIONS
2.1.1 Harmonic.java harmonic numbers (revisited)
2.1.2 Gaussian.java Gaussian functions
2.1.3 Coupon.java coupon collector (revisited)
2.1.4 PlayThatTuneDeluxe.java play that tune (revisited)
2.2.1 StdRandom.java random number library
2.2.2 StdArrayIO.java array I/O library
2.2.3 IFS.java iterated function systems
2.2.4 StdStats.java data analysis library
2.2.5 StdStats.java data analysis library
2.2.6 Bernoulli.java Bernoulli trials
2.3.1 Euclid.java Euclid's algorithm
2.3.2 TowersOfHanoi.java towers of Hanoi
2.3.3 Beckett.java Gray code
2.3.4 Htree.java recursive graphics
2.3.5 Brownian.java Brownian bridge
2.3.6 LongestCommonSubsequence.java longest common subsequence
2.4.1 Percolation.java percolation scaffolding
2.4.2 VerticalPercolation.java vertical percolation
2.4.3 PercolationVisualizer.java percolation visualization client
2.4.4 PercolationProbability.java percolation probability estimate
2.4.5 Percolation.java percolation detection
2.4.6 PercolationPlot.java adaptive plot client
3 OBJECT ORIENTED PROGRAMMING
3.1.1 PotentialGene.java identifying a potential gene
3.1.2 AlbersSquares.java Albers squares
3.1.3 Luminance.java luminance library
3.1.4 Grayscale.java converting color to grayscale
3.1.5 Scale.java image scaling
3.1.6 Fade.java fade effect
3.1.7 Cat.java concatenating files
3.1.8 StockQuote.java screen scraping for stock quotes
3.1.9 Split.java splitting a file
3.2.1 Charge.java charged-particle data type
3.2.2 Stopwatch.java stopwatch data type
3.2.3 Histogram.java histogram data type
3.2.4 Turtle.java turtle graphics data type
3.2.5 Spiral.java spira mirabilis
3.2.6 Complex.java complex number data type
3.2.7 Mandelbrot.java Mandelbrot set
3.2.8 StockAccount.java stock account data type
3.3.1 Complex.java complex number data type (revisited)
3.3.2 Counter.java counter data type
3.3.3 Vector.java spatial vector data type
3.3.4 Sketch.java document sketch data type
3.3.5 CompareDocuments.java similarity detection
3.4.1 Body.java gravitational body data type
3.4.2 Universe.java n-body simulation
4 DATA STRUCTURES
4.1.1 ThreeSum.java 3-sum problem
4.1.2 DoublingTest.java validating a doubling hypothesis
4.2.1 Questions.java binary search (20 questions)
4.2.2 Gaussian.java bisection search
4.2.3 BinarySearch.java binary search (in a sorted array)
4.2.4 Insertion.java insertion sort
4.2.5 InsertionTest.java doubling test for insertion sort
4.2.6 Merge.java mergesort
4.2.7 FrequencyCount.java frequency counts
4.3.1 ArrayStackOfStrings.java stack of strings (array)
4.3.2 LinkedStackOfStrings.java stack of strings (linked list)
4.3.3 ResizingArrayStackOfStrings.java stack of strings (resizing array)
4.3.4 Stack.java generic stack
4.3.5 Evaluate.java expression evaluation
4.3.6 Queue.java generic queue
4.3.7 MM1Queue.java M/M/1 queue simulation
4.3.8 LoadBalance.java load balancing simulation
4.4.1 Lookup.java dictionary lookup
4.4.2 Index.java indexing
4.4.3 HashST.java hash table
4.4.4 BST.java binary search tree
4.4.5 DeDup.java dedup filter
ST.java symbol table data type
SET.java set data type
4.5.1 Graph.java graph data type
4.5.2 IndexGraph.java using a graph to invert an index
4.5.3 PathFinder.java shortest-paths client
4.5.4 PathFinder.java shortest-paths implementation
4.5.5 SmallWorld.java small-world test
4.5.6 Performer.java performer–performer graph



Exercise solutions.

Here is a list of solutions to selected coding exercises.


1 ELEMENTS OF PROGRAMMING
1.1.1 TenHelloWorlds.java ten Hello, Worlds
1.1.5 UseThree.java three command-line arguments
1.2.20 SumOfTwoDice.java sum of two dice
1.2.23 SpringSeason.java is month and day in Spring?
1.2.25 WindChill.java compute wind chill factor
1.2.26 CartesianToPolar.java Cartesian to polar coordinates
1.2.29 DayOfWeek.java compute day of week from date
1.2.30 Stats5.java average, min, max of 5 random numbers
1.2.34 ThreeSort.java sort three integers
1.2.35 Dragon.java dragon curve of order 5
1.3.8 FivePerLine.java print integers five per line
1.3.11 FunctionGrowth.java table of functions
1.3.12 DigitReverser.java reverse digits
1.3.13 Fibonacci.java Fibonacci numbers
1.3.15 SeriesSum.java convergent sum
1.3.31 Ramanujan.java taxicab numbers
1.3.32 ISBN.java ISBN checksum
1.3.38 Sin.java sine function via Taylor series
1.3.41 MonteHall.java Monte Hall problem
1.4.2 HugeArray.java creating a huge array
1.4.10 Deal.java deal poker hands
1.4.13 Transpose.java tranpose a square matrix
1.4.25 InversePermutation.java compute inverse permutation
1.4.26 Hadamard.java compute Hadamard matrix
1.4.30 Minesweeper.java create Minesweeper board
1.4.33 RandomWalkers.java N random walkers
1.4.35 Birthdays.java birthday problem
1.4.37 BinomialCoefficients.java binomial coefficients
1.5.1 MaxMin.java max and min from standard input
1.5.3 Stats.java mean and stddev from standard input
1.5.5 LongestRun.java longest consecutive run from stdin
1.5.11 WordCount.java word count from standard input
1.5.15 Closest.java closest point
1.5.18 Checkerboard.java draw a checkerboard
1.5.21 Rose.java draw a rose
1.5.22 Banner.java animate a text banner
1.5.31 Spirograph.java draw spirograph
1.5.32 Clock.java animate a clock
1.5.33 Oscilloscope.java simulate an oscilloscope
2 FUNCTIONS
2.1.4 ArrayEquals.java are two integer arrays equal?
2.1.30 BlackScholes.java Black-Scholes option valuation
2.1.32 Horner.java Horner's method to evaluate a polynomial
2.1.33 Benford.java Benford's law
2.1.38 Calendar.java create a calendar
2.2.1 Gaussian.java overloaded gaussian distribution functions
2.2.2 Hyperbolic.java hyperbolic trig functions
2.2.4 StdRandom.java shuffle an array of doubles
2.2.6 StdArrayIO.java array IO methods
2.2.11 Matrix.java matrix operations
2.2.12 MarkovSquaring.java page rank via matrix squaring
2.2.14 StdRandom.java exponential random variable
2.3.14 AnimatedHtree.java animated H-tree
2.3.15 IntegerToBinary.java integer to binary conversion
2.3.17 Permutations.java all permutations
2.3.18 PermutationsK.java all permutations of size k
2.3.19 Combinations.java all combinations
2.3.20 CombinationsK.java all combinations of size k
2.3.22 RecursiveSquares.java recursive squares
2.3.24 GrayCode.java Gray code
2.3.26 AnimatedHanoi.java animated Towers of Hanoi
2.3.29 Collatz.java Collatz function
2.3.30 BrownianIsland.java Brownian island
2.3.31 PlasmaCloud.java plama cloud
2.3.32 McCarthy.java McCarthy's 91 function
2.3.33 Tree.java fractal tree
2.4.15 PercolationDirectedNonrecursive.java directed percolation