public void computeDailyHighSpan (Quote Q[]) {
// compute the span of the stock price on each day by means of a
// stack, and store the information in the Quote objects
int prevHigh; // closest preceding day when the stock price was higher
Stack D = new ArrayStack();
for (int i = 0 ; i < Q.length ; i++) { // process the current day i
while ( !D.isEmpty() &&
Q[i].getPrice() >= ((Quote) D.top()).getPrice() )
// note that if the first condition !D.empty() evaluates to
// false, then the second condition is not evaluated
D.pop();
if (D.isEmpty())
prevHigh = -1; // day i is a new high for the stock price
else
prevHigh = ((Quote) D.top()).getDay();
Q[i].setSpan(i - prevHigh); // compute and store the span
D.push(Q[i]); // add to the stack the current quote
}
}