• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need to understand the Java Program Flow  RSS feed

 
John Natto
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am a Java begineer, and I have following code, and I am confused with the flow, could someone please let me know the flow, and basic logic so that it can help me understand similar program.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

First of all, indent your code properly. Without any indentation, it's much harder to see the structure of the code.

Also, that code doesn't compile. The Manager class contains two public static void main(String[] args) methods. You can't have two methods with the same name and the same parameters in one class. You'll need to fix that first.
 
John Natto
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesper,

Thanks for the response, and my apologies for typo.

I have corrected and corrected the indent. Looking for your response.

Thanks, John
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so what exactly is it you don't understand about how this program works? It's a pretty straightforward program without any obviously confusing control flow.

The program starts at the main() method, line 40. A number of Job objects are created in lines 42, 43, 44. These are put in an array (line 45), and then an Employee object is created, passing the array of Job objects. In lines 47 to 52 the print() method is called, three times.

One thing that is peculiar is the index variable in class Employee (line 17), and the nextJob() and hasNext() methods. The Employee object should not contain state about the iteration of Job objects - that's bad class design.

Note that the index variable isn't reset between the calls to print(), so for the second and third call you won't see any jobs printed.

I would remove the index variable and the nextJob() and hasNext() methods from class Employee, and just provide a getJobs() method that returns x. (Also, 'x' is a really bad variable name!).
 
John Natto
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks a lot for the response.

I am not able to understand the flow for Employee Class ( row 14 - 35 ), also the print method between ( row 55 - 60 ). Especially, if you can elaborate on a beginner level.

I will really appreciate for your help in this regard.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, let's have a look at the print method.
Do you know what a while loop does? It has this structure:
It will check the condition. If that is true, it carries out the action. And it continues - it keeps performing the action until the condition is false.

In this case, the condition is emp.hasNext(), which will return true if there are any remaining Jobs for that Employee. So it will repeat until there are no more jobs. The actions calls emp.nextJob(), and then prints some details from it. So all this is doing is printing out details of the next job. Put these together: it will print details of all the jobs for an employee, and stop when there are none left.

The code in Employee just holds an array of Jobs, and a pointer for which is the next one. It's then got two methods. One to check if there are any more Jobs, and one to get the next Job and move the pointer on one. These two methods are called by the while loop.

Is that clear?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!