1 / 23

Announcement About HW-2

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 devices. Output devices. Goal. Java programs that interact with the outside world.

calvin
Download Presentation

Announcement About HW-2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. Input devices. Output devices. Goal. Java programs that interact with the outside world. Input and Output (I/O) Hard drive Network Digital camera Microphone Keyboard Mouse Display Network Hard drive Speakers MP3 Player Printer

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

  5. 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. Programs can manipulate arbitrary amounts of data. Input and Output in Java Hard drive Network Digital camera Microphone Keyboard Mouse Display Network Hard drive Speakers MP3 Player Printer

  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/

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

  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.jarDr. 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: • 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. • 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

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

  13. 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 of13,509 US cities usa.txt

  14. Plot FilterDr. 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); while (!fileIn.isEmpty()) { double x = fileIn.readDouble(); double y = fileIn.readDouble(); StdDraw.point(x, y); } } } Change Coordinates from (0,0), (1,1) square

  15. 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)

  16. Bouncing Ball publicclass BouncingBall { publicstaticvoid 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); } } } position constant velocity radius establish “walls” bounce update position clear background draw the ball turn on animation mode:display and pause for 50ms

  17. Special Effects Images. Put .gif, .png, or .jpg file in the working directory anduse 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: StdDraw.picture(rx, ry, "earth.gif"); StdAudio.play("boing.wav");

  18. 2 10 3 11 4 12 13 5 14 1 6 15 7 16 17 8 9 Computer Animation Computer animation. Display a sequenceof closely related images in rapid successionto produce the illusion of movement. Frame rate. Use 15-70 frames per secondto "trick" human eye and brain into seeingsmooth motion. Ex 1. Television and motion pictures. Ex 2. Java mascot Duke cart-wheeling. http://java.sun.com/docs/books/tutorial

  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. 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 Q. What picture emerges? B: (½, ½3) 2 B B G R B G … 5 1 3 6 4 0 G: (1, 0) R: (0, 0)

  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 hardwiredconstants like this) between 0 and 2

  22. 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 G Sierpinski triangle

  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? 20th century sciences. Formulas. 21st century sciences. Algorithms? probability new x new y 2% .50 .27y 15% -.14x + .26y + .57 .25x + .22y - .04 13% .17x - .21y + .41 .22x + .18y + .09 70% .78x + .03y + .11 -.03x + .74y + .27

More Related