User Tools

The Java training online

Back to the table of contents

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 named
  • use inheritance: the new class is a subclass of Tree: SpatializedTree extends Tree
  • the dbh instance vaiable is inherited from the Tree superclass, do not add it again
  • the two new instance variables for the tree location are private double x; and private 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), then x and y 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 and y: 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

A SpatializedTree class Code to be added in main Compile and run it in the terminal

Back to the table of contents

solution_of_3._create_a_spatializedtree_class.txt · Last modified: 2021/01/26 15:03 by coligny