1 / 24

CS177 RECITATION WEEK 7

CS177 RECITATION WEEK 7. Input and Output (Text & Graphical). ANNOUNCEMENTS. Project 3 Milestone due Thursday, October 22 by 9:00 pm Project3 due Thursday, October 29 by 9:00 pm. Exam 1 Questions. The answer is B. Exam 1 Questions. The answer is D. Exam 1 Questions.

arnie
Download Presentation

CS177 RECITATION WEEK 7

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. CS177 RECITATION WEEK 7 Input and Output (Text & Graphical)

  2. ANNOUNCEMENTS • Project 3 Milestone due • Thursday, October 22 by 9:00 pm • Project3 due • Thursday, October 29 by 9:00 pm

  3. Exam 1 Questions The answer is B

  4. Exam 1 Questions The answer is D

  5. Exam 1 Questions The answer is A

  6. Exam 1 Questions Do you remember pre-increment from recitations? First increment and then add. Added j values: 1, 3, 5, 7, 9 and 11 The answer is C

  7. QUESTIONS?

  8. OUTPUT • We have been using • System.out.print() • System.out.println() to print to the console window • Things printed on the console are lost when we exit the program • We can keep the output of our program after program termination using redirection to a file with ‘>’

  9. OUTPUT REDIRECTION EXAMPLE Task: Generate 100 random numbers and print them to a file random.txt: public class RandomSeq { public static void main( String [] args ) { for ( int i = 0; i < 100; i++ ) { System.out.println( Math.random() ); } } } Run the program using the command: java RandomSeq > random.txt

  10. INPUT • We have been using methods of the StdIn library to get input from the console • When performing tasks with many inputs, we would not like to type them one by one on the console  need to get input from file • Then we use input redirection from file with ‘<‘

  11. INPUT REDIRECTION EXAMPLE Task: Read 100 numbers from the file numbers.txt and print their sum public class Sum{ public static void main(String [] args) { double sum = 0; for ( int i = 0; i < 100; i++ ) { double value = StdIn.readDouble(); sum += value; } System.out.println( “Sum is ” + sum ); } } Run the program using the command: java Sum < numbers.txt

  12. PIPING • The output from one program can be the input to another program using piping • To send the output of program Writer to the program Reader use the following command: • java Writer | java Reader • Example: • To print the sum of 100 random numbers use: • java RandomSeq | java Sum • instead of the two commands: • java RandomSeq > numbers.txt • java Sum < numbers.txt

  13. GRAPHICAL OUTPUT • We focused on text output until now • The real world is full of graphics: • Imagine having games with text-based interface? Or websites? • System.out.println (print)  text output • StdDraw methods  graphical output

  14. StdDraw • Imagine as a device capable of drawing shapes on a two-dimensional canvas • Implemented by authors of the book: • Need to download StdDraw.java in the directory of your program (just as we did for StdIn) • Use methods to draw points, lines, rectangles, circles… as well as format the drawing area (set color, resize etc.) • !!!Always use StdDraw.x whether x is the method name, color name, etc.

  15. BASIC DRAWING: LINES & POINTS !!! Canvas we are drawing on is like Quadrant I of Cartesian plane

  16. LINES & POINTS . StdDraw.line( 0, 0, 1, 1 ); x0 y0 x1 y1 StdDraw.point(0.5, 0.5)

  17. SHAPE METHODS: Q: How can we draw a square? Idea: Draw four lines using StdDraw.line Better idea: Use StdDraw.square Methods for drawing shapes:

  18. COLORS • You can draw shapes of different colors using StdDraw.COLOR_NAME where available pen colors are • Need to set color before drawing, • i.e. to draw a pink line use • StdDraw.setPenColor(StdDraw.PINK); • StdDraw.line(0,0,1,1); • instead of • StdDraw.line(0,0,1,1); • StdDraw.setPenColor(StdDraw.PINK);

  19. CONTROLLING THE DRAW AREA StdDraw also has methods to provide more control over the display

  20. Let’s mark cities having population > 500 on the US map: public class PlotFilter { public static void main(String[] args) { double x0 = StdIn.readDouble(); double y0 = StdIn.readDouble(); double x1 = StdIn.readDouble(); double y1 = StdIn.readDouble(); StdDraw.setXscale(x0, x1); StdDraw.setYscale(y0, y1); while (!StdIn.isEmpty()) { double x = StdIn.readDouble(); double y = StdIn.readDouble(); StdDraw.point(x, y); } } }

  21. SHAPE EXAMPLES public class Shapes { public static void main( String args[] ) { StdDraw.setPenColor( StdDraw.RED); StdDraw.square(.2, .8, .1); StdDraw.filledSquare(.8, .8, .2); StdDraw.setPenColor( StdDraw.BLACK); StdDraw.circle(.8, .2, .2); double[] xd = {.1, .2, .3, .2}; double[] yd = {.2, .3, .2, .1}; StdDraw.filledPolygon(xd, yd); } }

  22. Let’s simulate the movement of a bouncing ball in the unit box: public class BouncingBall { public static void main(String[] args) { StdDraw.setXscale(-1.0, 1.0); StdDraw.setYscale(-1.0, 1.0); double rx = 0.480, ry = 0.860; // position double vx = 0.015, vy = 0.023; //velocity double radius = 0.05; while (true) {// main animation loop // bounce off wall according to law of elastic collision if (Math.abs(rx + vx) > 1.0 - radius) vx = -vx; if (Math.abs(ry + vy) > 1.0 - radius) vy = -vy; rx = rx + vx; //update position ry = ry + vy; StdDraw.setPenColor(StdDraw.GRAY); // clear the background StdDraw.filledSquare(0, 0, 1.0); StdDraw.setPenColor(StdDraw.BLACK); StdDraw.filledCircle(rx, ry, radius); // draw ball on the screen StdDraw.show(20); // display and pause for 20 ms } } } ANIMATIONS set the scale of the coordinate system

  23. PLOTTING FUNCTION GRAPHS public class FunctionGraph { public static void main(String[] args) { int N = Integer.parseInt(args[0]); double[] x = new double[N+1]; double[] y = new double[N+1]; for (int i = 0; i <= N; i++) { x[i] = Math.PI * i / N; y[i] = Math.sin(4*x[i]) + Math.sin(20*x[i]); } StdDraw.setXscale(0, Math.PI); StdDraw.setYscale(-2.0, +2.0); for (int i = 0; i < N; i++) { StdDraw.line(x[i], y[i], x[i+1], y[i+1]); } } } the function y = sin(4x) + sin(20x), sampled at N points between x = 0 and x = pi

  24. FINAL QUESTIONS?

More Related