solution_of_2._create_a_tree_class
The Java training online
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 beprivate
anddouble
- 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 calledgetDbh()
, it is public and returnsdbh
- to create an object of type
Tree
, usenew Tree (…)
and pass the expected argument
- If
t1
is a reference to a Tree object,System.out.println (t1.toString ())
orSystem.out.println (“” + t1)
print the result of itstoString()
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_of_2._create_a_tree_class.txt · Last modified: 2021/12/13 09:28 by 127.0.0.1