1.5 Input and Output

Download Report

Transcript 1.5 Input and Output

Announcement About HW-2

Deadline Extended – 11:55 PM
Thursday

5% Extra Credit if you meet the
original deadline

See the e-mail from the TAs for the
details
1.5 Input and Output
Input and Output (I/O)
Input devices.
Keyboard
Mouse
Hard drive
Network
Digital camera
Speakers
Hard drive
Network
Printer
Microphone
Output devices.
Display
MP3 Player
Goal. Java programs that interact with the outside world.
3
I/O Hardware/Software Architecture
Application Software
Operating System (Kernel)
4
Input and Output in Java
Input devices.
Keyboard
Mouse
Hard drive
Network
Digital camera
Speakers
Hard drive
Network
Printer
Microphone
Output devices.
Display
MP3 Player
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.
Programs can manipulate arbitrary amounts of data.



5
Search Engines


1 trillion webpages
Perform
same/similar
processing over all
the data
The World Wide Web in 2003.
Image source: http://www.opte.org/
6
Command-line Input vs. Standard Input
Command line inputs.
Use command line inputs to read in a few user values.
Not practical for many user inputs.
Input entered before program begins execution.



Standard input (stdin.java)
Flexible OS abstraction for input.
By default, stdin is received from Terminal window.
Input entered while program is executing.



7
I/O in Java Programs
8
Reading Data from a File
Can use the same interface to read a file
(Interface here means same methods.)
Issues:
Need to “connect to” or “open” the data file.
There’s just one standard input, but we could read from multiple files.
Princeton I/O Libraries – stdlib.jar
Dr. Java Demo
Java’s standard libraries (e.g. Math, System, etc.)
Automatically included when you build a Java program
Other libraries (your’s, our’s, Princeton’s):
•You must add them to your project, folder, etc. using your IDE.
•These can be in source files, e.g. StdIn.java
•Or, in an archive file called a Jar file
We provide a Jar file with all the Princeton libraries: stdlib.jar
In DrJava: from Preference menu, choose Resource Locations
Then click Add by Extra Classpath and find and select stdlib.jar
Reading File Data using the ‘In’ Library
In: Princeton provided library for reading file data.
Use In in a very similar way to StdIn -- but you must:


declare a new In object and create it by opening a file.
use your new In object by name instead of StdIn.
In fileIn = new In(“mydatafile.txt”);
double[] data = new double[100];
int i = 0;
while ( ! fileIn.isEmpty() ) data[i++] = fileIn.readDouble();
This opens a file for reading. Looks for mydatafile.txt in the current
working directory where your program is running.
2.Then reads double after double into the array. Stops when there are
no more values in the file to be read.
1.
DR. JAVA CODE EXAMPLE – readFile.java
11
Standard Drawing
StdDraw.java
(Part of stdlib.jar)
Data Visualization
Plot filter. Read in a sequence of (x, y) coordinates from standard
input, and plot using standard drawing.
bounding box
669905.0 247205.0 1244962.0 490000.0
1097038.8890 245552.7780
1103961.1110 247133.3330
1104677.7780 247205.5560
...
coordinates of
13,509 US cities
usa.txt
13
Plot Filter
Dr. Java Demo
669905.0 247205.0 1244962.0 490000.0
1097038.8890 245552.7780
1103961.1110 247133.3330
...
usa.txt
public class PlotFilter {
public static void main(String[] args) {
In fileIn = new In("USA.txt");
double xmin = fileIn.readDouble();
double ymin = fileIn.readDouble();
double xmax = fileIn.readDouble();
double ymax = fileIn.readDouble();
StdDraw.setXscale(xmin, xmax);
StdDraw.setYscale(ymin, ymax);
Change Coordinates
from (0,0), (1,1)
square
while (!fileIn.isEmpty()) {
double x = fileIn.readDouble();
double y = fileIn.readDouble();
StdDraw.point(x, y);
}
}
}
14
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.


(+1, +1)
(vx, vy)
(rx, ry)
Wall
(-1, -1)
15
Bouncing Ball
public class BouncingBall {
public static void main(String[] args) {
double rx = .480, ry = .860;
position
double vx = .015, vy = .023;
constant velocity
radius
double radius = .05;
StdDraw.setXscale(-1.0, +1.0);
StdDraw.setYscale(-1.0, +1.0);
establish “walls”
while(true) {
if (Math.abs(rx + vx) > 1.0) vx = -vx;
if (Math.abs(ry + vy) > 1.0) vy = -vy;
rx = rx + vx;
ry = ry + vy;
update position
StdDraw.clear(StdDraw.WHITE);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.filledCircle(rx, ry, radius);
StdDraw.show(50);
}
}
}
bounce
clear background
draw the ball
turn on animation mode:
display and pause for 50ms
16
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:

StdDraw.picture(rx, ry, "earth.gif");

Add following code upon collision with wall:
StdAudio.play("boing.wav");
17
Computer Animation
Computer animation. Display a sequence
of closely related images in rapid succession
to produce the illusion of movement.
Frame rate. Use 15-70 frames per second
to "trick" human eye and brain into seeing
smooth motion.
Ex 1. Television and motion pictures.
Ex 2. Java mascot Duke cart-wheeling.
1
10
2
11
3
12
4
13
5
14
6
15
7
16
8
17
9
http://java.sun.com/docs/books/tutorial
18
Java Implementation
public class Duke {
public static void main(String[] args) {
int images = 17;
int WIDTH = 130, HEIGHT = 80;
StdDraw.setCanvasSize(WIDTH, HEIGHT);
for (int t = 0; true; t++) {
int i = 1 + (t % images);
String file = "T" + i + ".gif";
StdDraw.picture(0.5, 0.5, file);
StdDraw.show(100);
T1.gif }
T17.gif
}
}
19
Chaos Game
Chaos game. Play on equilateral triangle, with vertices R, G, B.
Start at R.
Repeat the following N times:
– pick a random vertex
– move halfway between current point and vertex
– draw a point in color of vertex
B: (½, ½3)


Q. What picture emerges?
2
B B G R B G …
5
1
3
6
4
0
R: (0, 0)
G: (1, 0)
20
Chaos Game
public class Chaos {
public static void main(String[] args) {
int T = Integer.parseInt(args[0]);
double[] cx = { 0.000, 1.000, 0.500 };
double[] cy = { 0.000, 0.000, 0.866 };
½3
(avoid hardwired
constants like this)
double x = 0.0, y = 0.0;
for (int t = 0; t < T; t++) {
int r = (int) (Math.random() * 3);
x = (x + cx[r]) / 2.0;
y = (y + cy[r]) / 2.0;
between 0 and 2
StdDraw.point(x, y);
}
}
}
21
Chaos Game
Easy modification. Color point according to random vertex chosen using
StdDraw.setPenColor(StdDraw.RED) to change the pen color.
B
% java Chaos 10000
R
Sierpinski triangle
G
22
Barnsley Fern
Barnsley fern. Play chaos game with different rules.
probability
new x
new y
2%
.50
15%
-.14x + .26y + .57
.25x + .22y - .04
13%
.17x - .21y + .41
.22x + .18y + .09
70%
.78x + .03y + .11
-.03x + .74y + .27
.27y
Q. What does computation tell us about nature?
Q. What does nature tell us about computation?
20th century sciences. Formulas.
21st century sciences. Algorithms?
23