Java Input and Output: Interacting with Devices and Libraries, Slides of Computer Programming

An excerpt from 'introduction to programming in java: an interdisciplinary approach' by robert sedgewick and kevin wayne. It discusses the importance of input and output in java programming, the use of java libraries and operating system connections for input and output, and various methods for handling standard input and output. Examples include command-line arguments, libraries stdin and stdout, and redirection and piping.

Typology: Slides

2011/2012

Uploaded on 07/15/2012

sajeev
sajeev 🇮🇳

3.5

(2)

48 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1.5 Input and Output
Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · February 19, 2009 7:44 AM
2
Input and Output
Input devices.
Output devices.
Goal. Java programs that interact with the outside world.
Display Speakers MP3 Player
Printer
MouseKeyboard Digital camera Microphone
Hard drive Network
Hard drive Network
3
Input and Output
Input devices.
Output devices.
Our approach.
!Define Java libraries of functions for input and output.
!Use operating system (OS) to connect Java programs to:
file system, each other, keyboard, mouse, display, speakers.
Display Speakers MP3 Player
Printer
MouseKeyboard Digital camera Microphone
Hard drive Network
Hard drive Network
4
Terminal. Application where you can type commands to control the
operating system.
Terminal
Mac OS X Microsoft Windows
docsity.com
pf3
pf4
pf5
pf8

Partial preview of the text

Download Java Input and Output: Interacting with Devices and Libraries and more Slides Computer Programming in PDF only on Docsity!

1.5 Input and Output

Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · February 19, 2009 7:44 AM 2

Input and Output

Input devices.

Output devices.

Goal. Java programs that interact with the outside world.

Display (^) Speakers Printer MP3 Player Keyboard Mouse Hard drive Network Digital camera Microphone Hard drive Network 3

Input and Output

Input devices.

Output devices.

Our approach.

! Define Java libraries of functions for input and output.

! Use operating system (OS) to connect Java programs to:

file system, each other, keyboard, mouse, display, speakers.

Display (^) Speakers Printer MP3 Player Keyboard Mouse Hard drive Network Digital camera Microphone Hard drive Network 4

Terminal. Application where you can type commands to control the

operating system.

Terminal

Mac OS X Microsoft Windows

5

Command-Line Input and Standard Output

Command-line input. Read an integer N as command-line argument.

Standard output.

! Flexible OS abstraction for output.

! In Java, output from System.out.println() goes to standard output.

! By default, standard output is sent to Terminal.

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

Old Bird's Eye View

7

New Bird's Eye View

Standard Input and Output

Redirection and Piping

14 % java RandomSeq 1000 > data.txt redirect stdout

Redirecting Standard Output

Redirecting standard output. Use OS directive to send standard

output to a file for permanent storage (instead of terminal window).

15 % more < data.txt

… % java Average < data.txt

redirect stdin

Redirecting Standard Input

Redirecting standard input. Use OS directive to read standard input

from a file (instead of terminal window).

16

Connecting Programs

Piping. Use OS directive to make the standard output of one program

become the standard input of another.

% java RandomSeq 1000000 | java Average

% java RandomSeq 1000000 | java Average

Standard Drawing

18

Standard Draw

Standard drawing. We provide library StdDraw to plot graphics.

To use. Download StdDraw.java and put in working directory.

(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

Data Visualization

Plot filter. Read in a sequence of (x, y) coordinates from standard

input, and plot using standard drawing.

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

Data Visualization

% 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

Barnsley Fern

Barnsley fern. Play chaos game with different rules.

Q. What does computation tell us about nature?

Q. What does nature tell us about computation?

20 th^ century sciences. Formulas.

21 st^ century sciences. Algorithms?

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

Animation

Animation loop. Repeat the following:

! Clear the screen.

! Move the object.

! Draw the object.

! Display and pause for a short while.

Ex. Bouncing ball.

! Ball has position (rx, ry) and constant velocity (vx, vy).

! Detect collision with wall and reverse velocity.

(rx, ry) (vx, vy) (-1, -1) (+1, +1) 29

Bouncing Ball

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

Bouncing Ball Demo

% java BouncingBall

31

Special Effects

Images. Put .gif, .png, or .jpg file in the working directory and

use StdDraw.picture() to draw it.

Sound effects. Put .wav, .mid, or .au file in the working directory and

use StdAudio.play() to play it.

Ex. Modify BouncingBall to display image and play sound upon collision.

! Replace StdDraw.filledCircle() with:

! Add following code upon collision with wall:

StdAudio.play("boing.wav"); StdDraw.picture(rx, ry, "earth.gif"); 32

Deluxe Bouncing Ball Demo

% java DeluxeBouncingBall 33

Bouncing Ball Challenge

Q. What happens if you call StdDraw.filledSquare() once before loop

(instead of inside)?

% java DeluxeBouncingBall