Skip to main content

Attentiveness Evaluation

Posted by malenkov on July 16, 2010 at 4:24 AM PDT

The attentiveness evaluation test in my driving school was easier than one I developed by using JavaFX. I made it harder by adding the next value randomly.

The test data are stored in the PersistentProperties class that combines the Storage and the Properties classes. When you create its instance, all the key-value pairs are loaded from the specified storage. This class provides utility methods to convert a string value into the expected type. For example,

  public function getInteger(key, default: Integer) {
    var value = get(key);
    if (value != null) try {
      return Integer.parseInt(value)
    catch (exception) {

This method takes a string value for a given key and tries to parse it as an Integer value. If the value is not found or it could not be parsed, the method returns the default value. I think, it might be helpful to have such utility methods in the Properties class. The PersistentProperties class is used in this test as well as in the previous one. The application model based on this class is used to store the data when they are changed. Study the PersistentModel class in detail.

public class PersistentModel {
  def properties = PersistentProperties {
    source: "properties.txt"

First, the data storage is initialized.

  public var misses = properties.getInteger("misses", 0) on replace {
    if (isInitialized(blocker)) {
      properties.put("misses", misses);

Then the misses variable of the Integer type is declared. Note that when you change this variable, its new value is stored in the repository. The following simple method is designed to avoid overwriting the repository during class initialization. The blocker variable is declared as the last variable. It is not initialized during the initialization of other variables. So, during the initialization of the misses variable, the isInitialized(blocker) method returns false. Instead of the blocker variable, it is possible to use any class variable that is declared after this one.

  public-read var passes = properties.getInteger("passes", 0);

The passes variable is declared with the public-read modifier and changes only within the PersistentModel class. Its all changes are made by the model, so that it is possible to omit the 'on replace' block.

  public-read var millis: Long;
  var millisAll = properties.getLong("millis", 0) on replace {
    if (passes > 0) {
      millis = millisAll / passes

The millis variable is used for the average time. However, it is necessary to know all the spent time and the number of the passed attempts to calculate it. Therefore, the auxiliary millisAll variable is declared and the average time is recalculated when it is changed.

  public-read var errors: Long;
  var errorsAll = properties.getLong("errors", 0) on replace {
    if (passes > 0) {
      errors = errorsAll / passes

The errors variable is used similar to the millis variable.

  public function pass(millis: Long, errors: Long) {
    properties.put("passes", ++passes);
    properties.put("millis",   millisAll += millis);
    properties.put("errors",   errorsAll += errors);

The pass function is used to change several variables simultaneously and to store their new values in the storage.

  var blocker = true;

Note that using JavaFX storages solves the problem with the unsigned JNLP applets, that I discovered earlier.

original post

Related Topics >>