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

Slides:



Advertisements
Similar presentations
First of all – lets look at the windows you are going to use. At the top you have a toolbar, with all your various tools you can use when customising your.
Advertisements

Week 8 - Monday.  What did we talk about last time?  StdAudio.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Variables – the Key to Programming. ICS-3M1 - Mr. Martens - Variables Part 1 What is a Variable? A storage location that is given a “name” You can store.
The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
Week 4: Conditional Execution 1.  So far we have only considered Java programs that do one thing after another, in sequence  Our programs have not had.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
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.
1 CS 177 Week 4 Recitation Slides Conditionals. 2 Announcements Project 1 is due on Feb. 7th 9pm (extended) Project 2 will be posted today (Feb. 5th)
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
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.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
What to do when you are done. PRINTING WITH PHOTOSHOP.
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 3 Variables, Calculations, and Colors Starting Out with Games.
Week 7 - Wednesday.  What did we talk about last time?  Introduction to arrays  Lab 6.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
Click your mouse for next slide Flash – Introduction and Startup Many times on websites you will see animations of various sorts Many of these are created.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
ABI Gradebook Training We are all in this together!
Java Classes Using Java Classes Introduction to UML.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Text and Graphics September 26, Unit 3.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 2 Graphics Programming with C++ and the Dark GDK Library Starting.
1. Press the New Layer Button 3. Double click names to re-name Ball & Shadow layers 2. Click to change to 12 fps Step 1.
CS177 RECITATION WEEK 7 Input and Output (Text & Graphical)
1.To begin click: File, New, then select Design 2.Draw a circle, then use the dimension tool to set it’s size to 84 diameter or 42 radius. 3. Next draw.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
CSC 1010 Programming for All Lecture 7 Input, Output & Graphics.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
File Input and Graphics An extract of slides from set 05_inout.ppt Designed to get you ready for the HW3.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
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.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
CS 5JA Introduction to Java Graphics One of the powerful things about Java is that there is.
Week 10 - Friday.  What did we talk about last time?  References and primitive types  Started review.
Week 4 - Monday.  What did we talk about last time?  Precedence  Selection statements  Loops  Lab 3.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
First of all – lets look at the window’s you are going to use. At the top you have a toolbar, with all your various tools you can use when customising.
Turtle Graphics Let’s see what we can draw on Python!
Learning the Basics of ArcMap 3.3 Updated 4/27/2010 Using Arc/View pt. 1 1.
Graphics Lab: MyPaint Dan Maselko 1. MyPaint Description  For this assignment you will be implementing a very simple paint program.  You should be able.
CS 115 Lecture 6 Graphics Taken from notes by Dr. Neil Moore.
Week 2 - Wednesday CS 121.
EET 2259 Unit 11 Charts and Graphs
Week 8 - Monday CS 121.
Week 15 – Wednesday CS 121.
Week 4 - Monday CS222.
Learning to program with Logo
CS 177 Week 15 Recitation Slides
CS 177 Week 4 Recitation Slides
Lesson 2: Building Blocks of Programming
Coding Concepts (Basics)
1.5 Input and Output Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 2/14/11.
Unit 3: Variables in Java
EET 2259 Unit 11 Charts and Graphs
Week 7 - Monday CS 121.
Presentation transcript:

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()  You feel comfortable with them and can output nicely formatted int s, double s, boolean s, char s, and String s with them 2

 Printing things to the screen and the DrJava console is all well and good  Sometimes you need to store something for later  Sometimes you get tired of typing the same thing over and over again  For these and other reasons, we put things into more permanent storage: files 3

 When we run a Java program from the command line, we can send all the output it makes to a file, instead of to the screen  To do this, you use the > operator to redirect the output to the file name you choose  The following command runs program Test and sends its output to out.txt  The only problem is that you can’t do this easily inside DrJava $ java Test > out.txt 4

 But, a refresher never hurts MethodUse boolean isEmpty() Tells if there are more values int readInt() Read in the next int double readDouble() Read in the next double boolean readBoolean() Read in the next boolean char readChar() Read in the next char String readString() Read in the next String String readLine() Read in a whole line String readAll() Read in all remaining input 5

 Just as we can write data to a file, we can read data from a file, using the < operator  The program “pretends” that a user is typing in the data that it gets from the file  Now, the StdIn methods like isEmpty() and readAll() should make more sense  The following command runs program Test and reads in input from in.txt $ java Test < in.txt 6

 It is possible to redirect both input and output  For example, the following command would run program Test, reading in values from in.txt and printing out information to out.txt $ java Test out.txt 7

 It’s possible to send the output of one program into another program as the input  Sure, you could:  Run the first program  Redirect its output to a file  Run the second program  Read its input from that same file  Why not cut out the middleman?  To pipe data from one program to the next, you use the | operator 8

 The following command shows how you can send the output of a program called Writer into the input of a program called Reader :  Use of output redirection ( > ), input redirection ( < ), and piping ( | ) are not Java commands  These are a feature of the OS and can be used with languages other than Java $ java Writer | java Reader 9

 Files are useful  But not too mind-blowing  We are going to talk about the StdDraw library given by the textbook  StdDraw gives us a canvas we can draw all kinds of things:  Points and lines  Circles, squares, and polygons  Plots of functions  Colors 10

 StdDraw is a library of Java code developed by the textbook authors, just like StdIn  StdDraw allows you to draw output on the screen easily  You can draw points, lines, and polygons in various colors  You can clear and resize the drawing area and even save the results  StdDraw is not standard Java that everyone uses, but it’s a nice tool for graphics 11

 The simplest things you can draw with StdDraw are lines and points  The first thing you should be aware of is that the canvas is drawn like Quadrant I of a Cartesian plane (0,0) (0,1)(1,1) (1,0) 12

 The following methods can be used to draw lines and points MethodUse void line(double x0, double y0, double x1, double y1) Draw a line from (x0,y0) to (x1,y1) void point(double x, double y) Draw a point at (x,y) 13

 Let’s draw a box then divide it into two halves, like so: 14

StdDraw.line (0.0,0.0,1.0,0.0); StdDraw.line (1.0,0.0,1.0,1.0); StdDraw.line (1.0,1.0,0.0,1.0); StdDraw.line (0.0,1.0,0.0,0.0); StdDraw.line (0.5,0.0,0.5,1.0); StdDraw.square(0.5,0.5,0.5); 15

 There are built in commands for drawing:  Circles  Squares  Arbitrary polygons  Filled versions of each one of these  We won’t bother with the arbitrary polygons, but the book shows how to use them  It is also possible to set the color 16

 Here are some methods for drawing circles and squares and setting the color for doing so: MethodUse void circle(double x, double y, double r) Draw a circle centered at (x,y) with radius r void filledCircle(double x, double y, double r) Draw a filled circle centered at (x,y) with radius r void square(double x, double y, double r) Draw a square centered at (x,y) with edges 2r void filledSquare(double x, double y, double r) Draw a filled square centered at (x,y) with edges 2r void setPenColor(Color c) Start drawing with color c 17

 Eventually you will be able to define your own colors  For now you are limited to 13 presets  For example, to make something magenta, you would use the value StdDraw.MAGENTA BLACKBLUECYANDARK_GRAYGRAY GREENLIGHT_GRAYMAGENTAORANGEPINK REDWHITEYELLOW 18

 Make 100 circles at random locations with random sizes and random colors  Location is easy  Size is easy, we just decide on the range of sizes we want and do some math  Color is more painful  We need a switch statement with 13 choices 19

 A number of methods are given to give us more control over the display MethodUse void setXscale(double x0, double x1) Set the x scale void setYscale(double y0, double y1) Set the y scale void setPenRadius(double r) Set the pen radius void clear() Clear canvas to white void clear(Color c) Clear canvas to color c void show(int delay) Delay for delay ms void show() Turn off delay mode 20

StdDraw.setXscale (0.0,100.0); StdDraw.setYscale (0.0,100.0); StdDraw.setPenRadius(0.01); 2 times the default

 As you have seen, the default scale of the canvas is in the range [0,1] for both x and y  We can use the setXscale() method to set the minimum and maximum x values  We can use the setYscale() method to set the minimum and maximum y values  Useful for plotting functions 22

 The show() method lets you specify a delay in milliseconds before things are drawn on the screen  You can use it to slow down or speed up animations 23

 Plotting functions is really useful  Getting smooth curves is hard  Instead, we just pick a whole bunch of x points and figure out the function value  We can just draw dots to plot those values  We can connect them with lines for a more connected look 24

1. Ask the user for the coefficients of the four terms (ax 3 + bx 2 + cx + d) 2. Ask the user for an x range 3. Run through the function and find the minimum and maximum y values hit 4. Rescale the drawing area to show everything 5. Plot the function 25

 Can we simulate a cannon being fired?  Let the user enter an initial velocity in m/s  Let the user an angle between 0° and 90°  Assume each iteration takes 1/10 of a second  Assume an initial height of 20 m  We draw the path of the cannon ball as it flies through the air  Let’s also set the x and y scales to both be [0,100] 26