260 likes | 398 Views
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 ()
E N D
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() • You feel comfortable with them and can output nicely formatted ints, doubles, booleans, chars, and Strings with them
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
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
By now, you know StdIn • But, a refresher never hurts
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
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
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
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
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
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
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)
Line and point methods • The following methods can be used to draw lines and points
Line example • Let’s draw a box then divide it into two halves, like so:
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);
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
Shape methods • Here are some methods for drawing circles and squares and setting the color for doing so:
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
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
Controls • A number of methods are given to give us more control over the display
Examples StdDraw.setXscale (0.0,100.0); StdDraw.setYscale(0.0,100.0); StdDraw.setPenRadius(0.01); 2 times the default 0.005
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
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
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
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
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]