CS 112 Introduction to Programming Graphics; Animation Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email:

Slides:



Advertisements
Similar presentations
BUILDING JAVA PROGRAMS CHAPTER 3 PARAMETERS AND OBJECTS.
Advertisements

Nested for loops Cont’d. 2 Drawing complex figures Use nested for loops to produce the following output. Why draw ASCII art? –Real graphics require a.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs Lecture 3-1: Parameters (reading: 3.1)
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Copyright 2008 by Pearson Education 1 Class constants and scope reading: 2.4 self-check: 28 exercises: 11 videos: Ch. 2 #5.
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs.
Topic 7 parameters Based on slides bu Marty Stepp and Stuart Reges from "We're flooding people with information. We.
Building Java Programs
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
Building Java Programs Parameters and Objects. 2 Redundant recipes Recipe for baking 20 cookies: –Mix the following ingredients in a bowl: 4 cups flour.
Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 4: Loop Figures and Constants reading:
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
Nested for loops.
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
The for loop.
CS 112 Introduction to Programming Animations; Methods with return Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Copyright 2010 by Pearson Education Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1.
CS 112 Introduction to Programming Loop Examples; Variable Scoping; Nested Loops; Yang (Richard) Yang Computer Science Department Yale University 208A.
CS 112 Introduction to Programming Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Copyright 2009 by Pearson Education Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1 self-check: #1-6 exercises: #1-3 videos: Ch.
Building Java Programs Chapter 3
Building Java Programs
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
Building Java Programs
Topic 7 parameters "We're flooding people with information. We need to feed it through a processor. A human must turn information into intelligence or.
Building Java Programs
Redundant recipes Recipe for baking 20 cookies:
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Variables variable: A piece of the computer's memory that is given a name and type, and can store a value. Like preset stations on a car stereo, or cell.
Building Java Programs
Building Java Programs
Topic 6 loops, figures, constants
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
Building Java Programs
Presentation transcript:

CS 112 Introduction to Programming Graphics; Animation Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:

Admin  Issues on PS3 m NumberCoolness: printSpaces(n): prints n – 1 spaces, not n You can either change the method to print n or adapt your main m a walkthrough to be held by Fitz this evening from 8 to 9 pm m please suggest topics (e.g., nested loops, method parameters) that you want us to cover more 2

Recap  Nested loops o If a loop is inside another loop, we say the two form a nested loop o The #loop times of the inner loop can depend on the outer loop variable  Method with parameters o Specify parameters to control the behavior of a method o Redundancy removal/abstraction through generalization

Example: Nested Loop int N = 5; for (int line = 1; line <= N; line++) { for (int j = 1; j <= (-1 * line + N); j++) { System.out.print("."); } System.out.print(line); for (int j = 1; j <= (line - 1); j++) { System.out.print("."); } System.out.println(); }

Example: Rewrite public static void dots(int n) { for (int i = 1; i <= n; i++) { System.out.print("."); } public static void main(String[] args) { int N = 5; for (int line = 1; line <= N; line++) { dots( -1 * line + N ); System.out.print(line); dots( line – 1 ); System.out.println(); } // end of outer for loop }

Out-Class Read: Another Way to Motivate Method With Parameters 6

Example: Reuse Matrix int N = 5; for (int line = 1; line <= N; line++) { // initial dots for (int j = 1; j <= (-1 * line + N); j++) { System.out.print("."); } // the number System.out.print(line); // the second set of dots for (int j = 1; j <= (line - 1); j++) { System.out.print("."); } System.out.println(); }  Print two copies of inverse diagonal matrix

Solution 1 8 public class DrawMatrix1 { public static void main(String[] args) { drawMatrix3(); drawMatrix5(); } public static void drawMatrix3() { int N = 3; for (int line = 1; line <= N; line++) { for (int j = 1; j <= (-1 * line + N); j++) { // initial dots System.out.print("."); } System.out.print(line); // the number for (int j = 1; j <= (line - 1); j++) { // the second set of dots System.out.print("."); } System.out.println(); } } // end of drawMatrix3 public static void drawMatrix5() { int N = 5; for (int line = 1; line <= N; line++) { for (int j = 1; j <= (-1 * line + N); j++) { // initial dots System.out.print("."); } System.out.print(line); // the number for (int j = 1; j <= (line - 1); j++) { // the second set of dots System.out.print("."); } System.out.println(); } } // end of drawMatrix5 } This code is redundant.

Remove Redundancy 9 public class DrawMatrix2 { } public static void drawMatrix() { for (int line = 1; line <= N; line++) { for (int j = 1; j <= (-1 * line + N); j++) { // initial dots System.out.print("."); } System.out.print(line); // the number for (int j = 1; j <= (line - 1); j++) { // the second set of dots System.out.print("."); } System.out.println(); } // end of for line } // end of drawMatrix public static void main(String[] args) { drawMatrix(); // should print 3 drawMatrix(); // should print 5 } // end of main int N = 3; N = 5; ERROR: N is out of scope

Solution 2 10 public class DrawMatrix2 { } public static void drawMatrix() { for (int line = 1; line <= N; line++) { for (int j = 1; j <= (-1 * line + N); j++) { // initial dots System.out.print("."); } System.out.print(line); // the number for (int j = 1; j <= (line - 1); j++) { // the second set of dots System.out.print("."); } System.out.println(); } // end of for line } // end of drawMatrix public static void main(String[] args) { drawMatrix(); // should print 3 drawMatrix(); // should print 5 } // end of main static int N; // introduce a class scope variable N = 3; N = 5;

Problem of Class Variable Indirection 11 public class DrawMatrix2 { } public static void drawMatrix() { for (int line = 1; line <= N; line++) { for (int j = 1; j <= (-1 * line + N); j++) { // initial dots System.out.print("."); } System.out.print(line); // the number for (int j = 1; j <= (line - 1); j++) { // the second set of dots System.out.print("."); } System.out.println(); } // end of for line } // end of drawMatrix public static void main(String[] args) { drawMatrix(); } // end of main static int N; // introduce a class scope variable N = 3; N = 5; Not self clear; Implicit, depends on context;

Solution 3: Parameterization  Specify a parameter to control the behavior of a method  Methods with parameters solve an entire class of similar problems  Redundancy removal/abstraction through generalization m The more general a building block, the easier to reuse it m We will learn more techniques on generalization/abstraction 12

Method Design Heuristics 1. The main method should read as a concise summary of the overall set of tasks performed by the program. 2. Each method should have a clear set of responsibilities. 3. No method should do too large a share of the overall task. 4. Use method with parameters to remove redundancy with generalization, but do not over generalize 5. Data should be declared/used at the lowest level possible (localized data).

Example: Draw Boxes  Consider the task of printing the following box figures using “*” and “ “: ***** * ***** ********** * ********** ***** * ***** What method(s) and parameter(s)?

Box Figures Param. Chaining ***** * ***** ********** * ********** ***** * ***** public static void main(String[] args) { drawBox(5, 3); drawBox(10, 3); drawBox(5, 4); } // end of main public static void drawBox(int width, int height) { // step 1. print width of "*" repeat("*", width); System.out.println(); // step 2. loop a total of height - 2 times // for each row: // print * // print width -2 " " for (int i = 1; i <= height - 2; i++) { System.out.print("*"); repeat(" ", width - 2); System.out.println("*"); } // step 3. print width of "*" repeat("*", width); System.out.println(); } // end of drawBox public static void repeat(String s, int times) { for (int i = 1; i <= times; i ++) { System.out.print( s ); } } // end of repeat

Method Correctness Analysis/Testing  Q: How do you analyze/test the correctness of a method?  Basic idea m A method is correct if for each input (parameters), the output is what you expect for the input  An issue: m Some input may not make sense. m Two approaches Detect such bad input and report Assume that the input is in the valid range: called precondition of the method

Method Correctness Analysis/Testing ***** * ***** ********** * ********** ***** * ***** public static void drawBox(int width, int height) { // step 1. print width of "*" repeat("*", width); System.out.println(); // step 2. loop a total of height - 2 times // for each row: // print * // print width -2 " " for (int i = 1; i <= height - 2; i++) { System.out.print("*"); repeat(" ", width - 2); System.out.println("*"); } // step 3. print width of "*" repeat("*", width); System.out.println(); } // end of drawBox public static void repeat(String s, int times) { for (int i = 1; i <= times; i ++) { System.out.print( s ); } } // end of repeat

Recap: Graphics using StdDraw  To access a static method or class variable defined in another class, use. (…), for example, m StdDraw.setCanvasSize(100, 100); m Color.BLACK;  Method designers may design different methods for the same function 18 StdDraw: rectangle(double x, double y, double halfWidth, double halfHeight); DrawingPanel (Java): rect (int x0, int y0, int width, int height);

Example: Parameterized Drawing  Write method drawCar: r= (x0, y0) Center: (x /2, y0 + 50/2) Size: 50, 25 Center: (x , y0) Radius: 10 Center: (x , y0) Radius: 10 Center: (x /2, y0+50/2) Size: 15, 10

drawCar 20 public static void drawCar(int x0, int y0) { StdDraw.setPenColor(Color.BLACK); StdDraw.filledRectangle(x /2, y0 + 50/2, 100/2, 50/2); // draw wheels StdDraw.setPenColor(Color.RED); StdDraw.filledCircle(x , y0, 10); StdDraw.filledCircle(x , y0, 10); // draw window StdDraw.setPenColor(Color.CYAN); StdDraw.filledRectangle(x / 2, y0 + 25, 15, 10); } Criticism of the method? See Car.java for the final version.

Example: Drawing using Loops  You may use loop (nested loop) to produce more “complex” patterns: public static void main(String[] args) { init(); // set x, y scale to 512 int x0 = 56, y0 = 56; int size = 50; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { StdDraw.square(x0 + i * size + size / 2, y0 + j * size + size / 2, size/2); } } // end of main What is the display?

Example: Drawing using Loops

Example: Book Cover  White 500x600 drawing panel  Three components at m (20, 415), (165, 330), (220, 85) with sizes 150, 120, and 240 m Each component Yale blue background white “CS112" text 1/2 horizontal, 4/5 vertical 10 brown (red=192, green=128, blue=64) “bricks” –1 pixel between two adjacent bricks BookCover.java

Summary of Progress So Far  Primitive data types, expressions and variables  for loops  Methods with parameters 24

Motivating Example: Car Launch  Two cars launching from different heights, which lands further? m Car 1:initial height: 600m, horizontal speed: 30 m/sec, vertical speed: 20 m/sec m Car 2:initial height: 500m, horizontal speed: 40 m/sec, vertical speed: 30 m/sec  Write a program to display their trajectories in 10 sec

Background: Physics  In physics, for each dimension (x or y), given initial velocity v 0 in m/s, acceleration a in m/s 2, and elapsed time t in s m the speed of the moving body at time t: V = v0 + a t m The displacement of the moving body at time t: Displacement = v 0 t + ½ a t 2 m The position of the moving body at time t: Pos = Initial position + displacement

Position  P 0 + v 0 t + ½ a t 2  Horizontal (x): m v x0 * t  Vertical (y):  g ≅ 9.81 m/s 2, downward m y 0 + v y0 t - ½ g t 2

Example  See CarLaunchv1.java

Backup Slides

Exercise: Generalization  Consider the task of printing the following lines/boxes: ************* //13 ******* // 7 *********************************** // 35 ********** * ********** ***** * *****

Design I  Provide a new method drawLine() by calling repeat() public static void drawLine(int width) { repeat(“*”, width); } // end of drawLine ************************* ********** * **********

Design II  Consider a line as a special box with height m draw first line of stars m if (height >= 2) // a real box draw rest ************************* ********** * ********** public static void drawBox(int width, int height)

The if statement Executes a block of statements only if a test is true if ( test ) { statement ;... statement ; }  Example: if (gpa >= 2.0) { System.out.println("Application accepted."); }

34 The if-else Statement  An else clause can be added to an if statement to make it an if-else statement: if ( test ) { statement1; } else { statement2; }  If the condition is true, statement1 is executed; if the condition is false, statement2 is executed  One or the other will be executed, but not both

The if/else Statement  Example: if (gpa >= 2.0) { System.out.println("Welcome to Mars University!"); } else { System.out.println("Application denied."); }

Design II  Consider a line as a special box ************************* ********** * ********** // super elastic drawBox public static void drawBox(int width, int height) { // step 1. print width of "*" repeat("*", width); System.out.println(); for (int i = 1; i <= height - 2; i++) { System.out.print("*"); repeat(" ", width - 2); System.out.println("*"); } if (height >= 2) { // a real box repeat("*", width); System.out.println(); } // end of if } // end of drawBox public static void main(String[] args) { drawBox(13, -1); drawBox(10, 3); } // end of main

Comparison public static void drawLine(int width) { repeat(“*”, width); } // end of drawLine ************************* ********** * ********** // super elastic drawBox public static void drawBox(int width, int height) { // step 1. print width of "*" repeat("*", width); System.out.println(); for (int i = 1; i <= height - 2; i++) { System.out.print("*"); repeat(" ", width - 2); System.out.println("*"); } if (height >= 2) { // a real box repeat("*", width); System.out.println(); } // end of if } // end of drawBox public static void drawBox(int width, int height) { repeat("*", width); System.out.println(); for (int i = 1; i <= height - 2; i++) { System.out.print("*"); repeat(" ", width - 2); System.out.println("*"); } } // end of drawBox