User Tools

Site Tools


solution_of_2._create_a_tree_class

The Java training online

Back to the table of contents

Solution: 2. Create a Tree class

Try to work with the helping elements before looking at the solution…

Helping elements

  • The new class is to be saved in the same directory training matching its package name
  • the dbh instance variable should be private and double
  • the constructor has the same name than the class: Tree and no return type
  • the constructor takes one parameter (or argument), it assigns the value of the parameter to the instance variable like this: this.dbh = dbh (this.dbh is the instance variable and dbh is the method parameter)
  • the getter for a variable named dbh is a method called getDbh(), it is public and returns dbh
  • to create an object of type Tree, use new Tree (…) and pass the expected argument
  • If t1 is a reference to a Tree object, System.out.println (t1.toString ()) or System.out.println (“” + t1) print the result of its toString() method to the terminal
  • To compile all classes of the training package in a terminal: from the java directory, type javac training/*.java

A possible solution

Solution to exercise 2. Code to be added in main Run it in the terminal

Back to the table of contents

solution_of_2._create_a_tree_class.txt · Last modified: 2021/12/13 09:28 by 127.0.0.1