The Java training online

Back to the table of contents

2. Create a Tree class

Purpose: Write a complete class to describe a simple tree object.

Statement of the exercise:

  1. Add a new class in the training package, the class is named Tree, with a single instance variable named dbh, in centimeters (diameter at breast height is the main dimension of a tree for foresters). Add a constructor taking dbh in argument, a getter for the dbh variable and a toString() method displaying the tree dbh.
  2. Add code in the main method of exercise 1. to create a tree with dbh 5.6cm and print its toString() method to the terminal

Helper and solution

Back to the table of contents