CS177 RECITATION WEEK 7 Input and Output (Text & Graphical)

Slides:



Advertisements
Similar presentations
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Advertisements

Computer Science A 11: 24/3. Animation - Dubble buffering - Transformations - Sprites and textures - Sound.
Week 8 - Monday.  What did we talk about last time?  StdAudio.
Emerging Platform#5: Processing 2 B. Ramamurthy 6/13/2014B. Ramamurthy, CS6511.
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()
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Announcement About HW-2 Deadline Extended – 11:55 PM Thursday 5% Extra Credit if you meet the original deadline See the from the TAs for the details.
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
CS 177 Recitation Week 8 – Methods. Questions? Announcements  Project 3 milestone due next Thursday 10/22 9pm  Turn in with: turnin –c cs177=xxxx –p.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
Subdue Graph Visualizer by Gayathri Sampath, M.S. (CSE) University of Texas at Arlington.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
A tour around Java General introduction to aspects of the language (these will be covered in more detail later) After this tour you should have a general.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
By mr Hanley 10/31/2014.  StdDraw is a class developed by Princeton University to help novice java programmers interact with a JFrame’s  A computer.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 27: More on ArrayList, Reference Semantics Command Line Arguments reading:
Game Design Creating a game called PING Phase 3: Steps for building basic game.
Chapter 6: Iteration Part 2. Create triangle pattern [] [][] [][][] [][][][] Loop through rows for (int i = 1; i
1.5 Input and Output Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · October.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
The Rectangle Method. Introduction Definite integral (High School material): A definite integral a ∫ b f(x) dx is the integral of a function f(x) with.
Introduction to Computation and Problem Solving Class 9: Static Methods and Data Members Prof. Steven R. Lerman and Dr. V. Judson Harward.
Simulation Time-stepping and Monte Carlo Methods Random Number Generation Shirley Moore CS 1401 Spring 2013 March 26, 2013.
Chapter 2: Java Fundamentals
COMP 110: Spring Announcements Quiz Wednesday No Class Friday Assignment 5 Due next Monday Q&A Review Session Monday.
2.1 Functions Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · 9/10/08 9/10/08.
CS 112 Introduction to Programming Graphics; Animation Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Loops & Graphics IP 10 Mr. Mellesmoen Recall Earlier we wrote a program listing numbers from 1 – 24 i=1 start: TextWindow.WriteLine(i) i=i+1 If.
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
Chapter 4 Practice cont.. Practice with nested loops 1.What will be the output of the following program segment: 1.for (int i = 1; i
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
Creating visual interfaces in python
File Input and Graphics An extract of slides from set 05_inout.ppt Designed to get you ready for the HW3.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Week 11 - Friday.  What did we talk about last time?  Object methods  Accessors  Mutators  Constructors  Defining classes.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Week 10 - Friday.  What did we talk about last time?  References and primitive types  Started review.
Special Methods in Java. Mathematical methods The Math library is extensive, has many methods that you can call to aid you in your programming. Math.pow(double.
CS001 Introduction to Programming Day 6 Sujana Jyothi
Simple Console Output. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CS 112 Introduction to Programming Loop Examples; Variable Scoping; Nested Loops; Yang (Richard) Yang Computer Science Department Yale University 208A.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
Processing == Java + Extra Utilities Processing Adds: – Drawing functions – Text and font manipulations – Image and video – 3D transformations – Keyboard.
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
User Input ICS2O.
Contacts: Intro to game programming in Java (with almost-drawingpanel)
Example: Card Game Create a class called “Card”
Week 8 - Monday CS 121.
Week 15 – Wednesday CS 121.
Functions that Deal with Arrays CS 1111 Ryan Layer Feb 23, 2010
CS 177 Week 15 Recitation Slides
Week 9 – Audio Processing
Lecture 7: Introduction to Processing
Building Java Programs
The keyboard is the standard input device.
1.5 Input and Output Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 2/14/11.
Review of Previous Lesson
Building Java Programs
Random Numbers while loop
Presentation transcript:

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 similarity The answer is A

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

QUESTIONS?

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 ‘>’

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

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

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

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

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

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.

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

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

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:

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

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

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

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

ANIMATIONS 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) > radius) vx = -vx; if (Math.abs(ry + vy) > 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 } set the scale of the coordinate system

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

FINAL QUESTIONS?