Dave Drohan (SETU)

BACK NEXT

Validate Spot

At this stage, we can add and update the fields in the Spot class.

We will now add a bit of validation to these fields to make sure that we end up with appropriate data in them.

For example:

Validation rules for Spot

So, this means that we are going to add the following validations:

Implementing the Validation Rules for Spot

In Spot, update the field declarations to have the default values listed above:

    private float xCoord = 400;
    private float yCoord = 350;
    private float diameter = 100;

Now update the setXCoord to have the following validation:

    public void setxCoord(float xCoord) {
        if ((xCoord >= 0) && (xCoord <= 800)) {
            this.xCoord = xCoord;
        }
    }

Now try update the setYCoord and setDiameter methods yourself, using the validation rules above.

Lastly, update the constructor from:

    public Spot(float xCoord, float yCoord, float diameter) {
        this.xCoord = xCoord;
        this.yCoord = yCoord;
        this.diameter = diameter;
    }

to this (which calls the ‘set’ methods to apply the validation rules):

    public Spot(float xCoord, float yCoord, float diameter) {
        setxCoord(xCoord);
        setyCoord(yCoord);
        setDiameter(diameter);
    }

Run the app

Now run the app and test on the boundaries of these if statments. What this means is that xCoord should have tests for the following input:

This is called boundary testing as we are testing above, below and exactly the values in the if statement.

You can see we have input some boundary tests into our app (note how the default values are applied):