Cleaning up! Miscellaneous things of some import.

Slides:



Advertisements
Similar presentations
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Advertisements

Appendix B Solving Recurrence Equations : With Applications to Analysis of Recursive Algorithms.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Metode di Java Risanuri Hidayat, Ir., M.Sc.. Pendahuluan Classes usually consist of two things: instance variables and methods. The topic of methods is.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Functions Gabriel Hugh Elkaim Spring 2012.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Functions in C++ Eric Roberts CS 106B January 9, 2013.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Chapter 14: Recursion J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Recursion A function that calls itself. Recursion A function which calls itself is said to be recursive. Recursion is a technique which will allow us.
FUNCTIONS A function is a set of instructions that can perform a specific task accordingly. A function is a program that performs a specific task according.
Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem.
RECURSION Go back, Jack, and do it again.. Recursion 2  Recursion occurs whenever "something" is defined in terms of itself.  Structural recursion:
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Classes - Intermediate
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Concepts of Algorithms CSC-244 Unit 5 and 6 Recursion Shahid Iqbal Lone Computer College Qassim University K.S.A.
Methods.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
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.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
Functions and Libraries. The idea of a function Functions in programming A function is a block of code that has been given a name. To invoke that code.
Recursion ● Recursion is a computer programming technique that allows programmers to divide problems into smaller problems of the same type. ● You can.
CS212: Data Structures and Algorithms
Recursion CENG 707.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Data Structures with Java © Rick Mercer
Greatest Common Divisor
Greatest Common Divisor
Methods Chapter 6.
Functions in C++ Eric Roberts CS 106B January 7, 2015.
Java Programming: Program Design Including Data Structures
User-defined Functions
Exam 2 Review 1.
User-defined Functions
Recursion.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Recursion (part 1) October 24, 2007 ComS 207: Programming I (in Java)
Unit 3 Test: Friday.
Recursive GCD Demo public class Euclid {
ㅎㅎ Fifth step for Learning C++ Programming Pointers Homework solution
Classes.
Method of Classes Chapter 7, page 155 Lecture /4/6.
Unit-1 Introduction to Java
Review Lab assignments Homework #3
Recursion Recursio Recursi Recurs Recur Recu Rec Re R
Presentation transcript:

Cleaning up! Miscellaneous things of some import

Names  A “name” is called an “identifier” in Java  Many “things” can be named in Java  Classes  Variables  Methods  Names can be “re-used” (duplicated) if certain rules are followed  The rules make sure that a name refers to exactly one “thing”

Identifiers public class SomeClass { public int foo; private int fi; private int fee; public void foo( int fum ) { // code missing } public void foo( double fi ) { // code missing } public class Client { private SomeClass c; public void method() { // can we write c.fee c.foo c.foo(3) c.foo(3.0) }

Method Overloading  Two methods in the same class can have the same name  Must have different parameter lists  May differ in the type of arguments  May differ in the number of arguments  When the name is used, the specific function must be determined by context. public class SomeClass { public int foo(int x) { … } public int foo(double x) { … } public int foo(int x, int y) { … } public int foo(String x) { … } public int foo(Rectangle x) { … } public int foo(int z) { … } } public class SomeClass { public int foo(int x) { … } public int foo(double x) { … } public int foo(int x, int y) { … } public int foo(String x) { … } public int foo(Rectangle x) { … } public int foo(int z) { … } }

Summary of name reuse  There are no restrictions for using the same identifier in two separate classes.  There are no restrictions for using the same identifier to name local variables and/or parameters of different methods.  A local variable (or parameter) can have the same name as an instance variable of its class.  An instance variable and method can share the same name within the same class.  Methods can be overloaded public class SomeClass { public int fee; private int fi; private int foo; public void foo( int fum ) { int fo; int fee; } public void foo( double fi ) { int fo; int fum; } public class SomeClass { public int fee; private int fi; private int foo; public void foo( int fum ) { int fo; int fee; } public void foo( double fi ) { int fo; int fum; }

Difficulty with name reuse  Consider the following rule:  A local variable (or parameter) can have the same name as an instance variable of its class.  The keyword ‘this’ refers to the currently active object. Use the keyword ‘this’ as an object reference! public class SomeClass { public int conundrum; public void foo() { int conundrum; // how to output the conundrum instance var? } public class SomeClass { public int conundrum; public void foo() { int conundrum; // how to output the conundrum instance var? }

Recursion  Methods can be recursive. The method is invoked within the body of the method!  Consider the following mathematical definition of the factorial function:  How could this be written in Java? public int fact(int n) { int result = 1; for(int i=1; i<=n; i++){ result = result * i; } return result; } public int fact(int n) { int result = 1; for(int i=1; i<=n; i++){ result = result * i; } return result; } public int fact(int n) { if(n == 0) { return 1; } else { return n * fact(n-1); } public int fact(int n) { if(n == 0) { return 1; } else { return n * fact(n-1); } Iterative (non recursive) solution Recursive solution

Recursion  Consider the following mathematical definition of the greatest common divisor of two non-negative integers X and Y where X >= Y. /* precondition: x >= y */ public int gcd(int x, int y) { if(y == 0) { return x; } else { return gcd(y, x%y); } /* precondition: x >= y */ public int gcd(int x, int y) { if(y == 0) { return x; } else { return gcd(y, x%y); } gcd(259, 111) = …