Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 7: Input and Output 1.  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()

Similar presentations


Presentation on theme: "Week 7: Input and Output 1.  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()"— Presentation transcript:

1 Week 7: Input and Output 1

2  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 int s, double s, boolean s, char s, and String s with them 2

3  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 3

4  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 4

5  But, a refresher never hurts MethodUse boolean isEmpty() Tells if there are more values int readInt() Read in the next int double readDouble() Read in the next double boolean readBoolean() Read in the next boolean char readChar() Read in the next char String readString() Read in the next String String readLine() Read in a whole line String readAll() Read in all remaining input 5

6  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 6

7  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 out.txt 7

8  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 8

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

10  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 10

11  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 11

12  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,0) (0,1)(1,1) (1,0) 12

13  The following methods can be used to draw lines and points MethodUse void line(double x0, double y0, double x1, double y1) Draw a line from (x0,y0) to (x1,y1) void point(double x, double y) Draw a point at (x,y) 13

14  Let’s draw a box then divide it into two halves, like so: 14

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

16  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 16

17  Here are some methods for drawing circles and squares and setting the color for doing so: MethodUse void circle(double x, double y, double r) Draw a circle centered at (x,y) with radius r void filledCircle(double x, double y, double r) Draw a filled circle centered at (x,y) with radius r void square(double x, double y, double r) Draw a square centered at (x,y) with edges 2r void filledSquare(double x, double y, double r) Draw a filled square centered at (x,y) with edges 2r void setPenColor(Color c) Start drawing with color c 17

18  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 BLACKBLUECYANDARK_GRAYGRAY GREENLIGHT_GRAYMAGENTAORANGEPINK REDWHITEYELLOW 18

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

20  A number of methods are given to give us more control over the display MethodUse void setXscale(double x0, double x1) Set the x scale void setYscale(double y0, double y1) Set the y scale void setPenRadius(double r) Set the pen radius void clear() Clear canvas to white void clear(Color c) Clear canvas to color c void show(int delay) Delay for delay ms void show() Turn off delay mode 20

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

22  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 22

23  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 23

24  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 24

25 1. Ask the user for the coefficients of the four terms (ax 3 + bx 2 + cx + d) 2. Ask the user for an x range 3. Run through the function and find the minimum and maximum y values hit 4. Rescale the drawing area to show everything 5. Plot the function 25

26  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] 26


Download ppt "Week 7: Input and Output 1.  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()"

Similar presentations


Ads by Google