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:
So, this means that we are going to add the following validations:
xCoord
value must greater than or equal to zero and less than or equal to 800. If an incorrect value is supplied by the user, apply a default value of 400.yCoord
value must greater than or equal to zero and less than or equal to 700. If an incorrect value is supplied by the user, apply a default value of 350.diameter
value must greater than zero and less than 600. If an incorrect value is supplied by the user, apply a default value of 100.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);
}
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):