Transcript ppt

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
s
i
m
i
l
a
r
i
t
y
0
1
2
3
4
5
6
7
8
9
The answer is A
Exam 1 Questions
Do you remember pre-increment from recitations?
First increment and then add.
Added j values: 1, 3, 5, 7, 9 and 11
The answer is C
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
StdDraw.point(0.5, 0.5)
y1
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);
set the scale of the coordinate system
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) > 1.0 - radius)
vx = -vx;
if (Math.abs(ry + vy) > 1.0 - 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
}
}
}
PLOTTING FUNCTION GRAPHS
public class FunctionGraph {
public static void main(String[] args) {
the function
int N = Integer.parseInt(args[0]);
y = sin(4x) + sin(20x), sampled
double[] x = new double[N+1];
at N points between
double[] y = new double[N+1];
x = 0 and x = pi
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]);
}
}
}
FINAL QUESTIONS?