Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 1.5 Input and Output

3 3 Input and Output (I/O) Input devices. Output devices. Goal. Java programs that interact with the outside world. Display Speakers MP3 Player Printer MouseKeyboard Digital camera Microphone Hard driveNetwork Hard drive Network

4 4 I/O Hardware/Software Architecture Application Software Operating System (Kernel)

5 5 Input and Output in Java Input devices. Output devices. Our approach. n Define Java libraries of functions for input and output. n Use operating system (OS) to connect Java programs to: file system, each other, keyboard, mouse, display, speakers. n Programs can manipulate arbitrary amounts of data. Display Speakers MP3 Player Printer MouseKeyboard Digital camera Microphone Hard driveNetwork Hard drive Network

6 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

7 7 Command-line Input vs. Standard Input Command line inputs. n Use command line inputs to read in a few user values. n Not practical for many user inputs. n Input entered before program begins execution. Standard input (stdin.java) n Flexible OS abstraction for input. By default, stdin is received from Terminal window. n Input entered while program is executing.

8 8 I/O in Java Programs

9 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.

10 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

11 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: n declare a new In object and create it by opening a file. n 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(); 1. 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. DR. JAVA CODE EXAMPLE – readFile.java 11

12 Standard Drawing StdDraw.java (Part of stdlib.jar)

13 Plot filter. Read in a sequence of (x, y) coordinates from standard input, and plot using standard drawing. 13 Data Visualization 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 bounding box usa.txt

14 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); while (!fileIn.isEmpty()) { double x = fileIn.readDouble(); double y = fileIn.readDouble(); StdDraw.point(x, y); } } } 14 Plot Filter Dr. Java Demo 669905.0 247205.0 1244962.0 490000.0 1097038.8890 245552.7780 1103961.1110 247133.3330... usa.txt Change Coordinates from (0,0), (1,1) square

15 15 Animation Animation loop. Repeat the following: n Clear the screen. n Move the object. n Draw the object. n Display and pause for a short while. Ex. Bouncing ball. Ball has position (rx, ry) and constant velocity (vx, vy). n Detect collision with wall and reverse velocity. (rx, ry) (vx, vy) (-1, -1) (+1, +1) Wall

16 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) > 1.0) vx = -vx; if (Math.abs(ry + vy) > 1.0) vy = -vy; rx = rx + vx; ry = ry + vy; StdDraw.clear(StdDraw.WHITE); StdDraw.setPenColor(StdDraw.BLACK); StdDraw.filledCircle(rx, ry, radius); StdDraw.show(50); } } } 16 Bouncing Ball bounce position constant velocity update position clear background draw the ball turn on animation mode: display and pause for 50ms establish “walls” radius

17 17 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: n Add following code upon collision with wall: StdAudio.play("boing.wav"); StdDraw.picture(rx, ry, "earth.gif");

18 18 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. Computer Animation 1 3 2 4 6 5 7 9 8 10 12 11 13 15 14 16 17 http://java.sun.com/docs/books/tutorial

19 19 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

20 20 Chaos Game Chaos game. Play on equilateral triangle, with vertices R, G, B. n 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 Q. What picture emerges? R: (0, 0) G: ( 1, 0) B: ( ½, ½  3) 0 1 2 3 4 5 6 B B G R B G …

21 21 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 }; 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; StdDraw.point(x, y); } ½  3 (avoid hardwired constants like this) between 0 and 2

22 22 Chaos Game Easy modification. Color point according to random vertex chosen using StdDraw.setPenColor(StdDraw.RED) to change the pen color. R G B Sierpinski triangle % java Chaos 10000

23 23 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 probabilitynew xnew y 15% -.14x +.26y +.57.25x +.22y -.04 13%.17x -.21y +.41.22x +.18y +.09 70%.78x +.03y +.11-.03x +.74y +.27


Download ppt "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."

Similar presentations


Ads by Google