1.   Elements of Programming


Overview.

Our goal in this chapter is to convince you that writing a computer program is easier than writing a piece of text such as a paragraph or an essay. Writing prose is difficult: we spend many years in school to learn how to do it. By contrast, just a few building blocks suffice to take us into a world where we can harness the computer to help us solve all sorts of fascinating problems that would be otherwise unapproachable. In this chapter, we take you through these building blocks, get you started on programming in Java, and study a variety of interesting programs.


Java programs in this chapter.

Below is a list of Java programs in this chapter. 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.

REF PROGRAM DESCRIPTION
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 EightHellos.java Your first while loop
1.3.3 PowersOfTwo.java Computing powers of two
1.3.4 DivisorPattern.java Your first nested loops
1.3.5 Harmonic.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 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


Exercises.