• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't understand this Enthuware Explanation.  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would be the result of attempting to compile and run the following code?



This is in the Enthuware Test. The answer given is- Print 80.
Well the explanation says that Class C will call Class B which In turn Calls A,but then what is the reason for Class A calling B again? Plus it is calling with only one parameter.Which should cause error in my Knowledge.I don't know if i am right or not. That's why posting it here cause i need some help.
Capture.JPG
[Thumbnail for Capture.JPG]
This is the Explanation given of this question.
 
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi tushar parashar,

First of all, a warm welcome to CodeRanch!

tushar parashar wrote:Well the explanation says that Class C will call Class B which In turn Calls A,but then what is the reason for Class A calling B again?


Because of super.max(x, y) ; in the max() method of class B.

tushar parashar wrote:Plus it is calling with only one parameter.Which should cause error in my Knowledge.I don't know if i am right or not.


You are incorrect! Every invocation of each max() method occurs with 2 parameters: c.max(10, 20), super.max(x, y), and super.max( 2*x, 2*y). Could you tell us where you think the method is called with only one parameter?

Hope it helps!
Kind regards,
Roel
 
Saloon Keeper
Posts: 768
27
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • X Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tushar parashar wrote:but then what is the reason for Class A calling B again? Plus it is calling with only one parameter

class A is not calling B but returning value of y i.e. 40 on line no 9 in your code.
Below is the code of line no 9
Please go through below image,click on image to enlarge, see If you can understand by It.
ExecutionProcess.png
[Thumbnail for ExecutionProcess.png]
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • X Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ganesh Patekar wrote:Please go through below image,click on image to enlarge, see If you can understand by It.


Wow, that's an image with a lot of arrows HAve a cow for creating a diagram of the execution flow of this code snippet.

Here is a textual representation of the image, explaining the flow of execution (with some additional explanation when needed)
  • On line5 a value will be printed by System.out.println(). To know which value must be printed, c.max(10, 20) has to be executed first
  • Because the reference variable c is referring to an object of type C, the max() method defined in class C is executed with parameters x=10 and y=20 (line5)
  • The max() method of class C returns the result of the max() method defined in class B, so the max() method in class B will be executed with parameters x=20 and y=40 (line15)
  • The max() method of class B returns two times the result of the max() method defined in class A, so the max() method in class A will be executed with parameters x=20 and y=40 (line12)
  • The max() method of class A returns 40 because x=20 is less than y=40 (line9)
  • The return value of the previous step (40) is then used on line12 to calculate the return value of the max() method of class B. The return value will be 80 (2 * 40)
  • On line15 the return value of the previous step (80) is the return value of the max() method of class C too
  • On line5 System.out.println() is executed with the return value of the previous step (80), which (obviously) prints 80


  • Hope it helps!
    Kind regards,
    Roel
     
    Ganesh Patekar
    Saloon Keeper
    Posts: 768
    27
    Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Roel De Nijs wrote:Wow, that's an image with a lot of arrows

    I find drawing diagrams sometimes makes much easier to understand what is happening. Great detailed textual representation. Thank you for cow
     
    tushar parashar
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you to both of you. It is clear now. Thanks for investing your time in this. Cheers.
     
    Ganesh Patekar
    Saloon Keeper
    Posts: 768
    27
    Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!