Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Recursive linking question...

I'm in a CS course of Java and there is a problem I need a little help unwrapping (the text and code is posted below). I have two questions:

  1. In the text, emp is called a variable of type Employee. What is meant by this? Is emp an instance of class Employee?

  2. What is the recursive part of this code? I know that the supervisor is also an employee. But how is that recursion? Sorry if that does not make sense I'm just trying to wrap my head around the question

/**
* An object of type Employee holds data about one employee. */
public class Employee {

  String name; // Name of the employee. 
  Employee supervisor; // The employee’s supervisor.
.
. // (Other instance variables and methods.) .
} // end class Employee

If emp is a variable of type Employee, then emp.supervisor is another variable of type Employee. If emp refers to the boss, then the value of emp.supervisor should be null to indicate the fact that the boss has no supervisor. If we wanted to print out the name of the employee’s supervisor, for example, we could use the following Java statement:

  if ( emp.supervisor == null) {
    System.out.println( emp.name + " is the boss and has no supervisor!" );
  } else {
    System.out.print( "The supervisor of " + emp.name + " is " );
    System.out.println( emp.supervisor.name ); 
}

2 Answers

Hi there...

I hope you are not had enough of me yet. I really hope you can show more parts of the code but using what is available allow me try to elaborate some things. Okay first question:

1.In the text, emp is called a variable of type Employee. What is meant by this? Is emp an instance of class Employee?

Answer is: Yes, and so does supervisor for that matter. Meaning emp object and supervisor object have similar attributes and methods.

  1. What is the recursive part of this code? I know that the supervisor is also an employee. But how is that recursion? Sorry if that does not make sense I'm just trying to wrap my head around the question

Answer: this is still hard to determine since I do not know how object supervisor behaves inside the Employee class, but from the passage you gave me this looks like a recursion:

if ( emp.supervisor == null) {
    System.out.println( emp.name + " is the boss and has no supervisor!" );
  } else {
    System.out.print( "The supervisor of " + emp.name + " is " );
    System.out.println( emp.supervisor.name ); 
}

From the looks of it this program is intended to show the position of an individual (in this case emp) inside a corporation up all the way to the top boss. The output of this program should looks like this:

the supervisor of emp is A

the supervisor of A is B

..... All the way to someone is the the boss and has no supervisor

please remember this is just me guessing okay. I still unclear how the code works since so little information about the class Employee and also whether the if statement is part of larger code.

How come this is a recursion? Well recursion must fulfill two things:

  1. A simple base case (or cases)—a terminating scenario that does not use recursion to produce an answer

  2. A set of rules that reduce all other cases toward the base case. Although I am not a fan of using the word reduce. I like move all other cases toward the base case.

Okay let's go back to your case. Which part is the base case? Well this is the base case:

if ( emp.supervisor == null) {
    System.out.println( emp.name + " is the boss and has no supervisor!" );

As you can see in the result, when you find out the top boss the program stop since there is no other supervisor above the boss. Therefore the base case (in this case finding the boss) terminate the recursive process.

Meaning that leaves this part of the code as set of rules that move all cases to the base case:

else {
    System.out.print( "The supervisor of " + emp.name + " is " );
    System.out.println( emp.supervisor.name ); 

CAUTION: I DO NOT KNOW FOR SURE HOW THE COMPLETE CODE WORKS

but form the available code if the emp still has a supervisor it will print thi "The supervisor of (emp) is.." and then the second println command will print the name of the supervisor.

In the process of println printing it called Object called suervisor which is an instance of Employee. meaning it has name and also supervisor.

Then the supervisor called also undergo the same process and **if the supervisor still have supervisor (meaning supervisor is not null) the supervisor's supervisor will undergo the same process also.

This process goes all the way until the program finds Object instance of Employee who has supervisor = null meaning he/she is the boss of the company

and the program ends. In other word this part of the code drives (or moves) the result of the program closer and closer to the boss. Sounds familiar?

Please mind you I do not have complete understanding of the code since you only give the header of the code and not even a construction method of the class.

I hope this can help a ilittle.

This explains things perfectly. I can not thank you enough for assisting me these past few days. Recursion has caused me many "head scratchers". You are the best!