Methods.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

What is output line of the following C++ code? Please trace int i = 1, QP; DATA: 4, B, 3, A double credit, totCredit=0.0; double num = 0.0; string LG;
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Discussion1 Quiz. Q1 Which of the following are invalid Java identifiers (variable names)? a) if b) n4m3 c) Java d) e) DEFAULT_VALUE f) bad-choice.
Computer Science 1620 Loops.
Computer Science A 4 13/3. Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Chapter 6 Control Structures.
Chapter 7: User-Defined Methods
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Conditionals (Cont’d). 2 Nested if/else question Formula for body mass index (BMI): Write a program that produces output like the following: This program.
Chapter 5 Loops.
 Executes a block of code repeatedly  A condition controls how often the loop is executed  Most commonly, the statement is a block statement (set of.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
Java Prepared by Gary Langner Types of Loops u for loop (entrance controlled) –do an action for a definite number of times u while loop (entrance controlled.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program.
L what are executable/non-executable statements l out of the ones below which constructs are executable #include p=3.14; const double PI=3.14; int myfunc(int);
1 Looping Dale/Weems/Headington. 2 KA/JS/P Warning l Save your work often! l In the Khan Academy, JavaScript environment, infinite loops will lock up.
A: A: double “4” A: “34” 4.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
01/24/2005 Introduction to Programming with Java, for Beginners A Closer Look at Constructors and Methods.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
CSE 110 Midterm Review Hans and Carmine. If Statements If statements use decision logic In order to properly use if statements relational operators must.
Calling Methods turnRight(); move(); readInt(“Int please! ”);
Chapter 1.2 Introduction to C++ Programming
CSC111 Quick Revision.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Repetition (While-Loop) version]
Yanal Alahmad Java Workshop Yanal Alahmad
Intro To Classes Review
Coding #1 if(!(index < 0 || index >= list.size( )-1))
Selected Topics From Chapter 6 Iteration
CS106A, Stanford University
CS 106A, Lecture 7 Parameters and Return
CS 106A, Lecture 6 Control Flow and Parameters
CS106A, Stanford University
CS 106A, Lecture 5 Booleans, Control Flow and Scope
Review Operation Bingo
Multiple Files Revisited
Chapter 6: Functions Starting Out with C++ Early Objects Ninth Edition
CS2011 Introduction to Programming I Selections (II)
Recap Week 2 and 3.
Methods and Data Passing
Python Basics with Jupyter Notebook
Fundamental Programming
Multiple Files Revisited
Methods/Functions.
Corresponds with Chapter 5
Presentation transcript:

Methods

Calling Methods turnRight(); move(); readInt(“Int please! ”); println(“hello world”); rect.getX(); drawRobotFace(); rect.setLocation(10, 20);

Defining a Method private void turnRight() { turnLeft(); }

Anatomy of a method public void run() { double mid = average(5.0, 10.2); println(mid); } private double average(double a, double b) { double sum = a + b; return sum / 2;

Anatomy of a method public void run() { double mid = average(5.0, 10.2); println(mid); } private double average(double a, double b) { double sum = a + b; return sum / 2; Output expected Input expected

Anatomy of a method public void run() { double mid = average(5.0, 10.2); println(mid); } private double average(double a, double b) { double sum = a + b; return sum / 2; name

Anatomy of a method public void run() { double mid = average(5.0, 10.2); println(mid); } private double average(double a, double b) { double sum = a + b; return sum / 2; body

Anatomy of a method public void run() { double mid = average(5.0, 10.2); println(mid); } private double average(double a, double b) { double sum = a + b; return sum / 2; return

Anatomy of a method public void run() { method “call” double mid = average(5.0, 10.2); println(mid); } private double average(double a, double b) { double sum = a + b; return sum / 2; method “call”

Void Example private void printIntro() { println("Welcome to class"); println("It's the best part of my day."); } public void run() { printIntro(); }

Example private double metersToCm(double meters) { return 100 * meters; } public void run() { println(metersToCm(5.2)); }

Parameter Example private void printOpinion(int num) { if(num == 5) { println(“I love 5!”); } else { println(“Whattever”); } public void run() { printOpinion(5); }

Multiple Return private String getMonthName(int i) { if (i == 0) { return “January”; } if (i == 1) { return “February”; … return “Unknown”;

Defining a Method 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

private void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } private double readPositive(String prompt) { double value = readDouble(prompt); while(value < 0) { println(“Invalid”); value = readDouble(prompt); return value; private double getArea(double radius) { return PI * radius * radius;

public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); }

public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); }

public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); }

public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } private double readPositive(String prompt) { double value = readDouble(prompt); while(value < 0) { println(“Invalid”); value = readDouble(prompt); } return value;

prompt public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } private double readPositive(String prompt) { double value = readDouble(prompt); while(value < 0) { println(“Invalid”); value = readDouble(prompt); } return value; prompt “Enter radius: “

prompt value public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } private double readPositive(String prompt) { double value = readDouble(prompt); while(value < 0) { println(“Invalid”); value = readDouble(prompt); } return value; prompt “Enter radius: “ value -3

prompt value public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } private double readPositive(String prompt) { double value = readDouble(prompt); while(value < 0) { println(“Invalid”); value = readDouble(prompt); } return value; prompt “Enter radius: “ value -3

prompt value public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } private double readPositive(String prompt) { double value = readDouble(prompt); while(value < 0) { println(“Invalid”); value = readDouble(prompt); } return value; prompt “Enter radius: “ value -3

prompt value public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } private double readPositive(String prompt) { double value = readDouble(prompt); while(value < 0) { println(“Invalid”); value = readDouble(prompt); } return value; prompt “Enter radius: “ value -3

prompt value public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } private double readPositive(String prompt) { double value = readDouble(prompt); while(value < 0) { println(“Invalid”); value = readDouble(prompt); } return value; prompt “Enter radius: “ value 42

prompt value public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } private double readPositive(String prompt) { double value = readDouble(prompt); while(value < 0) { println(“Invalid”); value = readDouble(prompt); } return value; prompt “Enter radius: “ value 42

prompt value public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } private double readPositive(String prompt) { double value = readDouble(prompt); while(value < 0) { println(“Invalid”); value = readDouble(prompt); } return value; prompt “Enter radius: “ value 42

prompt value public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } private double readPositive(String prompt) { double value = readDouble(prompt); while(value < 0) { println(“Invalid”); value = readDouble(prompt); } return value; prompt “Enter radius: “ value 42

double r = readPositive(“Enter radius: “); double area = getArea(r); public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } 42

r public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } 42 r 42

r public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } r 42

r public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } r 42

r public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } private double getArea(double radius) { return PI * radius * radius; } r 42

radius r public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } private double getArea(double radius) { return PI * radius * radius; } r radius 42 42

radius r public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } private double getArea(double radius) { return PI * radius * radius; } r radius 42 42

radius r public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } private double getArea(double radius) { return PI * radius * radius; } 5538.96 r radius 42 42

r public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } 5538.96 r 42

r public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } 5538.96 r 42

r area public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } 5538.96 r 42 area 5538.96

r area public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } r 42 area 5538.96

r area public void run() { double r = readPositive(“Enter radius: “); double area = getArea(r); println(area); } r 42 area 5538.96

Bad Times With Methods // NOTE: This program is buggy!! private void addFive(int x) { x += 5; } public void run() { int x = 3; addFive(x); println("x = " + x);

Good Times With Methods // NOTE: This program is feeling just fine... private int addFive(int x) { x += 5; return x; } public void run() { int x = 3; x = addFive(x); println("x = " + x);

More Examples

Changed Name private void run() { int num = 5; cow(num); } private void cow(int grass) { println(grass);

Same Variable private void run() { int num = 5; cow(); } private void cow() { int num = 10; println(num);

Boolean

Boolean Variable boolean karelIsAwesome = true; boolean myBool = 1 < 2;

Boolean Operations boolean a = true; boolean b = false; //This is false boolean a_and_b = a && b; //This is true boolean a_or_b = a || b; boolean not_a = !a;

Now that’s style!