Lecture-05-Java-Methods
Download
Report
Transcript Lecture-05-Java-Methods
Java – Methods
Lecture Notes 5
Methods
All objects, including software objects,
have state and behavior.
example, a student as an object has
name, address, courses, grades, this is
a state. On the other hand, the student
has behaviors/methods, such that
adding/changing course.
Objects
Objects are defined by classes.
A class can contain any number of data and methods they are called members of a class.
Methods allow the programmer to modularize a program.
All variables declared in method definitions are local
variables–they are known only in the method in which
they are defined
Most methods have a list of parameters that provide the
means for communicating information between methods
via method calls.
A method’s parameters are also local variables.
Methods
The formal method definition:
access_modifiers method_modifiers return_type
method_name( parameter_list )
{
method body
}
A method has two major parts:
method declaration (the first line) and method body.
The method declaration defines all of the method's
attributes, such as access level, return type, name, and
arguments. The method body is where all the action
takes place.
Classes and Objects
All what is said below is true for fields and classes as well.
access_modifiers
You control which other classes have access to a method using one of four access
levels:
public, private, protected and package.
An example
package my_package;
public class Point
{
int x, y;
private int moves = 0;
public void move(int dx, int dy)
{
x += dx; y += dy;
moves++;
}
}
Class Member Access
A public class member is accessible throughout the package where it is
declared and from any other package.
A private class member is accessible only within the class body in which
the member is declared. Subclasses cannot see private names
declared in their parent classes.
protected class members can be visible to subclasses or other classes
within the same package.
Eg: public class Shape extends Object{
protected x;
}
public class Circle extends Shape{
// have the access to x;
}
If none of keywords public, private, protected are specified, the
method is not accessible in any other package.
Methods
method name
Method names should be verbs or verb phrases with the
first letter lowercase and the first letter of any
subsequent words capitalized, for example
toLowerCase.
field name
Names of fields that are not final should be nouns or
noun phrases with a lowercase first letter and the first
letters of subsequent words capitalized.
Methods
method_modifiers
static method
This is as a class method. A class method is always invoked without
reference to a particular object. A method that is NOT declared static
is called an instance method, and sometimes called a non-static
method.
abstract method
This method has no implementation, but it does have a signature:
name and number and type of parameters.
final method
This method cannot be overridden by subclasses.
native method is implemented in platform-dependent code, typically
written in another programming language.
synchronized method
We discuss this when we learn threads.
An Example
public class Coin
{
// our public data
final public int head = 0;
final public int tail = 1;
// our private data
private int face; //result of flipping
// methods
public void flip() // Math.random() generate a float number between 0 and 1
{
face = (int)(Math.random()*2); //casting is necesary
}
public int getFace() //since face is private we have to define a method
{
//which returns the result of flipping
return face;
}
}
An Example
public class CountFlips
{
public static void main(String[] args)
{
final int NUM = 10000; // number of flips
int counter=0;
//instantiate(create) an object
Coin mycoin = new Coin();
for(int i=1; i <= NUM; i++)
{
mycoin.flip();
if (mycoin.getFace() == mycoin.tail) //count tails
counter++;
}
double prob = (double) counter/NUM * 100;
System.out.println(
"The probability of having a tail is " + prob);
}
}