1 / 26

CS 177

Week 7: Input and Output. CS 177. Everyone loves System.out.println (). Now we are going to talk a little bit about output You have a lot of experience with System.out.println () and System.out.print ()

tameka
Download Presentation

CS 177

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. Week 7: Input and Output CS 177

  2. Everyone loves System.out.println() • Now we are going to talk a little bit about output • You have a lot of experience with System.out.println() and System.out.print() • You feel comfortable with them and can output nicely formatted ints, doubles, booleans, chars, and Strings with them

  3. What about files? • Printing things to the screen and the DrJava console is all well and good • Sometimes you need to store something for later • Sometimes you get tired of typing the same thing over and over again • For these and other reasons, we put things into more permanent storage: files

  4. Output redirection • When we run a Java program from the command line, we can send all the output it makes to a file, instead of to the screen • To do this, you use the > operator to redirect the output to the file name you choose • The following command runs program Test and sends its output to out.txt • The only problem is that you can’t do this easily inside DrJava $ java Test > out.txt

  5. By now, you know StdIn • But, a refresher never hurts

  6. We can use StdIn to read from files too • Just as we can write data to a file, we can read data from a file, using the < operator • The program “pretends” that a user is typing in the data that it gets from the file • Now, the StdIn methods like isEmpty()and readAll() should make more sense • The following command runs program Test and reads in input from in.txt $ java Test < in.txt

  7. Input and Output • It is possible to redirect both input and output • For example, the following command would run program Test, reading in values from in.txt and printing out information to out.txt $ java Test < in.txt > out.txt

  8. My in is your out • It’s possible to send the output of one program into another program as the input • Sure, you could: • Run the first program • Redirect its output to a file • Run the second program • Read its input from that same file • Why not cut out the middleman? • To pipe data from one program to the next, you use the | operator

  9. Pipe example • The following command shows how you can send the output of a program called Writer into the input of a program called Reader: • Use of output redirection (>), input redirection (<), and piping (|) are not Java commands • These are a feature of the OS and can be used with languages other than Java $ java Writer | java Reader

  10. Drawing on the screen • Files are useful • But not too mind-blowing • We are going to talk about the StdDraw library given by the textbook • StdDraw gives us a canvas we can draw all kinds of things: • Points and lines • Circles, squares, and polygons • Plots of functions • Colors

  11. StdDraw • StdDraw is a library of Java code developed by the textbook authors, just like StdIn • StdDraw allows you to draw output on the screen easily • You can draw points, lines, and polygons in various colors • You can clear and resize the drawing area and even save the results • StdDraw is not standard Java that everyone uses, but it’s a nice tool for graphics

  12. Lines and points • The simplest things you can draw with StdDraw are lines and points • The first thing you should be aware of is that the canvas is drawn like Quadrant I of a Cartesian plane (0,1) (1,1) (0,0) (1,0)

  13. Line and point methods • The following methods can be used to draw lines and points

  14. Line example • Let’s draw a box then divide it into two halves, like so:

  15. Line example StdDraw.line (0.0,0.0,1.0,0.0); StdDraw.line(1.0,0.0,1.0,1.0); StdDraw.line(1.0,1.0,0.0,1.0); StdDraw.line (0.0,1.0,0.0,0.0); StdDraw.line (0.5,0.0,0.5,1.0); StdDraw.square(0.5,0.5,0.5);

  16. Who wants to take all that time to make a square? • There are built in commands for drawing: • Circles • Squares • Arbitrary polygons • Filled versions of each one of these • We won’t bother with the arbitrary polygons, but the book shows how to use them • It is also possible to set the color

  17. Shape methods • Here are some methods for drawing circles and squares and setting the color for doing so:

  18. Colors • Eventually you will be able to define your own colors • For now you are limited to 13 presets • For example, to make something magenta, you would use the value StdDraw.MAGENTA

  19. Screen saver? • Make 100 circles at random locations with random sizes and random colors • Location is easy • Size is easy, we just decide on the range of sizes we want and do some math • Color is more painful • We need a switch statement with 13 choices

  20. Controls • A number of methods are given to give us more control over the display

  21. Examples StdDraw.setXscale (0.0,100.0); StdDraw.setYscale(0.0,100.0); StdDraw.setPenRadius(0.01); 2 times the default 0.005

  22. Scales • As you have seen, the default scale of the canvas is in the range [0,1] for both x and y • We can use the setXscale() method to set the minimum and maximum x values • We can use the setYscale()method to set the minimum and maximum y values • Useful for plotting functions

  23. What is this show() thing? • The show() method lets you specify a delay in milliseconds before things are drawn on the screen • You can use it to slow down or speed up animations

  24. Plotting functions • Plotting functions is really useful • Getting smooth curves is hard • Instead, we just pick a whole bunch of x points and figure out the function value • We can just draw dots to plot those values • We can connect them with lines for a more connected look

  25. Function plotting algorithm • Ask the user for the coefficients of the four terms (ax3 + bx2 + cx + d) • Ask the user for an x range • Run through the function and find the minimum and maximum y values hit • Rescale the drawing area to show everything • Plot the function

  26. Cannon simulator • Can we simulate a cannon being fired? • Let the user enter an initial velocity in m/s • Let the user an angle between 0° and 90° • Assume each iteration takes 1/10 of a second • Assume an initial height of 20 m • We draw the path of the cannon ball as it flies through the air • Let’s also set the x and y scales to both be [0,100]

More Related