Input and Output Yale University

Input And Output Yale University-PDF Download

  • Date:23 Jun 2020
  • Views:13
  • Downloads:0
  • Pages:13
  • Size:3.92 MB

Share Pdf : Input And Output Yale University

Download and Preview : Input And Output Yale University


Report CopyRight/DMCA Form For : Input And Output Yale University


Transcription:

Command Line Input and Standard Output Old Bird s Eye View. Command line input Read an integer N as command line argument. Standard output,Flexible OS abstraction for output. In Java output from System out println goes to standard output. By default standard output is sent to Terminal,public class RandomSeq. public static void main String args,int N Integer parseInt args 0. for int i 0 i N i,System out println Math random,java RandomSeq 4. 0 9320744627218469,0 4279508713950715,0 08994615071160994.
0 6579792663546435,New Bird s Eye View,Standard Input and Output. Command Line Input vs Standard Input Standard Input and Output. Command line input Standard input StdIn is library for reading text input. Use command line input to read in a few user values. Standard output StdOut is library for writing text output. Not practical for many user inputs,Input entered before program begins execution. Standard input,Flexible OS abstraction for input, By default standard input is received from Terminal window libraries developed. for this course, Input entered while program is executing also broadly useful. Standard Input and Output Averaging A Stream of Numbers. To use Download StdIn java and StdOut java from booksite Average Read in a stream of numbers and print their average. and put in working directory or use classpath,see booksite public class Average.
public static void main String args,double sum 0 0 cumulative total. public class Add int n 0 number of values, public static void main String args while StdIn isEmpty. StdOut print Type the first integer double x StdIn readDouble. int x StdIn readInt sum sum x,StdOut print Type the second integer n. int y StdIn readInt java Average,int sum x y,StdOut println sum n 10 0 5 0 6 0. StdOut println Their sum is sum,3 0 7 0 32 0,java Add 10 5.
Type the first integer 1 Ctrl d for OS X Linux Unix DrJava. Type the second integer 2 Ctrl z for Windows,Their sum is 3. Key point Program does not limit the amount of data. Redirecting Standard Output, Redirection and Piping Redirecting standard output Use OS directive to send standard. output to a file for permanent storage instead of terminal window. java RandomSeq 1000 data txt,redirect stdout,Redirecting Standard Input Connecting Programs. Redirecting standard input Use OS directive to read standard input Piping Use OS directive to make the standard output of one program. from a file instead of terminal window become the standard input of another. more data txt,0 5475375782884312 pipe stdout of RandomSeq. 0 4971087292684019 to stdin of Average,0 23123808041753813.
redirect stdin java RandomSeq 1000000 java Average. 0 4997970473016028,java Average data txt, 0 4947655567740991 java RandomSeq 1000000 java Average. 0 5002071875644842,Redirecting Standard Output to a Toast Printer. Standard Drawing,java HelloWorld dev toaster,Standard Drawing Standard Draw. Standard drawing StdDraw is library for producing graphical output Standard drawing We provide library StdDraw to plot graphics. To use Download StdDraw java and put in working directory. public class Triangle,public static void main String args. double t Math sqrt 3 0 2 0,StdDraw line 0 0 0 0 1 0 0 0.
StdDraw line 1 0 0 0 0 5 t,library developed StdDraw line 0 5 t 0 0 0 0. for this course StdDraw point 0 5 t 3 0,also broadly useful. java Triangle,Data Visualization Data Visualization. Plot filter Read in a sequence of x y coordinates from standard. bounding box, input and plot using standard drawing more USA txt. 669905 0 247205 0 1244962 0 490000 0 coordinates of. 1097038 8890 245552 7780 13 509 US cities,1103961 1110 247133 3330.
public class PlotFilter 1104677 7780 247205 5560,public static void main String args. double xmin StdIn readDouble rescale coordinate,system java PlotFilter USA txt. double ymin StdIn readDouble,double xmax StdIn readDouble. double ymax StdIn readDouble,StdDraw setXscale xmin xmax. StdDraw setYscale ymin ymax,while StdIn isEmpty read in points.
double x StdIn readDouble and plot them,double y StdIn readDouble. StdDraw point x y,Plotting a Function Chaos Game, Chaos game Play on equilateral triangle with vertices R G B. double x new double N 1,double y new double N 1 Start at R. for int i 0 i N i Repeat the following N times,x i Math PI i N. pick a random vertex,y i Math sin 4 x i Math sin 20 x i.
move halfway between current point and vertex, StdDraw setXscale 0 Math PI draw a point in color of vertex B 3. 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. Q What picture emerges 2,B B G R B G 5,R 0 0 G 1 0. y sin 4 x sin 20x x 0,Chaos Game Chaos Game, Easy modification Color point according to random vertex chosen using. StdDraw setPenColor StdDraw RED to change the pen color. public class Chaos,public static void main String args.
int T Integer parseInt args 0 3,double cx 0 000 1 000 0 500 avoid hardwired. double cy 0 000 0 000 0 866 constants like this B,double x 0 0 y 0 0 java Chaos 10000. for int t 0 t T t,int r int Math random 3,x x cx r 2 0. y y cy r 2 0 between 0 and 2,StdDraw point x y,Sierpinski triangle. Commercial Break Commercial Break,http xkcd com 543.
Barnsley Fern Animation, Barnsley fern Play chaos game with different rules Animation loop Repeat the following. Clear the screen,probability new x new y Move the object. Draw the object,Display and pause for a short while. 15 14x 26y 57 25x 22y 04,13 17x 21y 41 22x 18y 09,70 78x 03y 11 03x 74y 27 Ex Bouncing ball. Ball has position rx ry and constant velocity vx vy. Detect collision with wall and reverse velocity,Q What does computation tell us about nature.
Q What does nature tell us about computation,20th century sciences Formulas. 21st century sciences Algorithms rx ry,Bouncing Ball Bouncing Ball Demo. public class BouncingBall,public static void main String args. double rx 480 ry 860 position,double vx 015 vy 023 constant velocity. double radius 05 radius java BouncingBall,StdDraw setXscale 1 0 1 0 rescale coordinates.
StdDraw setYscale 1 0 1 0,while true,if Math abs rx vx radius 1 0 vx vx. if Math abs ry vy radius 1 0 vy vy,update position. StdDraw setPenColor StdDraw GRAY clear background,StdDraw filledSquare 0 0 0 0 1 0. StdDraw setPenColor StdDraw BLACK draw the ball,StdDraw filledCircle rx ry radius. StdDraw show 20,turn on animation mode,display and pause for 20ms.
Special Effects Deluxe Bouncing Ball Demo, Images Put gif png or jpg file in the working directory and. use StdDraw picture to draw it,java DeluxeBouncingBall. Sound effects Put wav mid or au file in the working directory and. use StdAudio play to play it, Ex Modify BouncingBall to display image and play sound upon collision. Replace StdDraw filledCircle with,StdDraw picture rx ry earth gif. Add following code upon collision with vertical wall. StdAudio play laser wav,laser wav pop wav,Bouncing Ball Challenge N body Simulation.
Q What happens if you call StdDraw filledSquare once before loop Challenge Add gravity. instead of inside,java DeluxeBouncingBall java NBody planets txt. Crash Course in Sound, Standard Audio Sound Perception of the vibration of molecules in our eardrums. Concert A Sine wave scaled to oscillate at 440Hz, Other notes 12 notes on chromatic scale divided logarithmically. Digital Audio Digital Audio in Java, Sampling Represent curve by sampling it at regular intervals Standard audio Library for playing digital audio. library developed,for this course,also broadly useful.
audio CD 44 100,Musical Tone Play That Tune, Concert A Play concert A for 1 5 seconds using StdAudio Play that tune Read in pitches and durations from standard input. sonify using standard audio,double hz 440 0,double seconds 1 5. int SAMPLE RATE 44100,int N int seconds SAMPLE RATE. double a new double N 1,for int i 0 i N i,a i Math sin 2 Math PI i hz SAMPLE RATE. StdAudio play a,Play That Tune,public class PlayThatTune 1 5 Extra Slides.
public static void main String args,while StdIn isEmpty. int pitch StdIn readInt,double seconds StdIn readDouble. double hz 440 0 Math pow 2 pitch 12 0,int SAMPLE RATE 44100. int N int seconds SAMPLE RATE,double a new double N 1. for int i 0 i N i,a i Math sin 2 Math PI i hz SAMPLE RATE.
StdAudio play a,code as before,User Interfaces Swing Graphical User Interface. Command line interface Swing is Java s GUI,import javax swing. User types commands at terminal,Buttons import java awt. import java awt event,Easily customizable,Menus public class GUI implements ActionListener. private int clicks 0,Extends to complex command sequences.
Scrollbars private JFrame frame new JFrame, private JLabel label new JLabel Number of clicks 0. Toolbars public GUI,JButton button new JButton Click Me. Point and click File choosers,button addActionListener this. JPanel panel new JPanel, panel setBorder BorderFactory createEmptyBorder 30 30 10 30. User launches applications by clicking panel setLayout new GridLayout 0 1. panel add button,panel add label, File Open HelloWorld java frame add panel BorderLayout CENTER.
frame setDefaultCloseOperation JFrame EXIT ON CLOSE. Restricted to pre packaged menu options frame setTitle GUI. frame pack,frame show,public void actionPerformed ActionEvent e. label setText Number of clicks clicks,public static void main String args. GUI gui new GUI,a sample Swing application,Ignore details. Computer Animation Java Implementation,Computer animation Display a sequence 1 10. of closely related images in rapid succession,to produce the illusion of movement 2 11.
public class Duke,3 12 public static void main String args. Frame rate Use 15 70 frames per second int images 17. to trick human eye and brain into seeing 13,int WIDTH 130 HEIGHT 80. StdDraw setCanvasSize WIDTH HEIGHT,smooth motion,5 14 for int t 0 true t. Ex 1 Television and motion pictures int i 1 t images. String file T i gif,Ex 2 Java mascot Duke cart wheeling 6 15. StdDraw picture 0 5 0 5 file,StdDraw show 100,7 16 T1 gif T17 gif.
http java sun com docs books tutorial, Operating System Specific Details Twenty Questions. Common OS abstractions Twenty questions User thinks of an integer between one and 1 million. Computer tries to guess it,Operation Windows XP OS X Unix. public class TwentyQuestions, Cycle through recent command Up down arrows Up down arrows Up down arrows public static void main String args. File name completion Tab Tab Tab,int lo 1 hi 1000000. while lo hi, End of file Ctrl z Enter Ctrl d Ctrl d int mid lo hi 2.
StdOut println Is your number mid, Newline character r n n or r n boolean response StdIn readBoolean. Scroll through text one screenful more more,if response hi mid. more else lo mid 1,at a time less less, List files in current directory dir ls ls StdOut println Your number is lo. Redirection pipes, File system C introcs Hi java u introcs Hi java u introcs Hi java. Binary search Each question removes half of possible remaining values. Unix means Unix variants Linux Solaris Aix, Most Windows XP commands also supported in other version of Windows Consequence Always succeeds after 20 questions.
invariant user s number,220 1 million always between lo and hi. Digital Michelangelo Project,Goal Precise 3D description of the David. Laser rangefinder,5 000 hours of scanning 32 Gigabytes. Keyboard Mouse Hard drive Network Digital camera Microphone Hard drive Network 4 Terminal Application where you can type commands to control the operating system Terminal Mac OS X Microsoft Windows 2 5 Command Line Input and Standard Output Command line input Read an integer N as command line argument Standard output Flexible OS abstraction for output In Java output from System out

Related Books