solution_of_3._create_a_spatializedtree_class
The Java training online
Solution: 3. Create a SpatializedTree class
Try to work with the helping elements before looking at the solution…
Helping elements
- class named
SpatializedTree
→ the source code must be saved in a file namedSpatializedTree.java
- use inheritance: the new class is a subclass of
Tree
:SpatializedTree extends Tree
- the
dbh
instance vaiable is inherited from theTree
superclass, do not add it again
- the two new instance variables for the tree location are
private double x;
andprivate double y;
- the
SpatializedTree
constructor takes 3 parameters:double dbh, double x, double y
- in the constructor,
dbh
is passed to the constructor of the superclass:super(dbh)
, thenx
andy
are saved in the instance variables with code like:this.x = x;
- it is possible to propose a single setter method for the two variables
x
andy
:setXY (double x, double y)
- the toString() method can be linked to the toString() method of the Tree superclass by writing: return super.toString() + “, x: ” + x + “, y: ” + y;
A possible solution
solution_of_3._create_a_spatializedtree_class.txt · Last modified: 2021/12/13 09:28 by 127.0.0.1