A Methodical Approach to Methods

Slides:



Advertisements
Similar presentations
Recursion CS 367 – Introduction to Data Structures.
Advertisements

CIT 590 Intro to Programming Java lecture 3. Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows.
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
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.
Access to Names Namespaces, Scopes, Access privileges.
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
More on Objects CS 102 More, more, more on Objects.
Applets. An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from a browser You.
26-Jun-15 Applets. 2 An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Lecture 17: Animation Yoni Fridman 7/27/01 7/27/01.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Supplementary for method, DCO10803, Quarter 3, Page 1 of 7 Object-Oriented Programming and Design DCO10803 Supplementary for method  Prototype.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Introduction to Methods
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
Recitation 2 James Wei Professor Peck 1/17/2013. Covered in this Recitation Arrays Variable Scoping Local variables Instance variables Class variables.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
OOP Basics Classes & Methods (c) IDMS/SQL News
8/2: Recursion About Scoping.java Recursion Program of the Day.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
User-Written Functions
Suppose we want to print out the word MISSISSIPPI in big letters.
The Lifetime of a Variable
C Functions -Continue…-.
“Form Ever Follows Function” Louis Henri Sullivan
ㅎㅎ Fourth step for Learning C++ Programming Namespace Function
Java Course Review.
Class Structure 15-Jun-18.
Some Eclipse shortcuts
A Lecture for the c++ Course
Programming Fundamentals Lecture #7 Functions
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
INF3110: Exercises Part 3 Comments and Solutions
Namespaces, Scopes, Access privileges
Systems of Computation
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
Recursion Recursive Thinking Recursive Programming
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Class Structure 16-Nov-18.
Lecture 11 C Parameters Richard Gesick.
Class Structure 28-Nov-18.
Class Structure 7-Dec-18.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Class Structure 2-Jan-19.
CS2011 Introduction to Programming I Methods (II)
Namespaces, Scopes, Access privileges
Class Structure 25-Feb-19.
Java Programming COMP-417 Applet
Introduction to Primitives
CS148 Introduction to Programming II
Local variables and how to recognize them
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Methods/Functions.
Visibilities and Static-ness
Presentation transcript:

A Methodical Approach to Methods Java's Work Horses CS 102-02 Lecture 4-2 1

What’s up? Automatic variables & scope Recursion Applets again

Scope & Duration In life: In programming: Scope is where you go Duration is how long you live In programming: Scope is where a variable can be referenced Duration is how long the variable's value exists

Automatic Means There When You Need Them Automatic variables Automatic duration: automatically created & destroyed Don't get intialized Automatic variables include: Local variables declared (and defined) inside a method Parameter values passed to a method

Initializing Variables Class instance variables are initialized: public class Circle { private int x_center, y_center, radius; // Each of the above variables is now // set to 0, because all instance // variables are initialized, and // numeric variables are initialized // to 0. : }

Initializing Auto Variables Automatic variables are not initialized: public class Circle { : public double Area() { double r_squared; // Next line's no good, because we // haven't initialized r_squared // yet. return Math.PI * r_squared; }

Auto in Action Suppose the circle class includes the following method: public double Area() { double r_squared; r_squared = radius * radius; return Math.PI * r_squared; } In the course of a program, we call the area method on a Circle object: Circle testCircle = new Circle(10, 10, 200); : // r_squared (defined in Area()) doesn't exist testCircle.Area(); // r_squared did exist, but it's gone now

The Rules of Scope Class scope Visible everywhere within a class class Test { Test() { k = 2; // Notice that k hasn't appeared } int j = 1; int i = j; int k; // k is defined here, but its scope // is the entire class

Block Scope Block scope Variables are visible everywhere within curly braces: { and }. Includes: Methods for loops Other blocks

Hiding Variables What happens if a variable is declared in the scope of another variable with the same name? class Test { static int x = 1; public static void main(String[] args) { int x = 0; : // What's the value of x here? }

Telling Them Apart When you run this code: class Test { static int x = 1; public static void main(String[] args) { int x = 0; System.out.print("x=" + x); System.out.println(", Test.x=" + Test.x); } You get: x=0, Test.x=1

Running Again with Recursion Doing the same thing over and over Looping Recursion: functions that call themselves Recursion is like induction Example: Factorial 5! = 5 * 4 * 3 * 2 * 1 is the same as 5! = 5 * 4!

The Factorial Example How do we calculate the factorial of n? Multiply n times the factorial of n-1: n * (n-1)! n * (n-1) * ((n-1) -1)! n * (n-1) * ((n-1) - 1) * (((n-1) -1) - 1)! n * (n-1) *•••((((•••((((n-1) -1) -1) ••• -1) Now we have to translate this into Java Remember that n! = n * (n-1)! Special case when n == 1

Factorial Recursion // Recursive definition of method factorial public long factorial( long number ) { if ( number <= 1 ) // base case return 1; else // n * (n-1)! return number * factorial( number - 1 ); }

Bottoming Out // Recursive definition of method factorial public long factorial( long number ) { if ( number <= 1 ) // base case // 1! = 1 (By definition, 0! = 1) return 1; else // n * (n-1)! return number * factorial( number - 1 ); }

Visual Café Example The Call window shows the call stack Call stack is a list of "active" methods As one method calls another, each method is placed on the stack For the factorial example, what do you expect to see in the call stack?

Why Choose Recursion? Recursion is very similar to iteration Recursive methods can be rewritten with iteration Recursion is a different way of looking at the problem Factorial recursion based on: n! = n * (n-1)! Factorial iteration based on: n! = n * (n-1) * (n-2) * ... * 1

Factorial Again // Iterative definition public long factorialIter ( long number ) { long factorial = 1; if (number == 0) { return 1; } else { for(long temp=number; temp != 1; temp--){ factorial *= temp; } return factorial;

Applets Revisited Applets aren't standalone applications Depend on somebody else to provide numerous functions Applet class has 21 methods, but 5 of them control interaction between an applet and its container Browser Applet

The Fab Five Applet Methods I public void init() Gets things started Good place to set up the initial screen appearance Only gets called once public void paint(Graphics g) Draws stuff on the screen called every time the applet needs to be redrawn

The Fab Five Applet Methods II Three methods used with threads public void start() Called after init() Called again every time the browser returns to the HTML page public void stop() Called every time the browser leaves the page public void destroy()