• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can anyone explain this code ? as I don't understand that why the result is "8" !  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

What value do you think it should have? Which methods are called in which order, according to your understanding?
 
hardik vyas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused when MultiCalc extends SimpleCalc and both has value of +=7 and -=3, value*=multiplier gonna multiply which value, etc etc......
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, tell us which lines of code are executed in which order, and what value the variable has at each step. We can jump in with comments in case you get stuck. (I've added code tags to your code, so each line has a number to make this easier.)

To start with, initially the value is "0", because that is how int variables are initialized if no other value is given.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hardik vyas wrote:I am confused when MultiCalc extends SimpleCalc and both has value of +=7 and -=3, value*=multiplier gonna multiply which value, etc etc......

In the words of Jonesy: "Don't panic Mr. Mainwaring! Don't panic!".

Stop; take a deep breath; and LOOK at your code.

What is it doing? And start with the main() method (because that's what the JVM will do).

1. It creates a MultiCalc object. So: what will the value of 'value' be when that object is created?
2. It then calls compute(2) on that object. So: which method will it call?
3. Now look at that method and list out what IT does:
3a. It calls calculate(). So: which method is that, and what will the value of 'value' be when it finishes?
3b. It then calls super.calculate(). Same procedure again: which method is that, and what will the value of 'value' be when it finishes?
3c. It then calls value *= multiplier. What does that do, and what will the value of 'value' be when it finishes? (Hint: in order to work that out, you'll also need to know what the value of 'multiplier' is)
4. Finally, (back in main()) it calls System.out.println("Value is: " + calculator.value). What does that do?

And when you're doing this, I strongly suggest you get out a pencil and LOTS of paper.

Playing JVM can be tricky, but you'll never get anywhere by getting in a tizz.

Break things down, read code carefully, and take one step at a time.

HIH

Winston
 
Marshal
Posts: 56610
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch (again) You have got some good advice from Winston, and two out of three isn't bad. Not pencil and paper but pencil paper and eraser. You will make mistakes when you write things down and will have to destroy the evidence. But don't worry. That is a normal part of the beginner's learning process.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to debug the code in Eclipse or any java based IDE and try to find out the value for "value" at each step.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anindya Roy wrote:try to debug the code in Eclipse or any java based IDE and try to find out the value for "value" at each step.

I disagree - and I fear I won't be be the first.

IDEs are great, but they can make you lazy - or simply ignorant - particularly if you've never learnt any other way.

Learn how to work out what a program is doing without one first. In fact, I'd say, learn how to program in Java for at least a couple of months before you start using one.

It'll teach you a lot you wouldn't have learned otherwise - and it'll make you appreciate your IDE even more when you do start using one.

Winston
 
hardik vyas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much guys for help and specially Winston and Ulf !
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!