80 likes | 220 Views
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”.
E N D
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 intfoo(int x) { … } public intfoo(double x) { … } public intfoo(int x, int y) { … } public intfoo(String x) { … } public intfoo(Rectangle x) { … } public intfoo(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 intfi; private intfoo; public void foo( intfum ) { intfo; int fee; } public void foo( double fi ) { intfo; intfum; } }
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? } }
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(inti=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); } } 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 intgcd(int x, int y) { if(y == 0) { return x; } else { return gcd(y, x%y); } } gcd(259, 111) = …