CS 4 Io
CS 4 Io
S E D G E W I C K / W A Y N E
PA R T I : P R O G R A M M I N G I N J AVA
Computer Science
ROBERT SEDGEWICK
1.5 K E V I N WAY N E
http://introcs.cs.princeton.edu
COMPUTER SCIENCE
S E D G E W I C K / W A Y N E
PA R T I : P R O G R A M M I N G I N J AVA
CS.4.A.IO.Standard
Basic building blocks for programming
objects
arrays
Ability to interact with
conditionals and loops the outside world
3
Input and output
Goal: Write Java programs that interact with the outside world via input and output devices.
Typical
INPUT
devices
Keyboard Trackpad Storage Network Camera Microphone
Typical
OUTPUT
devices
Our approach.
• Define input and output abstractions.
• Use operating system (OS) functionality to connect our Java programs to actual devices.
4
Abstraction
plays an essential role in understanding computation.
Interested in thinking more deeply about this concept?
Consider taking a philosophy course.
Good abstractions simplify our view of the world, by unifying diverse real-world artifacts.
This lecture. Abstractions for delivering input to or receiving output from our programs.
5
Quick review
% java DrawCards 10
7♠ 2♥ Q♦ A♠ Q♠ 2♦ Q♥ 6♦ 5♥ 10♦
Output to
standard
output stream
Virtual
VT-100
terminal
6
Input-output abstraction (so far)
Java program
7
Review: command-line input
Basic properties
• Strings you type after the program name are available as args[0], args[1], ... at run time.
• Arguments are available when the program begins execution.
• Need to call system conversion methods to convert the strings to other types of data.
8
Review: standard output
Basic properties
• Strings from System.out.println() are added to the end of the standard output stream.
• Standard output stream is sent to terminal application by default.
command-line
standard input stream arguments
Java program
10
Standard input
Java program
11
StdIn library
Computer Science
• Implement abstractions invented for UNIX in the 1970s.
• Available for download at booksite. Computer
• Included in introcs software you downloaded at the beginning of the course. Science
An Interdisciplinary Approach
ROBERT SEDGEWICK
K E V I N WAY N E
Computer Science
• Implement abstractions invented for UNIX in the 1970s.
• Available for download at booksite. Computer
• Included in introcs software you downloaded at the beginning of the course. Science
An Interdisciplinary Approach
ROBERT SEDGEWICK
K E V I N WAY N E
Q. These are the same as System.out. Why not just use System.out? use StdOut
from now on
A. We provide a consistent set of simple I/O abstractions in one place.
A. We can make output independent of system, language, and locale.
13
StdIn/StdOut warmup
Interactive input
• Prompt user to type inputs on standard input stream.
• Mix input stream with output stream.
14
StdIn application: average the numbers on the standard input stream
Key points
% java Average
• No limit on the size of the input stream. 10.0 5.0 6.0
3.0 7.0 32.0
• Input and output can be interleaved. <Ctrl-d>
10.5
15
Summary: prototypical applications of standard output and standard input
StdOut: Generate a stream of random numbers StdIn: Compute the average of a stream of numbers
16
Redirection: keep data in files on your computer
RandomSeq
Average
Slight problem. Still limited by maximum file size.
17
Piping: entirely avoid saving data
Critical point. No limit within programs on the amount of data they can handle.
It is the job of the system to collect data on standard output and provide it to standard input.
18
Streaming algorithms
Early computing
• Amount of available memory was much smaller than
amount of data to be processed.
• But dramatic increases happened every year.
• Redirection and piping enabled programs to handle
much more data than computers could store.
Modern computing
• Amount of available memory is much smaller than
amount of data to be processed.
• Dramatic increases still happen every year.
• Streaming algorithms enable our programs to handle
much more data than our computers can store.
Image sources
http://www.digitalreins.com/wp-content/uploads/2013/05/Binary-code.jpg
http://en.wikipedia.org/wiki/Punched_tape#mediaviewer/File:Harwell-dekatron-witch-10.jpg
CS.4.A.IO.Standard
COMPUTER SCIENCE
S E D G E W I C K / W A Y N E
PA R T I : P R O G R A M M I N G I N J AVA
CS.4.B.IO.Drawing
Further improvements to our I/O abstraction
command-line
standard input stream arguments
Java program
StdDraw library
Computer Science
standard drawing
• Developed for this course, but broadly useful.
Computer • Available for download at booksite.
Science
• Included in introcs software.
An Interdisciplinary Approach
ROBERT SEDGEWICK
K E V I N WAY N E
22
StdDraw library
void picture(double x, double y, String filename) place .gif, .jpg or .png file
Java program
void setPenRadius(double r)
void setPenColor(Color c)
standard drawing
23
“Hello, World” for StdDraw
(0, 0) (1, 0)
24
“Hello, World” for StdDraw
window for standard drawing virtual terminal for editor
%
% javac Triangle.java
% java Triangle
26
StdDraw application: plotting a function
CS.4.B.IO.Drawing
COMPUTER SCIENCE
S E D G E W I C K / W A Y N E
PA R T I : P R O G R A M M I N G I N J AVA
CS.4.C.IO.Fractals
StdDraw application: a random game
0 1
30
StdDraw application: a random game
StdDraw.setPenRadius(0.01);
double x = 0.0, y = 0.0;
for (int t = 0; t < trials; t++)
{
int r = (int) (Math.random() * 3);
x = (x + cx[r]) / 2.0;
y = (y + cy[r]) / 2.0;
StdDraw.point(x, y);
}
}
}
31
Sierpinski triangles in the wild
1
odd numbers
1 1
1 2 1
1 3 3 1
1 4
6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
Pascal's triangle
32
Iterated function systems
What happens when we change the rules? % java IFS 10000 < coral.txt
% more coral.txt
3
0.40 0.15 0.45
3 3
0.307692 -0.531469 0.8863493
0.307692 -0.076923 0.2166292
0.000000 0.545455 0.0106363
3 3
-0.461538 -0.293706 1.0962865
0.153846 -0.447552 0.3383760
0.692308 -0.195804 0.3808254
33
Iterated function systems
Another example of changing the rules % java IFS 10000 < barnsley.txt
2% 0.5 .27y
% more barnsley.txt
4
.02 .15 .13 .70
4 3
.000 .000 .500
-.139 .263 .570
.170 -.215 .408
.781 .034 .1075
4 3
.000 .270 .000
.246 .224 -.036
.222 .176 .0893
-.032 .739 .270
34
Iterated function systems
35
COMPUTER SCIENCE
S E D G E W I C K / W A Y N E
PA R T I : P R O G R A M M I N G I N J AVA
Image sources
http://paulbourke.net/fractals/gasket/cokegasket.gif
http://www.buzzfeed.com/atmccann/11-awesome-math-foods#39wokfk
http://sheilakh.deviantart.com/art/The-Legend-of-Sierpinski-308953447
http://commons.wikimedia.org/wiki/File:Lady_Fern_frond_-_normal_appearance.jpg
http://img3.wikia.nocookie.net/__cb20100707172110/jamescameronsavatar/images/e/e1/Avatar_concept_art-3.jpg
CS.4.C.IO.Fractals
COMPUTER SCIENCE
S E D G E W I C K / W A Y N E
PA R T I : P R O G R A M M I N G I N J AVA
CS.4.D.IO.Animation
Animation
rx+vx
To create animation with StdDraw.
Repeat the following:
• Clear the screen. ry+vy
ry
• Move the object.
vy
• Draw the object. vx
• Display and pause briefly.
When display time is much greater than the rx
screen-clear time, we have the illusion of motion.
Bouncing ball.
• Ball has position (rx, ry) and constant velocity (vx, vy).
• To move the ball, update position to (rx+vx, ry+vy).
• If the ball hits a vertical wall, set vx to -vx.
• If the ball hits a horizontal wall, set vy to -vy.
38
Bouncing ball
% java BouncingBall
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)
{
StdDraw.setPenColor(StdDraw.LIGHT_GRAY);
StdDraw.filledSquare(0.0, 0.0, 1.0);
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.BLACK);
StdDraw.filledCircle(rx, ry, radius);
StdDraw.show(20);
}
}
}
39
Pop quiz on animation
40
Pop quiz on animation
% java BouncingBallDeluxe
public class BouncingBallDeluxe
{
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)
{
StdDraw.setPenColor(StdDraw. LIGHT_GRAY);
StdDraw.filledSquare(0.0, 0.0, 1.0);
if (Math.abs(rx + vx) + radius > 1.0)
{ StdAudio.play("pipebang.wav"); vx = -vx; }
if (Math.abs(ry + vy) + radius > 1.0)
{ StdAudio.play("pipebang.wav"); vy = -vy; }
rx = rx + vx;
ry = ry + vy;
StdDraw.picture(rx, ry, "TennisBall.png");
StdDraw.show(20);
}
}
} Stay tuned to next lecture for full description of StdAudio.
42
A set of I/O abstractions for Java
Computer Science
Developed for this course, but broadly useful
• StdIn, StdOut, StdDraw, and StdAudio.
Computer
• Available for download at booksite (and included in introcs software). Science
An Interdisciplinary Approach
ROBERT SEDGEWICK
K E V I N WAY N E
command-line
standard input stream arguments
Java program
standard drawing
standard audio
43
COMPUTER SCIENCE
S E D G E W I C K / W A Y N E
PA R T I : P R O G R A M M I N G I N J AVA
CS.4.D.IO.Animation
COMPUTER SCIENCE
S E D G E W I C K / W A Y N E
PA R T I : P R O G R A M M I N G I N J AVA
Computer Science
ROBERT SEDGEWICK
1.5 K E V I N WAY N E
http://introcs.cs.princeton.edu