Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Displaying answer of a calculation  RSS feed

 
Kristoffer Van Strijdonck
Greenhorn
Posts: 9
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm an absolute beginner at Java and I'm trying to make a basic calculator. I have made two classes.

This code performs the mathematical operations:




And this is my main class:






Entering the first number, the second number and the operator seems to work but after my program did the calculation it doesn't display the answer on screen. What code do I need to add so it does?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristoffer Van Strijdonck wrote:after my program did the calculation it doesn't display the answer on screen


Of course not, because you don't tell it to.

. What code do I need to add so it does?


You already know the answer to that question. I know you do, because you're doing it elsewhere in your code.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Kristoffer Van Strijdonck
Greenhorn
Posts: 9
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:And welcome to the Ranch!


Thank you. And would it be something like this?

I still wouldn't know what to put after the "+" though. Am I heading in the right direction?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hint: it would probably be useful to *not* throw away the result that is returned from your calculator object.

 
Kristoffer Van Strijdonck
Greenhorn
Posts: 9
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Hint: it would probably be useful to *not* throw away the result that is returned from your calculator object.





This?

That just gives me: The answer is: null

 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still wouldn't know what to put after the "+" though. Am I heading in the right direction?

Yes.
Think about your code flow. You call a Calculator method passing in 2 values and then compute the answer which you return. Then what do you do with the returned value.

BTW It's bad practice to reference static methods from an instance variable you should use the class name. ie Calculator.add(var1, var2);
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
BTW It's bad practice to reference static methods from an instance variable you should use the class name. ie Calculator.add(var1, var2);


As an extreme example of why it's bad to do so, you're doing this, which is legal, but confusing:


In this case, as Tony suggested, using Calculator.add() (the class identifier for accessing static members) rather than looking like you're using a reference to access an object (which your not actually doing) will be an improvement.

However, in a normal OO setting, it would be more common--and usually more appropriate--to create a Calculator object and use a reference to that to call non-static methods. In this particular case, there's no state and no overriding, so there's no real need to create an object. I just don't want you do get into the habit of making everything static all the time.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristoffer Van Strijdonck wrote:


This?

That just gives me: The answer is: null


Right, because you're telling it to print out the Calculator object that the c variable points to, and c is null, so there's no Calculator object. But you don't want to print out your Calculator anyway. What you want to print out is the result of the method call, and, as already pointed out, you're just ignoring--throwing away--the value that your method returns.
 
Kristoffer Van Strijdonck
Greenhorn
Posts: 9
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've found the answer. Thanks guys.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing: It's not necessary to have those if statements where you check the operator the user has entered and then move to switch. In Java 7 you can use strings in your switch statement and check immediately the operator user has entered.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, if you are not using Java 7, move the calculator methods calls into the if statements (which should be if-else statements) and get rid of the opFlag altogether.
 
Kristoffer Van Strijdonck
Greenhorn
Posts: 9
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kemal Sokolovic wrote:One more thing: It's not necessary to have those if statements where you check the operator the user has entered and then move to switch. In Java 7 you can use strings in your switch statement and check immediately the operator user has entered.


I am using Java 7. I ended up doing this:

 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not what I meant. You could have:


And there is no need to do this:

You can assign the result of add method to result variable, not to duplicate the code. Also methods in your Calculator class are static, so you invoke them with Calculator.add(var1, var2), no need to create class instance.
 
Kristoffer Van Strijdonck
Greenhorn
Posts: 9
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does look more effective. Thank you, Kemal.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome, glad we could help.
 
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!