Methods and Intro to OOP
Download
Report
Transcript Methods and Intro to OOP
Functions/Methods
1
Function / Method
Definition
collection of statements that may be invoked
by name (from another function or method)
Functions/methods that we have invoked
Input.readInt(), setLayout(), Math.sqrt()
Functions/methods that we have defined
main(), init(), action()
* the term “method” will be used from now on
2
Defining methods that the
program will invoke
Why?
code inside main() (or init() or action()) may
be lengthy
sometimes, the task requires something
redundant (e.g., print two blocks of asterisks,
one of size 5, the other of size 10)
we want to define our own math function
Methods allow us to organize our code
into logical units
3
Defining Methods
Syntax (for applications)
public static void methodName() {
// statements
}
For applets, omit the static keyword
Methods are defined at the level of the
class (same level as main() or init())
4
Parameters and Return
Values
It is possible to pass data to a method
this allows for the possibility of a different
effect based on the value of the parameter
It is possible to return data to the caller
like Input.readInt() or Math.sqrt(), the
method can return a value
Example that uses both features
int factorial(int num) { … }
5
Invoking Methods
Invocation
methodName(expression-list)
Formal versus actual parameters
formal parameter: the variable used to
represent the argument within the method
the expression used during invocation that is
evaluated and then passed as a value to the
method
6
Object-Oriented
Programming:
Classes in Java
7
Object
Definition: a thing that has identity, state,
and behavior
identity: a distinguished instance of a class
state: collection of values for its variables
behavior: capability to execute methods
* variables and methods are defined in a class
8
Class
Definition: a collection of data (variables)
and methods that operate on that data
data/methods define the
contents/capabilities of the instances
(objects) of the class
object creation occurs with the statement
variable = new class(parameters);
classes can be viewed as factories for
objects
9
Class
A class is a template for an object
An object is an instance of a class
10
Class
All data and variables defined within a
class are called instance variables
because each instance of that class (each
object of the class) contains a copy of that
variable
Methods and variables defined within a
class are called members of that class
11
Two Kinds of Variables
in Java
Variables of a primitive type
e.g., int x; char c;
Variables of a reference type (class)
e.g., Button b; String s;
Conventions
Primitive types are reserved words in Java
and are indicated in all-lower-case letters
Class names: first letter usually capitalized
12
Variables and Values
Primitive type variables
int x;
…
x = 5;
X
X
5
13
Variables and References
Reference type variables
X
Button x;
…
x = new Button(“click”);
Button Object
X
“click”
14
The new Keyword
new Button(“click”) creates a Button
object and returns a reference (an
address) to that object that a Button
variable could hold
Button Object
X
1023
1023:
“click”
1023 is some address in memory
15
The Dot (“.”) Operator
Allows access to variables (primitive and
reference types) and methods of
reference type variables.
Ex.
TextField t = new TextField(10);
t.setText(“hi”);
*accessing a method using the dot operator
16
Method Invocation
Syntax for method invocation
object.methodName(arguments)
Method may return a value or simply
produce an effect on the object
To find out what methods are available for
a given class
javap package.name.NameOfClass
ex. Javap java.awt.Button
17
Strings Revisited
Strings are objects as well
String is an existing Java class
s = “Hello” is just a shorthand for
s= new String(“Hello”);
String methods
int length()
String substring(int x,int y);
no “manipulating” methods
18
Variables and Objects
Let Circle be a class with:
variable r that indicates its radius
method area() that computes its area
Declaration:
Circle c;
Instantiation:
c = new Circle();
Usage:
c.r = 5.5;
System.out.println(c.area());
19
The complete Circle class
public class Circle {
public double x,y; // center coordinates
public double r; // radius
// the methods
public double circumference()
{ return 2*3.14*r; }
public double area() { return 3.14*r*r; }
}
20
Using the Circle class
public class TestCircle {
public static void main(String args[]) {
Circle c;
c = new Circle();
c.x = 2.0; c.y = 2.0; c.r = 5.5;
System.out.println(c.area());
}
}
21