I. Introduction
/**
The Java.util package contains the class Vector which conatins the
class Stack. That is the class Stack inherites the class Vector. In
Java literature, we write:
public class java.util.Stack extends java.util.Vector{constructors,
methods, coding, ...}
Forethermore, all the method of the class Vector can be used by
the class Stack:
public Stack(); (the empty constructor)
public boolean empty(); (Is the pile empty?)
public Object peek(); (Checks the object at the top of the pile)
public Object pop(); (pops up the object from the top of the pile)
public Object push (Object AnItem); (puts an item at the top of the pile)
public int search (Object AnObject); ( returns the rank of the object
AnObject starting from the top of the pile)
In this example:
println (ThePile); will print all theelement of the pile ThePile.
It uses toString() method from the super class Vector (the method
toString() doesn't exist in Stack class)
size(); comes from Vector class
push.(Object AnItem) comes from addElement of the Vector class
public Object push (Object AnItem){
addElement(AnItem);
return AnItem;
}
Stack inherits (extends) from Vector.
*/
II. The program
import java.util.*;
// package containing Vector class, then Stack class
public class TheStack {
static void ToPile (Stack<String> ThePile, String [] TheWeek) {//fil out the pile
//Castinh here with in order to avoid deprecation : Compile with -Xlint option.
for (int k = 0; k < TheWeek.length ; k++){
ThePile.push(TheWeek[k]);
}
}
static void Display (Stack ThePile, String info){
if(ThePile.empty())
System.out.println("\n\t" +info + ", the pile is empty. Its sise is: "
+ ThePile.size());
else {
System.out.println(info + " , the pile contains : \n\t" + ThePile + "\n");
}
}
public static void main (String [] args) {
String [] TheWeek = {//An array of the seven days of the week
new String ("Sunday"),
new String ("Monday"),
new String ("Tuesday"),
new String ("Wednesday"),
new String ("Thursday"),
new String ("Friday"),
new String ("Saturday")};
System.out.println("\n\tHi!, The last in, the first out");
Stack ThePile = new Stack();
Display (ThePile, "At first");// The pile is empty
ToPile (ThePile, TheWeek);//Filling out ThePile with the elements of
TheWeek, and display and give its size.
Display (ThePile, "\n\tAfter piling 7 days");
System.out.println ("\tThe size of the pile is " + ThePile.size());
System.out.println ("\n\tThe number of days from Wednesday " +
ThePile.search(TheWeek[2]));
System.out.println ("\n\tThe first element at the top of the pile is "
+ ThePile.peek());
System.out.println ("\n\tWe pop up the top of the pile, which is "
+ ThePile.pop());
System.out.println ("\n\tThe size of the pile is then " + ThePile.size());
System.out.println ("\n\tThe first element at the top of the pile is then "
+ ThePile.peek()+ "\n");
}
}//End of class TheStack
/*
III. Execution:
C:\Java>javac -Xlint TheStack.java
C:\Java>java TheStack
Hi!, The last in, the first out
At first, the pile is empty. Its sise is: 0
After piling 7 days , the pile contains :
[Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
The size of the pile is 7
The number of days from Wednesday 5
The first element at the top of the pile is Saturday
We pop up the top of the pile, which is Saturday
The size of the pile is then 6
The first element at the top of the pile is then Friday
C:Java>
/*
|