1 / 8

Cleaning up!

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”.

jera
Download Presentation

Cleaning up!

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cleaning up! Miscellaneous things of some import

  2. 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”

  3. 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) } }

  4. 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) { … } }

  5. 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; } }

  6. 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? } }

  7. 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

  8. 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) = …

More Related