CS 106A, Lecture 7 Parameters and Return
|
|
- Austen Briggs
- 6 years ago
- Views:
Transcription
1 CS 106A, Lecture 7 Parameters and Return suggested reading: Java Ch This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, and others.
2 Plan For Today Announcements Recap: For Loops Recap: Scope Parameters Return 2
3 Plan For Today Announcements Recap: For Loops Recap: Scope Parameters Return 3
4 For Loops in Java Repeats the loop if this condition passes for (int i = 0; i < 3; i++) { println("i love CS 106A!"); 4
5 Nested loops nested loop: A loop placed inside another loop. for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { print("*"); println(); // to end the line Output: ********** ********** ********** ********** ********** The outer loop repeats 5 times; the inner one 10 times. 5
6 Nested loop question 2 How would we produce the following output? Answer: for (int i = 0; i < 5; i++) { for (int j = 0; j < 5 i - 1; j++) { print("."); for (int j = 0; j <= i; j++) { print(i + 1); println(); 6
7 Methods in Java We can define new methods in Java just like in Karel: private void name() { statement; statement;... For example: private void printgreeting() { println("hello world!"); println("i hope you have a great day."); 7
8 Methods in Java public void run() { int x = 2; printx(); private void printx() { // ERROR! "Undefined variable x" println("x has the value " + x); 8
9 Plan For Today Announcements Recap: For Loops Recap: Scope Parameters Return 9
10 By Chris Piech 10
11 Variable Scope The scope of a variable refers to the section of code where a variable can be accessed. Scope starts where the variable is declared. Scope ends at the termination of the code block in which the variable was declared. A code block is a chunk of code between { brackets 11
12 Variable Scope You cannot have two variables with the same name in the same scope. for (int i = 1; i <= 100 * line; i++) { int i = 2; // ERROR print("/"); 12
13 Variable Scope You cannot have two variables with the same name in the same scope. for (int i = 1; i <= 100 * line; i++) { int i = 2; // ERROR while (...) { int i = 5; // ERROR 13
14 Variable Scope You can have two variables with the same name in separate scopes. public void run() { for (int i = 0; i < 5; i++) { int w = 2; // i ok here // w ok here for (int i = 0; i < 2; i++) { int w = 3; // i ok here // w ok here 14
15 Variable Scope You can have two variables with the same name in separate scopes. public void run() { int num = 5; cow(); println(num); // prints 5 private void cow() { int num = 10; println(num); // prints 10 15
16 Variable Scope You can have two variables with the same name in different scopes. public void run() { int num = 5; cow(); println(num); run 5 num private void cow() { int num = 10; println(num); 16
17 Variable Scope You can have two variables with the same name in different scopes. public void run() { int num = 5; cow(); println(num); run cow 5 num private void cow() { int num = 10; println(num); 17
18 Variable Scope You can have two variables with the same name in different scopes. public void run() { int num = 5; cow(); println(num); run cow 5 num num 10 private void cow() { int num = 10; println(num); 18
19 Variable Scope You can have two variables with the same name in different scopes. public void run() { int num = 5; cow(); println(num); run cow 5 num num 10 private void cow() { int num = 10; println(num); 19
20 Variable Scope You can have two variables with the same name in different scopes. public void run() { int num = 5; cow(); println(num); run 5 num private void cow() { int num = 10; println(num); 20
21 Plan For Today Announcements Recap: For Loops Recap: Scope Parameters Return 21
22 Parameters Parameters let you provide a method some information when you are calling it. 22
23 Methods = Toasters parameter 23
24 Example: readint readint( Your guess? "); 24
25 Example: readint We call readint We give readint some information in parenthesis (the text to print to the user) readint( Your guess? "); 25
26 Example: printgreeting printgreeting(5); (Prints a greeting a certain number of times) 26
27 Wouldn t it be nice if We call printgreeting We give printgreeting some information (the number of greetings to print) printgreeting(5); 27
28 Methods with Parameters Tells Java this method needs one int in order to execute. private void printgreeting(int times) { // use times to print the greeting 28
29 Methods with Parameters printgreeting(5); private void printgreeting(int times) { // use times to print the greeting 29
30 Methods with Parameters printgreeting(5); private void printgreeting(int times) { for (int i = 0; i < times; i++) { println("hello world!"); 30
31 Methods with Parameters public void run() { int repeats = 5; printgreeting(repeats); private void printgreeting(int times) { for (int i = 0; i < times; i++) { println("hello world!"); 31
32 Methods with Parameters public void run() { int repeats = 5; printgreeting(repeats); private void printgreeting(int times) { for (int i = 0; i < times; i++) { println("hello world!"); run 5 repeats printgreeting? times 32
33 Methods with Parameters public void run() { int repeats = 5; printgreeting(repeats); private void printgreeting(int times) { for (int i = 0; i < times; i++) { println("hello world!"); run 5 repeats printgreeting 5 times 33
34 Methods with Parameters public void run() { int times = 5; printgreeting(times); private void printgreeting(int times) { for (int i = 0; i < times; i++) { println("hello world!"); run 5 times printgreeting? times 34
35 Methods with Parameters public void run() { int times = 5; printgreeting(times); private void printgreeting(int times) { for (int i = 0; i < times; i++) { println("hello world!"); run 5 times printgreeting 5 times 35
36 Parameters are Copies // NOTE: This program is buggy!! public void run() { int x = 3; run addfive(x); // prints "x = 3"! println("x = " + x); 3 x private void addfive(int x) { x += 5; 36
37 Parameters are Copies // NOTE: This program is buggy!! public void run() { int x = 3; run addfive addfive(x); // prints "x = 3"! println("x = " + x); private void addfive(int x) { x += 5; 3 x 3 x 37
38 Parameters are Copies // NOTE: This program is buggy!! public void run() { int x = 3; run addfive addfive(x); // prints "x = 3"! println("x = " + x); private void addfive(int x) { x += 5; 3 x 8 x 38
39 Parameters are Copies // NOTE: This program is buggy!! public void run() { int x = 3; run addfive(x); // prints "x = 3"! println("x = " + x); 3 x private void addfive(int x) { x += 5; 39
40 Parameters parameter: A value passed to a method by its caller. Write a method drawbox to draw a box of any size. When declaring the method, we will state that it requires the caller to tell it the width and height of the box. When calling the method, we will specify the width and height to use. run 10, 4 drawbox ********** * * * * ********** 7, 5 drawbox ******* * * * * * * ******* 40
41 Declaring a parameter Stating that a method requires a parameter in order to run private void name(type name) { statements; Example: private void password(int code) { println("the password is: " + code); When password is called, the caller must specify the integer code to print. 41
42 Multiple parameters A method can accept multiple parameters separated by commas:, When calling it, you must pass values for each parameter. Declaration: private void name(type name,..., type name) { statements; Call: name(value, value,..., value); 42
43 Passing a parameter Calling a method and specifying values for its parameters methodname(expression); Example: public void run() { password(42); password(12345); Output: The password is 42 The password is Illegal to call without passing an int for that parameter. password(); // Error password(3.7); // Error 43
44 How params are passed When the method is called: The value is stored into the parameter variable. The method's code executes using that value. public void run() { chant(7); 7 private void chant(int times) { for (int i = 0; i < times; i++) { println("java is great!"); 44
45 Drawing boxes Lets write a program that uses methods and parameters to print the following boxes: ********** * * * * ********** ******* * * * * * * * * ******* The code to draw each box will be very similar. Would variables help? Would constants help? 45
46 drawbox drawbox(10, 4); 46
47 drawbox We call drawbox We give drawbox some information (the size of the box we want) drawbox(10, 4); 47
48 drawbox private void drawbox(int width, int height) { // use width and height variables // to draw a box 48
49 drawbox ********** * * * * ********** private void drawbox(int width, int height) { line(width); for (int line = 0; line < height - 2; line++) { boxside(width); line(width); 49
50 drawbox ********** * * * * ********** private void drawbox(int width, int height) { line(width); for (int line = 0; line < height - 2; line++) { boxside(width); line(width); 50
51 drawbox ********** * * * * ********** private void drawbox(int width, int height) { line(width); for (int line = 0; line < height - 2; line++) { boxside(width); line(width); 51
52 drawbox ********** * * * * ********** private void drawbox(int width, int height) { line(width); for (int line = 0; line < height - 2; line++) { boxside(width); line(width); 52
53 drawbox ********** * * * * ********** private void drawbox(int width, int height) { line(width); for (int line = 0; line < height - 2; line++) { boxside(width); line(width); 53
54 line ********** private void line(int count) { for (int i = 0; i < count; i++) { print("*"); println(); 54
55 boxside * * private void boxside(int width) { print("*"); for (int i = 0; i < width - 2; i++) { print(" "); println("*"); 55
56 boxside public void run() { drawbox(10, 4); drawbox(7, 6); ********** * * * * ********** ******* * * * * * * * * ******* 56
57 Plan For Today Announcements Recap: For Loops Recap: Scope Parameters Return 57
58 Return Return values let you give back some information when a method is finished. 58
59 Methods = Toasters parameter 59
60 Methods = Toasters parameter 60
61 Methods = Toasters 61
62 Methods = Toasters 62
63 Methods = Toasters return 63
64 Example: readint int x = readint( Your guess? "); 64
65 Example: readint We call readint We give readint some information (the text to print to the user) int x = readint( Your guess? "); 65
66 Example: readint When finished, readint gives us information back (the user s number) and we put it in x. int x = readint( Your guess? "); 66
67 Example: readint When we set a variable equal to a method, this tells Java to save the return value of the method in that variable. int x = readint( Your guess? "); 67
68 Example: meterstocm double cm = meterstocm(5); (Returns the given number of m as cm) 68
69 Example: meterstocm We call meterstocm We give meterstocm some information (the number of meters) double cm = meterstocm(5); 69
70 Example: meterstocm When meterstocm finishes, it returns the number of cm, and we put that in this variable. double cm = meterstocm(5); 70
71 Methods and Return Tells Java this method needs one double in order to execute. private double meterstocm(double meters) {... 71
72 Methods and Return Tells Java that, when this method finishes, it will return a double. private double meterstocm(double meters) {... 72
73 Methods and Return Tells Java that, when this method finishes, it will return a double. (Void meant returns nothing) private double meterstocm(double meters) { return 100 * meters; 73
74 Methods and Return public void run() { double meters = readdouble("#meters? ); double cm = meterstocm(meters); println(cm + " centimeters."); private double meterstocm(double meters) { return 100 * meters; 74
75 Methods and Return public void run() { double meters = readdouble("#meters? ); double cm = meterstocm(meters); println(cm + " centimeters."); private double meterstocm(double meters) { return 100 * meters; run? meters 75
76 Methods and Return public void run() { double meters = readdouble("#meters? ); double cm = meterstocm(meters); println(cm + " centimeters."); private double meterstocm(double meters) { return 100 * meters; run 5 meters 76
77 Methods and Return public void run() { double meters = readdouble("#meters? ); double cm = meterstocm(meters); println(cm + " centimeters."); private double meterstocm(double meters) { return 100 * meters; run 5 meters meterstocm? meters 77
78 Methods and Return public void run() { double meters = readdouble("#meters? ); double cm = meterstocm(meters); println(cm + " centimeters."); private double meterstocm(double meters) { return 100 * meters; run 5 meters meterstocm 5 meters 78
79 Methods and Return public void run() { double meters = readdouble("#meters? ); double cm = meterstocm(meters); println(cm + " centimeters."); private double meterstocm(double meters) { return 100 * meters; 500 run 5 meters meterstocm 5 meters 79
80 Methods and Return public void run() { double meters = readdouble("#meters? ); double cm = meterstocm(meters); println(cm + " centimeters."); private double meterstocm(double meters) { return 100 * meters; 500 run 500 cm 5 meters meterstocm 5 meters 80
81 Methods and Return public void run() { double meters = readdouble("#meters? ); double cm = meterstocm(meters); println(cm + " centimeters."); private double meterstocm(double meters) { return 100 * meters; run 500 cm 5 meters 81
82 Methods and Return public void run() { double meters = readdouble("#meters? ); println(meterstocm(meters) + "cm."); private double meterstocm(double meters) { return 100 * meters; If a method returns something, you can use it directly in an expression! 82
83 Return return: To send out a value as the result of a method. Parameters send information in from the caller to the method. Return values send information out from a method to its caller. A call to the method can be used as part of an expression. -42 Math.abs(-42) run Math.round(2.71) Q: Why return? Why not just println the result value? 83
84 Methods visibility type nameofmethod(parameters) { statements visibility: usually private or public type: type returned by method (e.g., int, double, etc.) Can be void to indicate that nothing is returned parameters: information passed into method 84
85 Returning Booleans private boolean iseven(int number) { 85
86 Returning Booleans private boolean iseven(int number) { if (number % 2 == 0) { return true; else { return false; 86
87 Returning Booleans private boolean iseven(int number) { if (number % 2 == 0) { return true; else { return false; public void run() { int num = readint("? "); if (iseven(num)) { println("even!"); else { println("odd!"); 87
88 Returning Booleans private boolean iseven(int number) { return number % 2 == 0; 88
89 Returning Booleans public void run() { for(int i = 1; i <= 100; i++) { if(isdivisibleby(i, 7)) { println(i); private void isdivisibleby(int a, int b) { return a % b == 0; 89
90 Return Return ends a method s execution. private int multiplybytwo(int num) { return num * 2; println("hello world?"); // not executed! 90
91 Return Return ends a method s execution. private int max(int num1, int num2) { if(num1 >= num2) { return num1; return num2; // here only if num1 < num2 public void run() { println(max(2,3)); 91
92 Revisiting a Bug // NOTE: This program is buggy!! public void run() { int x = 3; addfive(x); // prints "x = 3"! println("x = " + x); private void addfive(int x) { x += 5; 92
93 Fixed! // NOTE: This program is feeling just fine public void run() { int x = 3; x = addfive(x); // prints "x = 5"! println("x = " + x); private int addfive(int x) { x += 5; return x; 93
94 Recap Announcements Recap: For Loops Recap: Scope Parameters Return Next time: Strings (new variable type!) 94
CS 106A, Lecture 5 Booleans and Control Flow
CS 106A, Lecture 5 Booleans and Control Flow suggested reading: Java Ch. 3.4-4.6 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5
More informationMemory Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University
Memory Chris Piech CS106A, Stanford University Learning Goals 1. Be able to trace memory with references Write this program Who thinks this prints true? Who thinks this prints true? public void run() {
More informationCS106A Review Session
CS106A Review Session Nick Troccoli This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides
More informationCS 106A, Lecture 25 Life After CS 106A, Part 1
CS 106A, Lecture 25 Life After CS 106A, Part 1 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based
More informationCS 106A, Lecture 14 Events and Instance Variables
CS 106A, Lecture 14 Events and Instance Variables Reading: Art & Science of Java, Ch. 10.1-10.4 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons
More informationCS 106A, Lecture 3 Problem-solving with Karel
CS 106A, Lecture 3 Problem-solving with Karel suggested reading: Karel, Ch. 5-6 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5
More informationCS 106A, Lecture 16 Arrays
CS 106A, Lecture 16 Arrays suggested reading: Java Ch. 11.1-11.5 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights
More informationNested Loops Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University
Nested Loops Chris Piech CS106A, Stanford University By Chris Once upon a time X was looking for love! int x = 5; if(lookingforlove()) { int y = 5; println(x + y); 5 x X was looking for love! int x =
More informationCS 106A, Lecture 3 Problem-solving with Karel
CS 106A, Lecture 3 Problem-solving with Karel suggested reading: Karel, Ch. 5-6 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5
More informationCS 106A, Lecture 27 Final Exam Review 1
CS 106A, Lecture 27 Final Exam Review 1 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on
More informationCS 106A, Lecture 14 Events and Instance Variables
CS 106A, Lecture 14 Events and Instance Variables Reading: Art & Science of Java, Ch. 10.1-10.4 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons
More informationCS 106A, Lecture 9 Problem-Solving with Strings
CS 106A, Lecture 9 Problem-Solving with Strings suggested reading: Java Ch. 8.5 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5
More informationCS 106A, Lecture 9 Problem-Solving with Strings
CS 106A, Lecture 9 Problem-Solving with Strings suggested reading: Java Ch. 8.5 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5
More informationCS 106A, Lecture 27 Final Exam Review 1
CS 106A, Lecture 27 Final Exam Review 1 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on
More informationCS 106A, Lecture 23 Interactors and GCanvas
CS 106A, Lecture 23 Interactors and GCanvas suggested reading: Java Ch. 10.5-10.6 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5
More informationSSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford
SSEA Computer Science: Track A Dr. Cynthia Lee Lecturer in Computer Science Stanford Topics for today Java programming language: new tools Two new kinds of operators: RELATIONAL operators (>,
More informationFriday Four Square! Today at 4:15PM, Outside Gates
Control Structures Announcements Programming Assignment #1 due right now. Due on next Wednesday if using a late day. Email due on Sunday night. Programming Assignment #2 out today, due Wednesday, January
More informationCOSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand
COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236
More informationCS 106A, Lecture 11 Graphics
CS 106A, Lecture 11 Graphics reading: Art & Science of Java, 9.1-9.3 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All
More informationControl Statements. if for while
Control Structures Control Statements if for while Control Statements if for while This This is is called called the the initialization initialization statement statement and and is is performed performed
More informationControl Structures and Methods
Control Structures and Methods An Interesting Article For Newcomers in Silicon Valley, the Dream of Entrepreneurship Still Lives http://www.nytimes.com/2012/01/25/us/silicon-valley-newcomers-are-still-dreaming-big.html
More informationYEAH 2: Simple Java! Avery Wang Jared Bitz 7/6/2018
YEAH 2: Simple Java! Avery Wang Jared Bitz 7/6/2018 What are YEAH Hours? Your Early Assignment Help Only for some assignments Review + Tips for an assignment Lectures are recorded, slides are posted on
More informationTopic 7 parameters. Based on slides from Marty Stepp and Stuart Reges from
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 knowledge. We've tended to forget that no computer
More information2. [20] Suppose we start declaring a Rectangle class as follows:
1. [8] Create declarations for each of the following. You do not need to provide any constructors or method definitions. (a) The instance variables of a class to hold information on a Minesweeper cell:
More informationBuilding Java Programs
Building Java Programs Chapter 3 Lecture 5: Parameters and Scope reading: 3.1 i's scope Scope scope: The part of a program where a variable exists. From its declaration to the end of the { braces A variable
More informationCS 106A, Lecture 24 Interactors and NameSurfer
CS 106A, Lecture 24 Interactors and NameSurfer suggested reading: Java Ch. 10.5-10.6 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution
More informationVariables Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University
Variables Chris Piech CS106A, Stanford University New Ability Write a program that calculates the tax, tip and total bill for us at a restaurant. The program should ask the user for the subtotal, and then
More informationHomework Assignment 2: Java Console and Graphics
SSEA August 2016 Cynthia Lee CS106A Homework Assignment 2: Java Console and Graphics Based on past assignments created by Marty Stepp, Mehran Sahami, Keith Schwarz, Eric Roberts, Stuart Reges, and others.
More informationBuilding Java Programs
Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1 self-check: #1-6 exercises: #1-3 videos: Ch. 3 #1 Redundant recipes Recipe for baking 20 cookies: Mix the following ingredients in
More informationProgramming Lecture 4
Five-Minute Review 1. What are classes and objects? What is a class hierarchy? 2. What is an expression? A term? 3. What is a variable declaration? 4. What is an assignment? What is precedence? 5. What
More informationExpressions, Statements, and Control Structures
Expressions, Statements, and Control Structures Announcements Assignment 2 out, due next Wednesday, February 1. Explore the Java concepts we've covered and will be covering. Unleash your creative potential!
More informationBuilding Java Programs
Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1 (I think this is a made up language like Visual Basic but not quite.) 2 Promoting reuse Programmers build increasingly complex applications
More informationCS 106A, Lecture 19 ArrayLists
CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights
More informationCS111: PROGRAMMING LANGUAGE II
CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A
More informationCS 106B, Lecture 1 Introduction to C++
CS 106B, Lecture 1 Introduction to C++ reading: Programming Abstractions in C++, Chapters 1 & 2 This document is copyright (C) Stanford Computer Science and Ashley Marty Stepp, Taylor, licensed under Creative
More informationCS 106X, Lecture 16 More Linked Lists
CS 106X, Lecture 16 More Linked Lists reading: Programming Abstractions in C++, Chapters 11-12, 14.1-14.2 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative
More informationCS 106X, Lecture 10 Recursive Backtracking
CS 106X, Lecture 10 Recursive Backtracking reading: Programming Abstractions in C++, Chapter 9 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons
More informationRecap: Assignment as an Operator CS 112 Introduction to Programming
Recap: Assignment as an Operator CS 112 Introduction to Programming q You can consider assignment as an operator, with a (Spring 2012) lower precedence than the arithmetic operators First the expression
More informationSimple Java YEAH Hours. Brahm Capoor and Vrinda Vasavada
Simple Java YEAH Hours Brahm Capoor and Vrinda Vasavada What are YEAH hours? Held soon after each assignment is released Help you to get an early start on your assignments Future dates TBA Slides will
More informationAppendix: Common Errors
Appendix: Common Errors Appendix 439 This appendix offers a brief overview of common errors that occur in Processing, what those errors mean and why they occur. The language of error messages can often
More informationCS 112 Introduction to Programming
CS 112 Introduction to Programming (Spring 2012) Lecture #7: Variable Scope, Constants, and Loops Zhong Shao Department of Computer Science Yale University Office: 314 Watson http://flint.cs.yale.edu/cs112
More informationCS 112 Introduction to Programming
CS 112 Introduction to Programming Graphics; Animation Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin q Issues on PS3 NumberCoolness:
More informationCS 112 Introduction to Programming
CS 112 Introduction to Programming Graphics; Animation Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin q Issues on PS3 NumberCoolness:
More informationAdmin. CS 112 Introduction to Programming. Recap. Example: Nested Loop. Example: Rewrite
Admin CS 112 Introduction to Programming Graphics; Animation Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu q Issues on PS3 NumberCoolness:
More informationCS 106A, Lecture 20 ArrayLists and HashMaps
CS 106A, Lecture 20 ArrayLists and HashMaps suggested reading: Java Ch. 13.2 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License.
More informationCS 106X, Lecture 14 Classes and Pointers
CS 106X, Lecture 14 Classes and Pointers reading: Programming Abstractions in C++, Chapter 6, 11 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons
More informationProgramming Lecture 4
Five-Minute Review 1. What are classes and objects? What is a class hierarchy? 2. What is an expression? A term? 3. What is a variable declaration? 4. What is an assignment? What is precedence? 5. What
More informationAnimation. Piech, CS106A, Stanford University
Animation Our story so far Our story so far Learning Goals 1. Write animated programs 2. Center an object You will be able to write Bouncing Ball Learning Goals For Me 1. Speak slower But First! private
More informationSSEA Computer Science: CS106A. Dr. Cynthia Lee Lecturer in Computer Science Stanford
SSEA Computer Science: CS106A Dr. Cynthia Lee Lecturer in Computer Science Stanford Topics for today Learn how to harness computing power to solve problems. To that end: Introduce Java programming language
More informationPick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1
Conditionals Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary CS105 04 Conditionals 1 Pick a number CS105 04 Conditionals 2 Boolean Expressions An expression that
More informationCS 106A Midterm Review. Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017
+ CS 106A Midterm Review Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017 Details n Only the textbook is allowed n n n The Art and Science of Java Karel Course Reader You will
More informationCS110 Introduction to Computing Fall 2016 Practice Exam 1 -- Solutions
CS110 Introduction to Computing Fall 2016 Practice Exam 1 -- Solutions The exam will be closed-note and closed-book; please consider this fact before using your notes on this practice version. Please see
More informationCSc 110, Autumn 2016 Lecture 6: Parameters. Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2016 Lecture 6: Parameters Adapted from slides by Marty Stepp and Stuart Reges Promoting reuse Programmers build increasingly complex applications Enabled by existing building blocks, e.g.
More informationExpressions and Control Statements
Expressions and Control Statements Recap From Last Time Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated
More informationProgramming Lecture 4
Five-Minute Review 1. What is a class hierarchy? 2. Which graphical coordinate system is used by Java (and most other languages)? 3. Why is a collage a good methapher for GObjects? 4. What is a CFG? What
More informationCS 101 Computer Programming
CS 101 Computer Programming Methods (Chapter 5) Özyeğin University slides partially adapted from UW CSE 142 course slides Remember? Control Flow: the order in which statements are executed CS 101 Özyeğin
More informationLecture 13: Two- Dimensional Arrays
Lecture 13: Two- Dimensional Arrays Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson 2013. All rights reserved. Nested Loops Nested loops nested loop:
More informationAdvanced if/else & Cumulative Sum
Advanced if/else & Cumulative Sum Subset of the Supplement Lesson slides from: Building Java Programs, Chapter 4 by Stuart Reges and Marty Stepp (http://www.buildingjavaprograms.com/ ) Questions to consider
More informationCS 139 Practice Midterm Questions #2
CS 139 Practice Midterm Questions #2 Spring 2016 Name: 1. Write Java statements to accomplish each of the following. (a) Declares numbers to be an array of int s. (b) Initializes numbers to contain a reference
More informationCS107, Lecture 9 C Generics Function Pointers
CS107, Lecture 9 C Generics Function Pointers Reading: K&R 5.11 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution 2.5 License. All
More informationPractice Midterm Examination
Nick Troccoli Practice Midterm CS 106A July 18, 2017 Practice Midterm Examination Midterm Time: Monday, July 24th, 7:00P.M. 9:00P.M. Midterm Location: Hewlett 200 Based on handouts by Mehran Sahami, Eric
More informationProblem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur
Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Lecture 18 Switch Statement (Contd.) And Introduction to
More informationExpressions and Control Statements
Expressions and Control Statements Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 18 at 3:15 PM. Email: Due Sunday, January 20 at 11:59PM. Need help? Stop by the LaIR!
More informationboolean & if-then-else
boolean & if-then-else Lecture 03 Step 1: Open VSCode and its Integrated Terminal Step 2: npm run pull Step 3: npm run start Step 4: Open another tab to pollev.com/comp110 Assignments Out Problem Set 0
More informationCS/ENGRD 2110 SPRING 2018
CS/ENGRD 2110 SPRING 2018 Lecture 7: Interfaces and http://courses.cs.cornell.edu/cs2110 1 2 St Valentine s Day! It's Valentines Day, and so fine! Good wishes to you I consign.* But since you're my students,
More informationArray. Prepared By - Rifat Shahriyar
Java More Details Array 2 Arrays A group of variables containing values that all have the same type Arrays are fixed length entities In Java, arrays are objects, so they are considered reference types
More informationAssignment #2: Intro to Java Due: 11AM PST on Wednesday, July 12
Nick Troccoli Assignment 2 CS 106A July 5, 2017 Assignment #2: Intro to Java Due: 11AM PST on Wednesday, July 12 This assignment should be done individually (not in pairs) Based on handouts by Mehran Sahami,
More informationDrawing Geometrical Objects. Graphic courtesy of Eric Roberts
Methods Drawing Geometrical Objects Graphic courtesy of Eric Roberts Drawing Geometrical Objects Constructors new GRect( x, y, width, height) Creates a rectangle whose upper left corner is at (x, y) of
More informationProgramming: Java. Chapter Objectives. Chapter Objectives (continued) Program Design Including Data Structures. Chapter 7: User-Defined Methods
Chapter 7: User-Defined Methods Java Programming: Program Design Including Data Structures Chapter Objectives Understand how methods are used in Java programming Learn about standard (predefined) methods
More informationMore on Strings. String methods and equality. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Compu<ng and Mathema<cs h=p://www.wit.
More on Strings String methods and equality Produced by: Mairead Meagher Dr. Siobhán Drohan Department of Compu
More informationCSE 142 Su01 Midterm 1 Sample Solution page 1 of 5
CSE 142 Su01 Midterm 1 Sample Solution page 1 of 5 Answer all of the following questions. READ EACH QUESTION CAREFULLY. Answer each question in the space provided on these pages. Budget your time so you
More informationTopic 4 Expressions and variables
Topic 4 Expressions and variables "Once a person has understood the way variables are used in programming, he has understood the quintessence of programming." -Professor Edsger W. Dijkstra Based on slides
More informationWhat s the difference?
What s the difference? Syntax Types Compiled Scope Objects Raw memory access Garbage collection Syntax def hello(): if True: print("hello world!") void hello() { if (true) { cout
More informationCS 151. Exceptions & Javadoc. slides available on course website. Sunday, September 9, 12
CS 151 Exceptions & Javadoc slides available on course website 1 Announcements Prelab 1 is due now. Please place it in the appropriate (Mon vs. Tues) box. Please attend lab this week. There may be a lecture
More informationCSE 341 Lecture 27. JavaScript scope and closures. slides created by Marty Stepp
CSE 341 Lecture 27 JavaScript scope and closures slides created by Marty Stepp http://www.cs.washington.edu/341/ Recall: Scope scope: The enclosing context where values and expressions are associated.
More informationDeclarations and Access Control SCJP tips
Declarations and Access Control www.techfaq360.com SCJP tips Write code that declares, constructs, and initializes arrays of any base type using any of the permitted forms both for declaration and for
More informationCS 177 Week 15 Recitation Slides. Review
CS 177 Week 15 Recitation Slides Review 1 Announcements Final Exam on Friday Dec. 18 th STEW 183 from 1 3 PM Complete your online review of your classes. Your opinion matters!!! Project 6 due Just kidding
More informationControl Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003
Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an
More informationCS 101 Spring 2007 Midterm 2 Name: ID:
You only need to write your name and e-mail ID on the first page. This exam is CLOSED text book, closed-notes, closed-calculator, closed-neighbor, etc. Questions are worth different amounts, so be sure
More informationObject-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018
Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 1: Types and Control Flow http://courses.cs.cornell.edu/cs2110/2018su Lecture 1 Outline 2 Languages Overview Imperative
More informationMobile App:IT. Methods & Classes
Mobile App:IT Methods & Classes WHAT IS A METHOD? - A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. -
More informationCS 106X Lecture 26: Inheritance and Polymorphism in C++
CS 106X Lecture 26: Inheritance and Polymorphism in C++ Monday, March 13, 2017 Programming Abstractions (Accelerated) Winter 2017 Stanford University Computer Science Department Lecturer: Chris Gregg reading:
More informationVariables One More (but not the last) Time with feeling
1 One More (but not the last) Time with feeling All variables have the following in common: a name a type ( int, float, ) a value an owner We can describe variables in terms of: who owns them ( Processing
More informationClasswork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015
Classwork 7: Craps N. Duong & R. Rodriguez, Java Crash Course January 6, 2015 For this classwork, you will be writing code for the game Craps. For those of you who do not know, Craps is a dice-rolling
More informationFinal Exam CS 152, Computer Programming Fundamentals December 9, 2016
Final Exam CS 152, Computer Programming Fundamentals December 9, 2016 Name: NetID: Answer all questions in the space provided. Write clearly and legibly, you will not get credit for illegible or incomprehensible
More informationSection Handout #6: HashMaps, ArrayLists, and Classes
Nick Troccoli Section #6 CS 106A August 1, 2017 Section Handout #6: HashMaps, ArrayLists, and Classes HashMaps Portions of this handout by Eric Roberts, Marty Stepp, Chris Piech, and Mehran Sahami 1. Name
More informationProgramming Lecture 4
Five-Minute Review 1. What is a class hierarchy? 2. Which graphical coordinate system is used by Java (and most other languages)? 3. Why is a collage a good methapher for GObjects? 4. What is a CFG? What
More informationQuestion 1 (10 points) Write the correct answer in each of the following: a) Write a Processing command to create a canvas of 400x300 pixels:
Question 1 (10 points) Write the correct answer in each of the following: a) Write a Processing command to create a canvas of 400x300 pixels: size(400, 300); b) After the above command is carried out,
More informationIntroduction to Programming (Java) 4/12
Introduction to Programming (Java) 4/12 Michal Krátký Department of Computer Science Technical University of Ostrava Introduction to Programming (Java) 2008/2009 c 2006 2008 Michal Krátký Introduction
More informationQueues and Unit Testing
Queues and Unit Testing Shreya Shankar Stanford CS 106B 3 July 2018 Based on slides created by Ashley Taylor, Marty Stepp, Chris Gregg, Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami,
More informationObject-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I
Object-Oriented Programming (OOP) Basics CSCI 161 Introduction to Programming I Overview Chapter 8 in the textbook Building Java Programs, by Reges & Stepp. Review of OOP History and Terms Discussion of
More informationChapter 4 The If Then Statement
The If Then Statement Conditional control structure, also called a decision structure Executes a set of statements when a condition is true The condition is a Boolean expression For example, the statement
More informationGuide to Success I. in December)
Five-Minute Review 1. What are expression statements? Compound statements? 2. What is a scope? 3. What are conditional statements in Java? How about iterative statements? 4. In conditionals, why should
More informationObject-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007
Object-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007 (These notes are very rough, and differ somewhat from what I presented in class; I am posting them here to supplemental
More informationChapter 7 User-Defined Methods. Chapter Objectives
Chapter 7 User-Defined Methods Chapter Objectives Understand how methods are used in Java programming Learn about standard (predefined) methods and discover how to use them in a program Learn about user-defined
More informationCS110 Introduction to Computing Fall 2016 Practice Exam 1
CS110 Introduction to Computing Fall 2016 Practice Exam 1 The exam will be closed-note and closed-book; please consider this fact before using your notes on this practice version. Please see the abbreviated
More informationCS 112 Introduction to Programming
CS 112 Introduction to Programming (Spring 2012) Lecture #8: More on Conditional & Loop Zhong Shao Department of Computer Science Yale University Office: 314 Watson http://flint.cs.yale.edu/cs112 Acknowledgements:
More informationAssignment #4 Hangman Due: 10:30am on Friday, Febuary 17th This assignment may be done in pairs (which is optional, not required)
Chris Piech Handout #20 CS 106A Feb 8, 2017 Assignment #4 Hangman Due: 10:30am on Friday, Febuary 17th This assignment may be done in pairs (which is optional, not required) Y.E.A.H. hours Thursday from
More informationCOMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading
COMP 202 CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading More on OO COMP 202 Objects 3 1 Static member variables So far: Member variables
More informationFlow of Control of Program Statements CS 112 Introduction to Programming. Basic if Conditional Statement Basic Test: Relational Operators
Flow of Control of Program Statements CS 112 Introduction to Programming (Spring 2012) q Java provides two types of program flow of control statements: decision statements, or conditional statements: decide
More information