/****************************************************************************
*
* ConstPredictor - class for recognizing and predicting a constant sequence.
*
* Purpose: COMP2011 Project 1, Session 1 2006
* Topic: Sequence recognition and prediction
* Author: Alan Blair
* Date: March 2006
*
* Note: You should not edit this file;
* you should only edit the following files:
*
* ArithmeticPredictor.java
* GeometricPredictor.java
* QuadraticPredictor.java
* HomogeneousPredictor.java
* RecurrentPredictor.java
*/
public class ConstantPredictor extends Predictor {
private boolean inconsistent = false;
private double a;
private int n; // number of items
/**
@return name of the type of sequence ("Constant").
*/
public String toString() {
return( "Constant" );
}
/**
Store the first item in the sequence,
and test all subsequent items to see if they are equal to it.
*/
public void step( double u ) {
if( n == 0 ) {
a = u;
}
else if( u != a ) {
inconsistent = true;
}
n++; // increment the number of items
}
/**
@return true if we have seen at least one item,
and all the items have been equal; false otherwise.
*/
public boolean canPredict() {
return(( n > 0 )&& !inconsistent );
}
/**
@return the value of the first item
(if there are not enough items,
or the items are inconsistent, return zero).
*/
public double predict() {
if( canPredict()) {
return( a );
}
else {
return( 0.0 );
}
}
}