Lec 25
Lec 25
I Semester 2008-09
Lecture 25
1
Complete picture of a class
class class_name
{
public static double pi=3.1426 ;
private static int count =0 ;
Static attributes
public static void method1() and
{ .... } Static methods
private static int method2(int n)
{....}
2
Static and non-static methods
3
Revisit Point class
public class Point
{ double x;
double y;
4
Example : Point class
5
Example : Point class
6
Example : Point class
7
Example : Point class
For the reason, read carefully the description of method Center(). If it were
designed as a non-static method then P.Center(A,B,C) has got nothing to do with
the point P as such. So there is no reason we should invoke it as P.Center(A,B,C).
If you do so, it would not be an error but it is a very bad programing practice.
8
Example : Point class
9
Example : Point class
Hence :
So whenever you design a method in Point class, first ask yourself whether the
method if invoked as P.method() involves the current point P in any way. If yes,
design it as non-static, otherwise design it as a static method.
10
Example : Point class
public class Point
{ double x;
double y;
// constructors and the existing method
// for Point are not shown here due to limited space
11
How to invoke a static method, say method1 ?
• Within its own class : you may invoke it in a method directly by calling
method1 and passing arguments if any.
Note : Though you may invoke a static method as refer.method1 where refer is a
reference to an object of the same class (whose member is method1), but it is
considered a bad programming practice.
12
Example implementation
The files program1.java and Geometry package with altered Point class are on
the course webpage. Try it out.
13
Example2 of Static methods
Suppose you want to create a library of fundamental functions which canbe used
in your programs , so design a library of static methods.
14
Example1 of Static attributes
Recall the example of Bank account class where we had to assign different
account number to each customer. We could achieve it using static attribute
NextN. Please refer to the lecture held on Friday 26th September for its
significance and implementation details. This example highlights the power of
static attributes,
15
Example2 of Static attributes
You might like to create a class which has all the constants of math and physics.
16
Arrays
17
What if you have a large collection of identical data items which you want to
process.
Examples :
• Sort n numbers.
• Compute the diameter of a set of 100 points in 2-D space.
• Compute the smallest enclosing sphere for a set of 10000 points in 3-D
space.
18
What if you have a large collection of identical data items which you want to
process.
Examples :
• Sort n numbers.
• Compute the diameter of a set of 100 points in 2-D space.
• Compute the smallest enclosing sphere for a set of 10000 points in 3-D
space.
19
Arrays offers a solution
Array : An Object which is an ordered collection of data items. These data items
could be
• primitive types.
20
Array : declaration and creation
Memory
A
int[ ] A ;
?
21
Array : declaration and creation
Memory
A
int[ ] A ;
0 0 0 0
A = new int[4];
22
Array : declaration and creation
Memory
A
int[ ] A ;
0 0 0 0
A = new int[4];
23
Manipulating the contents of array A
• A[0] represent the first variable, A[1] the second variable, ..., A[3] the last
variable.
24
Array : Accessing and Manipulating elements of an array
Memory
A
int[ ] A ;
11 13 15 17
A = new int[4];
A [0] = 11;
A [1] = 13;
A [2] = 15;
A [3] = 17;
25
What will be the result of execution of last statement ?
Memory
A
int[ ] A ;
11 13 15 17
A = new int[4];
A [0] = 11;
A [1] = 13;
A [2] = 15;
??
A [3] = 17;
A [3] = A [2] −A [1]+ 7;
26
Treat each element of array as a variable
Memory
A
int[ ] A ;
11 13 15 9
A = new int[4];
A [0] = 11;
A [1] = 13;
A [2] = 15;
A [3] = 17;
A [3] = A [2] −A [1]+ 7;
27
Array of Points
Memory
A
Point[ ] A ; ?
28
Array of Points
Memory
A
Point[ ] A ;
A = new Point[3];
null null null
29
Array of Points
Memory
A
Point[ ] A ;
A = new Point[3];
Point
x=1
y=2
30
More on arrays
31
A nice problem to work during vacations
32
Design a class My integers
which could be used for storing all integers in the range of long and could support
the following arithmetic operations without worrying about overflow.
• Addition of My integers
• Multiplication of My integers
• Subtraction of My integers.
• Computing mt where m is a My integer number and t is an integer of type
byte.
33