Unit 3-1
Unit 3-1
// This class defines an integer stack // Push an item onto the stack // Pop an item from the stack
that can hold 10 values. int pop() {
void push(int item) {
class Stack { if(tos < 0) {
if(tos==9)
int stck[] = new int[10]; System.out.
System.out. println("Stack
int tos; println("Stack is full. underflow.");
// Initialize top-of-stack "); return 0;
Stack() { else }
tos = -1; else
stck[++tos] = item;
} return stck[tos--];
} }
}
This program generates the following output:
class TestStack { Stack in mystack1:
public static void main(String args[]) { 9
Stack mystack1 = new Stack(); 8
7
Stack mystack2 = new Stack();
6
// push some numbers onto the stack 5
for(int i=0; i<10; i++) mystack1.push(i); 4
for(int i=10; i<20; i++) mystack2.push(i); 3
2
// pop those numbers off the stack 1
System.out.println("Stack in mystack1:"); 0
for(int i=0; i<10; i++) Stack in mystack2:
System.out.println(mystack1.pop()); 19
18
System.out.println("Stack in mystack2:"); 17
for(int i=0; i<10; i++) 16
System.out.println(mystack2.pop()); 15
} 14
13
} 12
11
10
A Closer Look at Methods and Classes :
Overloading Methods
} double volume()
{ return width * height * depth;
}
}
Using Objects as Parameters
class PassOb {
// Objects may be passed to methods.
public static void main(String args[]) {
class Test { Test ob1 = new Test(100, 22);
int a, b; Test ob2 = new Test(100, 22);
Test(int i, int j) { Test ob3 = new Test(-1, -1);
a = i; System.out.println("ob1 == ob2: " + ob1.equals(ob2));
b = j; }
} }
// return true if o is equal to the
invoking object This program generates the following output:
boolean equals(Test o) { ob1 == ob2: true
if(o.a == a && o.b == b) return true; ob1 == ob3: false
else return false;
}
}
A Closer Look at Argument Passing
• In general, there are two ways that a computer language can pass an
argument to a subroutine.
• The first way is call-by-value. This approach copies the value of an
argument into the formal parameter of the subroutine. Therefore,
changes made to the parameter of the subroutine have no effect on
the argument.
• The second way an argument can be passed is call-by-reference. In
this approach, a reference to an argument (not the value of the
argument) is passed to the parameter. Inside the subroutine, this
reference is used to access the actual argument specified in the call.
This means that changes made to the parameter will affect the
argument used to call the subroutine.
• Java uses both approaches, depending upon what is passed.
A Closer Look at Argument Passing: Call by value
• Outside of the class in which they are defined, static methods and variables can
be used independently of any object. To do so, only specify the name of their
class followed by the dot operator. For example, to call a static method from
outside its class,
General form :
classname.method( )
final keyword:
• A variable can be declared as final.
• Doing so prevents its contents from being modified.
• This means that you must initialize a final variable when it is
declared.
• Examples:
final int FILE_NEW = 1;
final int FILE_OPEN = 2;
final int FILE_SAVE = 3;
final int FILE_SAVEAS = 4;
final int FILE_QUIT = 5;
Arrays Revisited
• Arrays are implemented as objects.
• Because of this, there is a special array attribute that you will want to
take advantage of.
• Specifically, the size of an array that is, the number of elements that
an array can hold is found in its length instance variable.
• All arrays have this variable, and it will always hold the size of the
array.