Week 8 - Friday.  What did we talk about last time?  Static methods.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
30-Jun-15 Static Methods. About methods A method is a named group of declarations and statements You execute those declarations and statements by calling.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
COSC 1P02 Introduction to Computer Science 3.1 Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
C Functions Pepper. Objectives Create functions Function prototypes Parameters – Pass by value or reference – Sending a reference Return values Math functions.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Learners Support Publications Functions in C++
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Chapter 6 Methods Chapter 6 - Methods.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
Department of Computer Engineering Methods Computer Programming for International Engineers.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
Week 10 - Friday.  What did we talk about last time?  References and primitive types  Started review.
Methods.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Functions + Overloading + Scope
Building Java Programs
C Functions Pepper.
Building Java Programs
Week 8 - Friday CS 121.
Week 15 – Wednesday CS 121.
Control structures Chapter 3.
Static Methods 14-Nov-18.
For Loops October 12, 2017.
Introduction to Computer Programming Counting Loops 2
Passing Parameters by value
CS2011 Introduction to Programming I Arrays (II)
Building Java Programs
Building Java Programs
Building Java Programs
Introduction to Computer Science
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
Methods/Functions.
Building Java Programs
Corresponds with Chapter 5
Functions Maria Novosolov.
Presentation transcript:

Week 8 - Friday

 What did we talk about last time?  Static methods

 Proper syntax for calling a static method gives first the name of the class that the method is in, a dot, the name of the method, then the arguments  If the method is in the same class as the code calling it, you can leave off the Class. part  If it is a value returning method, you can store that value into a variable of the right type Class.name(arg1, arg2, arg3);

 Variables from outside of the method don’t exist unless they have been passed in as parameters  No matter how complex a program is, inside this method, only x, y, and z variables exist public static int add(int x, int y){ int z = x + y; return z; } public static int add(int x, int y){ int z = x + y; return z; }

 A magical process called binding happens which copies the values from the calling code into the parameters of the method  The calling code can use variables with the same names, with different names, or even just literals  The method does not change the values of the variables in the original code  Remember, it only has copies of the values

 No connection between the two different x ’s and y ’s xy public static int add(int x, int y){ xy int z = x + y; // return z; } xy public static int add(int x, int y){ xy int z = x + y; // return z; } int a = 10; int x = 3; 5a int y = add( 5, a ); //y contains 15 now int a = 10; int x = 3; 5a int y = add( 5, a ); //y contains 15 now

1. Start a method with the keywords public static 2. Next comes the return type 3. Then the name of the method 4. Then, in parentheses, the arguments you want to give to the method 5. Inside braces, put the body of the method 6. Include a return statement that gives back a value if needed

 Finding the Euclidean distance between two points is very important  There are applications in:  Route planning  Graphic design software  Video game AI  Let’s make a method  Formula:

 Drawing a star is more complicated than drawing a rectangle  For a five pointed star, we need 5 points  Let’s put it into a method so that we can do it repeatedly  We want the method to take a scale, a starting point, and a color 108°

 2D arrays  Overloading methods  More method practice

 Keep reading Chapter 8 of the textbook  Keep working on Project 3