DOC PREVIEW
Princeton COS 126 - Input and Output

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 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 36 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 36 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 36 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 36 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 36 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 36 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Introduction to Computer Science • Sedgewick and Wayne • Copyright © 2007 • http://introcs.cs.princeton.edu1.5 Input and OutputA Foundation for Programmingobjectsfunctions and modulesgraphics, sound, and image I/Oarraysconditionals and loopsMath text I/Oassignment statementsprimitive data typesany program you might want to writeinteract with theoutside worldInput and OutputInput devices.Output devices.DisplaySpeakersMP3 PlayerPrinterMouseKeyboardDigital cameraMicrophoneHard drive NetworkHard driveNetworkGoal. Java programs that interact with the outside world.Our 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.Terminal. Application for typing commands to control the operating system.TerminalMac OS X Microsoft Windows (of long long ago...)Mac TerminalCommand-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 the 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 40.93207446272184690.42795087139507150.089946150711609940.6579792663546435Old Bird's Eye Viewstandard outputcommand-lineargumentsNew Bird's Eye Viewstandard inputstandard outputstandard drawingstandard audiocommand-lineargumentsIntroduction to Computer Science • Sedgewick and Wayne • Copyright © 2007 • http://introcs.cs.princeton.eduStandard Input and OutputCommand-Line Input vs. Standard InputCommand-line inputs.•Useful for providing a few user values (arguments) to a program.•Not practical for a large or unspecified number of user inputs.•Input entered before program begins execution.Standard input.•Flexible OS abstraction for input.•Useful for providing an unlimited amount of data to a program. •By default, standard input is received from Terminal window.•Input entered while program is executing.Standard IO WarmupTo use. If you installed your programming environment correctly in Assignment 0, then you’re all set. Otherwise, download StdIn.java and StdOut.java from the booksite, and put in working directory (with Add.java). 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); }}% java AddType the first integer: 1Type the second integer: 2Their sum is 3Standard IO Example: Averaging A Stream of NumbersAverage. Read in a stream of numbers, and print their average.Key point. Program does not limit 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> is OS X/Linux/Unix/DrJava EOF<Ctrl-z> is Windows analogStandard Input and OutputStandard input. StdIn library has methods to read text input.Standard output. StdOut library has methods to write text output.public class StdInpublic class StdInboolean isEmpty()true if no more values, false otherwise int readInt()read a value of type int double readDouble()read a value of type double long readLong()read a value of type longboolean readBoolean()read a value of type boolean char readChar()read a value of type char String readString()read a value of type String String readLine()read the rest of the line String readAll()read the rest of the textpublic class StdOutpublic class StdOut void print(String s)print s void println(String s)print s, followed by a newline void println()print a new line void printf(String f, ...)formatted printlibraries developedfor this course(and also broadly useful)Introduction to Computer Science • Sedgewick and Wayne • Copyright © 2007 • http://introcs.cs.princeton.eduRedirection and Piping% java RandomSeq 1000 > data.txtredirect standard outputRedirecting Standard OutputRedirecting standard output. Use OS directive to send standard output to a file for permanent storage (instead of terminal window).% more < data.txt0.54753757828843120.49710872926840190.23123808041753813…% java Average < data.txt0.4947655567740991redirect standard inputRedirecting Standard InputRedirecting standard input. Use OS directive to read standard input from a file (instead of terminal window).Connecting ProgramsPiping. Use OS directive to make the standard output of one programbecome the standard input of another.Key point. Program does not limit amount of data.% java RandomSeq 1000000 | java Average0.4997970473016028% java RandomSeq 1000000 | java Average0.5002071875644842pipe standard output to standard inputIntroduction to Computer Science • Sedgewick and Wayne • Copyright © 2007 • http://introcs.cs.princeton.eduStandard Drawing“Hello World” for Standard DrawTo use. If you installed your programming environment correctly in Assignment 0, you’re all set. Otherwise, download StdDraw.java and put in working directory (with Triangle.java).(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); }}Data 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); } }} rescalecoordinatesystemread in points,and


View Full Document

Princeton COS 126 - Input and Output

Download Input and Output
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 Input and Output 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 Input and Output 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?