[ Home Page ] [ Eiffel Archive ] [ Eiffel Classes and Clusters]

Arc de Triomphe clipart (2486 bytes)

Filtering Incomplete User Input


Written by Peter Horan.

School of Computing and Mathematics, Deakin University,
Geelong
Victoria
Australia, 3217

float_filter_v1.zip (5,143 bytes) - source code


The Problem

When entering numerical data in a text window, it is best to inform the user that it is invalid as soon as an unwanted character is entered. However, incomplete numerical strings can fail the tests "is_real" or "is_integer". For example, the strings "", "+" and "." fail. But these are legitimate prefixes of numerical strings.

In a similar manner, the expected input may be limited in range. For example, x >= x_min, x =< x_max. Checking these limits during data entry causes problems especially if the value x = 0 is not in range. For example, in setting an error percentage limit, x >= 0.01% and x <= 100% During entry, the set of prefixes representing x is {"0", "0.", "0.0"} all of which fall outside the permitted range. Hence, the need for routines which allow for these cases.

Class Features

The class FLOAT_FILTER has been developed to address these problems for real numbers. It has the following features:
   is_float_prefix(s: STRING): BOOLEAN
      require
         s /= Void
      ensure
         -- Result = "s" is a valid numerical prefix
      end

   definitely_smaller(s, lower_limit: STRING): BOOLEAN
      require
         lower_limit.is_real
            is_float_prefix(s)
         ensure
            -- Result = "s" is the prefix of 
            -- a numerical string < lower_limit
         end

   definitely_bigger(s, upper_limit: STRING): BOOLEAN
      require
         upper_limit.is_real
         is_float_prefix(s)
      ensure
         -- Result = "s" is the prefix of 
         -- a numerical string > lower_limit
      end

The routines "definitely_smaller" and "definitely_bigger" perform simple comparisons in the cases where "lower_limit" is negative or "upper_limit" is positive, respectively, or when the quantities contain no decimal point. However, when the numerical string contains a decimal point, the comparison must be done by comparing the input string with the limit string truncated to the same number of fractional digits. For example, s = 0.2 is not definitely smaller than lower_limit = 0.25 because it could be extended to s = 0.25 and be in range. That is, the call definitely_smaller("0.2", "0.25") should return False. However, definitely_smaller("0.1", "0.25") should return True.

Test Class Features

The test class FLOAT_FILTER_TEST is the root class. The feature "make" executes the tests.

The feature "check_it" implements the exception handling.

Comments

This submission is made because it responds to a user problem that is not evident until faced with actual examples and it may be of interest to others. The method of development relies on design by contract factors. Apart from this and perhaps using the class by implementation inheritance, I claim no special object oriented virtues.

A number of points are worth making.

Peter Horan
14 October, 1999


[ Home Page ] [ Eiffel Archive ] [ Eiffel Classes and Clusters]