• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need to understand the Java Program Flow

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic