




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · February 19, 2009 7:44 AM 2
Display (^) Speakers Printer MP3 Player Keyboard Mouse Hard drive Network Digital camera Microphone Hard drive Network 3
Display (^) Speakers Printer MP3 Player Keyboard Mouse Hard drive Network Digital camera Microphone Hard drive Network 4
Mac OS X Microsoft Windows
5
public class RandomSeq { public static void main(String[] args) { int N = Integer.parseInt(args[ 0 ]); for (int i = 0 ; i < N; i++) { System.out.println(Math.random()); } } } %^ java^ RandomSeq^4
6
7
14 % java RandomSeq 1000 > data.txt redirect stdout
15 % more < data.txt
… % java Average < data.txt
redirect stdin
16
% java RandomSeq 1000000 | java Average
% java RandomSeq 1000000 | java Average
18
(0, 0) (1, 0) (!, !! 3 ) public class Triangle { public static void main(String[] args) { double t = Math.sqrt(3.0) / 2.0; StdDraw.line(0.0, 0.0, 1.0, 0.0); StdDraw.line(1.0, 0.0, 0.5, t); StdDraw.line(0.5, t, 0.0, 0.0); StdDraw.point(0.5, t/3.0); } } % java Triangle 19
public class PlotFilter { public static void main(String[] args) { double xmin = StdIn.readDouble(); double ymin = StdIn.readDouble(); double xmax = StdIn.readDouble(); double ymax = StdIn.readDouble(); StdDraw.setXscale(xmin, xmax); StdDraw.setYscale(ymin, ymax); while (!StdIn.isEmpty()) { double x = StdIn.readDouble(); double y = StdIn.readDouble(); StdDraw.point(x, y); } } } rescale coordinate system read in points, and plot them 20
% more < USA.txt 669905.0 247205.0 1244962.0 490000. 1097038.8890 245552. 1103961.1110 247133. 1104677.7780 247205. ... % java PlotFilter < USA.txt coordinates of 13,509 US cities bounding box
27
2% .50^ .27y probability new x new y 15% -.14x^ +^ .26y^ +^ .57^ .25x^ +^ .22y^ -^. 13% .17x^ -^ .21y^ +^ .41^ .22x^ +^ .18y^ +^. 70% .78x^ +^ .03y^ +^ .11^ -.03x^ +^ .74y^ +^. 28
(rx, ry) (vx, vy) (-1, -1) (+1, +1) 29
public class BouncingBall { public static void main(String[] args) { double rx = .480, ry = .860; double vx = .015, vy = .023; double radius = .05; StdDraw.setXscale(-1.0, +1.0); StdDraw.setYscale(-1.0, +1.0); while(true) { if (Math.abs(rx + vx) + radius > 1.0) vx = - vx; if (Math.abs(ry + vy) + radius > 1.0) vy = - vy; rx = rx + vx; ry = ry + vy; StdDraw.setPenColor(StdDraw.GRAY); StdDraw.filledSquare(0.0, 0.0. 1.0); StdDraw.setPenColor(StdDraw.BLACK); StdDraw.filledCircle(rx, ry, radius); StdDraw.show( 20 ); } } } bounce position constant velocity update position clear background draw the ball turn on animation mode: display and pause for 50ms rescale coordinates radius 30
% java BouncingBall
31
StdAudio.play("boing.wav"); StdDraw.picture(rx, ry, "earth.gif"); 32
% java DeluxeBouncingBall 33
% java DeluxeBouncingBall