s and Relations Definitions and Examples E. Wenderholm Department of Computer Science SUNY Oswego c 2016 Elaine Wenderholm All rights Reserved E. Wenderholm s and Relations
Outline 1 Java Methods Method Overloading 2 A is a Mapping E. Wenderholm s and Relations
s Java Methods Java Methods Method Overloading We define and use Java methods to perform some operation. Methods are a handy way to group together a sequence of statements to be used over and over again. E. Wenderholm s and Relations
s Java Methods Java Methods Method Overloading We define and use Java methods to perform some operation. Methods are a handy way to group together a sequence of statements to be used over and over again. The signature of a Java method consists of the: method name The (ordered) sequence of its input parameter types. The return type and thrown Exceptions are not part of the signature. E. Wenderholm s and Relations
Method Overloading Different Signatures Java Methods Method Overloading What is method overloading? Two methods of the same name but with different input parameters. E. Wenderholm s and Relations
Method Overloading Different Signatures Java Methods Method Overloading What is method overloading? Two methods of the same name but with different input parameters. The return type is not part of the signature. That s why trying to overload a method by changing only it s return type causes a compile error. E. Wenderholm s and Relations
Method Signatures Set Theory Explanation Java Methods Method Overloading Example: method m name <visibility> <return type> m name(t 1 p 1,..., T n p n ) Remember - visibility and return type are not part of a signature. E. Wenderholm s and Relations
Method Signatures Set Theory Explanation Java Methods Method Overloading Example: method m name <visibility> <return type> m name(t 1 p 1,..., T n p n ) Remember - visibility and return type are not part of a signature. To define m name in math notation we would write m name: T 1... T n return type Here we use return type. E. Wenderholm s and Relations
Method Signatures Set Theory Explanation Java Methods Method Overloading Example: method m name <visibility> <return type> m name(t 1 p 1,..., T n p n ) Remember - visibility and return type are not part of a signature. To define m name in math notation we would write m name: T 1... T n return type Here we use return type. Both these notations say the same thing: The function (method) has n input parameters of types T 1,..., T n. The function (method) returns an element of type <return type> E. Wenderholm s and Relations
s in Math A Mapping A is a Mapping Let s look at a function definition using math notation. m name : T 1... T n return type E. Wenderholm s and Relations
s in Math A Mapping A is a Mapping Let s look at a function definition using math notation. m name : T 1... T n return type Think of the types T i as sets. E. Wenderholm s and Relations
s in Math A Mapping A is a Mapping Let s look at a function definition using math notation. m name : T 1... T n return type Think of the types T i as sets. The set of all inputs T 1... T n is the Cartesian product of the sets. E. Wenderholm s and Relations
s in Math A Mapping A is a Mapping Let s look at a function definition using math notation. m name : T 1... T n return type Think of the types T i as sets. The set of all inputs T 1... T n is the Cartesian product of the sets. This says passing any n-tuple of elements (e 1 T 1,..., e n T n ) to the function (method) m name will return an element e T return type. E. Wenderholm s and Relations
s Drawn as a Picture A is a Mapping Let s keep this simple. Say we have a function that has one input argument of type A, and returns an element of type B. E. Wenderholm s and Relations
s Drawn as a Picture A is a Mapping Let s keep this simple. Say we have a function that has one input argument of type A, and returns an element of type B. B f(a a); (Java notation) f : A B (math notation) The math notation is typically drawn as a picture. A f B f: A > B A is the domain of f B is the codomain of f E. Wenderholm s and Relations
A is a Mapping Consider f : A B The domain of f is the set A. The codomain of f is the set B. E. Wenderholm s and Relations
A is a Mapping Consider f : A B The domain of f is the set A. The codomain of f is the set B. The range of f is the (subset) of B that f actually maps to. E. Wenderholm s and Relations
A is a Mapping Consider f : A B The domain of f is the set A. The codomain of f is the set B. The range of f is the (subset) of B that f actually maps to. The range of f is R B = {y f (x) = y, x A} E. Wenderholm s and Relations
Example A is a Mapping abs maps an int to it s absolute value abs: Z > Z domain(abs) = Z codomain(abs) = Z BUT abs only returns positive ints! Z abs Z Z+ The range of abs is the set of values returned by the function abs. range(abs) = {y abs(x) = y, x in Z} = Z+ E. Wenderholm s and Relations
Example A is a Mapping abs maps an int to it s absolute value abs: Z > Z domain(abs) = Z codomain(abs) = Z BUT abs only returns positive ints! Z abs Z Z+ The range of abs is the set of values returned by the function abs. abs maps all integers Z into only the positive integers Z+. The negative integers get left out. Even though the codomain is Z, the range is the positive integers - a subset of Z range(abs) = {y abs(x) = y, x in Z} = Z+ E. Wenderholm s and Relations
Codomain vs Range A is a Mapping The difference between codomain and range becomes painful. Sipser defines the Range as I ve defined the Codomain. I told you it s painful. abs maps an int to it s absolute value abs: Z > Z domain(abs) = Z codomain(abs) = Z BUT abs only returns positive ints! Why might you care? Z abs Z Z+ The range of abs is the set of values returned by the function abs. range(abs) = {y abs(x) = y, x in Z} = Z+ E. Wenderholm s and Relations
Codomain vs Range A is a Mapping The difference between codomain and range becomes painful. Sipser defines the Range as I ve defined the Codomain. I told you it s painful. abs maps an int to it s absolute value abs: Z > Z domain(abs) = Z codomain(abs) = Z BUT abs only returns positive ints! Why might you care? Z abs Z Z+ The range of abs is the set of values returned by the function abs. range(abs) = {y abs(x) = y, x in Z} = Z+ E. Wenderholm s and Relations
Codomain vs Range A is a Mapping The difference between codomain and range becomes painful. Sipser defines the Range as I ve defined the Codomain. I told you it s painful. abs maps an int to it s absolute value abs: Z > Z domain(abs) = Z codomain(abs) = Z BUT abs only returns positive ints! Z abs Z Z+ Why might you care? You will need to in MAT 215. The range of abs is the set of values returned by the function abs. range(abs) = {y abs(x) = y, x in Z} = Z+ E. Wenderholm s and Relations
Codomain vs Range A is a Mapping The difference between codomain and range becomes painful. Sipser defines the Range as I ve defined the Codomain. I told you it s painful. abs maps an int to it s absolute value abs: Z > Z domain(abs) = Z codomain(abs) = Z BUT abs only returns positive ints! Why might you care? Z abs Z Z+ I will make the distinction. The range of abs is the set of values returned by the function abs. range(abs) = {y abs(x) = y, x in Z} = Z+ E. Wenderholm s and Relations
s Just a few more Definitions A is a Mapping Let s use a simple function f : A B as an example. f is a total function provided every element a A is mapped by f to an element f (a) = b B. E. Wenderholm s and Relations
s Just a few more Definitions A is a Mapping Let s use a simple function f : A B as an example. f is a total function provided every element a A is mapped by f to an element f (a) = b B. Integer addition is total. E. Wenderholm s and Relations
s Just a few more Definitions A is a Mapping Let s use a simple function f : A B as an example. f is a total function provided every element a A is mapped by f to an element f (a) = b B. Integer addition is total. f is a partial function if there is at least one element a A in which f (a) is undefined. E. Wenderholm s and Relations
s Just a few more Definitions A is a Mapping Let s use a simple function f : A B as an example. f is a total function provided every element a A is mapped by f to an element f (a) = b B. Integer addition is total. f is a partial function if there is at least one element a A in which f (a) is undefined. Example: integer divide is undefined whenever the denominator is zero. E. Wenderholm s and Relations
s Just a few more Definitions A is a Mapping Let s use a simple function f : A B as an example. f is a total function provided every element a A is mapped by f to an element f (a) = b B. Integer addition is total. f is a partial function if there is at least one element a A in which f (a) is undefined. Example: integer divide is undefined whenever the denominator is zero. f is onto whenever the range(f ) = codomain(f ). E. Wenderholm s and Relations
s Just a few more Definitions A is a Mapping Let s use a simple function f : A B as an example. f is a total function provided every element a A is mapped by f to an element f (a) = b B. Integer addition is total. f is a partial function if there is at least one element a A in which f (a) is undefined. Example: integer divide is undefined whenever the denominator is zero. f is onto whenever the range(f ) = codomain(f ). Example: Absolute value is not onto; increment is onto. E. Wenderholm s and Relations
s Just a few more Definitions A is a Mapping Let s use a simple function f : A B as an example. f is a total function provided every element a A is mapped by f to an element f (a) = b B. Integer addition is total. f is a partial function if there is at least one element a A in which f (a) is undefined. Example: integer divide is undefined whenever the denominator is zero. f is onto whenever the range(f ) = codomain(f ). Example: Absolute value is not onto; increment is onto. f is one-to-one if every element a A is mapped to a unique element in B. E. Wenderholm s and Relations
s Just a few more Definitions A is a Mapping Let s use a simple function f : A B as an example. f is a total function provided every element a A is mapped by f to an element f (a) = b B. Integer addition is total. f is a partial function if there is at least one element a A in which f (a) is undefined. Example: integer divide is undefined whenever the denominator is zero. f is onto whenever the range(f ) = codomain(f ). Example: Absolute value is not onto; increment is onto. f is one-to-one if every element a A is mapped to a unique element in B. Example: Increment is one-to-one. E. Wenderholm s and Relations
s Just a few more Definitions A is a Mapping Let s use a simple function f : A B as an example. f is a total function provided every element a A is mapped by f to an element f (a) = b B. Integer addition is total. f is a partial function if there is at least one element a A in which f (a) is undefined. Example: integer divide is undefined whenever the denominator is zero. f is onto whenever the range(f ) = codomain(f ). Example: Absolute value is not onto; increment is onto. f is one-to-one if every element a A is mapped to a unique element in B. Example: Increment is one-to-one. f has an inverse provided it is one-to-one and onto. Why dwell on this? We will be analyzing and defining functions, and we will need to be sure they are total. E. Wenderholm s and Relations