DOC PREVIEW
WUSTL CSE 131 - sp14_4

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Input and OutputOperating SystemsTerminalCommand-Line Input and Standard OutputOld Bird's Eye ViewNew Bird's Eye ViewCommand-Line Input vs. Standard InputStandard Input and OutputStandard Input and OutputAveraging A Stream of NumbersRedirecting Standard OutputRedirecting Standard InputConnecting ProgramsRedirecting Standard Output to a Toast PrinterSlide 16Standard DrawingDefault Drawing CanvasStandard DrawData VisualizationData VisualizationPlotting a FunctionChaos GameChaos GameChaos GameBarnsley FernCommercial BreakFor Studio4/Lab41.5 Input and OutputIntroduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · 1/14/19 07:13:03 AM2Input and OutputInput devices.Output devices.Goal. Java programs that interact with the outside world.DisplaySpeakersMP3 PlayerPrinterMouseKeyboardDigital cameraMicrophoneHard drive NetworkHard driveNetwork3Operating SystemsOur approach.Define Java libraries of functions for input and output.Use operating system (OS) to connect Java programs to:file system, each other, keyboard, mouse, display, speakers.4Terminal. Application where you can type commands to control the operating system.TerminalMac OS X Microsoft WindowsWe have been running programs in eclipse, where output is directed to the Console Window.If you use the Sedgewick library for input, you can supply values by typing them into the Console Window.5Command-Line Input and Standard OutputCommand-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.Example: output N random numberspublic 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()); } }}6Old Bird's Eye ViewThe ArgsProcessor implementation we have been using accepts command-line arguments, but if they are not present, it prompts the user for the required inputs.7New Bird's Eye ViewUseful for lots of data.Useful for providing just a few inputs, such as for configuring a program.8Command-Line Input vs. Standard InputCommand-line input.Use command-line input to read in a few user values.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.Input entered while program is executing.9Standard Input and OutputStandard input. StdIn is library for reading text input.Standard output. StdOut is library for writing text output.libraries developedfor this course(also broadly useful)10Standard Input and OutputTo use. Download StdIn.java and StdOut.java from booksite,and put in working directory (or use classpath). public class Add { public static void main(String[] args) { StdOut.print("Type the first integer: "); int x = StdIn.readInt(); StdOut.print("Type the second integer: "); int y = StdIn.readInt(); int sum = x + y; StdOut.println("Their sum is " + sum); }}see booksite http://introcs.cs.princeton.edu/java/stdlib/11Averaging A Stream of NumbersAverage. Read in a stream of numbers, and print their average.Key point. Program does not limit the amount of data.public class Average { public static void main(String[] args) { double sum = 0.0; // cumulative total int n = 0; // number of values while (!StdIn.isEmpty()) { double x = StdIn.readDouble(); sum = sum + x; n++; } StdOut.println(sum / n); }}% java Average10.0 5.0 6.0 3.0 7.0 32.0<Ctrl-d>10.5<Ctrl-d> for OS X/Linux/Unix/DrJava<Ctrl-z> for WindowsRedirecting standard output. Use OS directive to send standard output to a file for permanent storage (instead of terminal window).12% java RandomSeq 1000 > data.txtredirect stdoutRedirecting Standard OutputRedirecting standard input. Use OS directive to read standard input from a file (instead of terminal window).13% type data.txt 0.54753757828843120.49710872926840190.23123808041753813…% java Average < data.txt0.4947655567740991redirect stdinRedirecting Standard InputFor windows14Connecting ProgramsPiping. Use OS directive to make the standard output of one programbecome the standard input of another.% java RandomSeq 1000000 | java Average0.4997970473016028% java RandomSeq 1000000 | java Average0.5002071875644842pipe stdout of RandomSeqto stdin of Average15Redirecting Standard Output to a Toast Printer% java HelloWorld > /dev/toasterStandard Drawing17Standard DrawingStandard drawing. StdDraw is library for producing graphical output.library developedfor this course(also broadly useful)Default Drawing Canvas18(0,0)(1,1)(0,1)(1,0)You can change the corners by calling methods shown on the previous slide that alter the xFand y scales. Anything you plot outside the set scale will not be shown, and no error message is issued.19Standard DrawStandard drawing. We provide library StdDraw to plot graphics.To use. Download StdDraw.java and put in working directory.(0, 0)(1, 0)(½, ½3)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); StdDraw.line(0.5, t, 0.0, 0.0); StdDraw.point(0.5, t/3.0); }} % java Triangle20Data VisualizationPlot filter. Read in a sequence of (x, y) coordinates from standard input, and plot using standard drawing.public class PlotFilter { public static void main(String[] args) { double xmin = StdIn.readDouble(); double ymin = StdIn.readDouble(); double xmax = StdIn.readDouble(); double ymax = StdIn.readDouble(); StdDraw.setXscale(xmin, xmax); StdDraw.setYscale(ymin, ymax); while (!StdIn.isEmpty()) { double x = StdIn.readDouble(); double y = StdIn.readDouble(); StdDraw.point(x, y); } }} rescale coordinatesystemread in points,and plot them21Data Visualization% more < USA.txt669905.0 247205.0 1244962.0 490000.0 1097038.8890 245552.7780 1103961.1110 247133.3330 1104677.7780 247205.5560 ...% java PlotFilter < USA.txtcoordinates of13,509 US citiesbounding box22Plotting a Functiondouble[] 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,


View Full Document

WUSTL CSE 131 - sp14_4

Documents in this Course
sp14_3

sp14_3

29 pages

sp14_2

sp14_2

43 pages

sp14_10

sp14_10

19 pages

sp14_9

sp14_9

16 pages

sp14_8

sp14_8

22 pages

sp14_7

sp14_7

33 pages

sp14_6

sp14_6

27 pages

sp14_5

sp14_5

55 pages

lecture1

lecture1

33 pages

lab0

lab0

6 pages

Load more
Download sp14_4
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view sp14_4 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view sp14_4 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?