Originally posted by Ernest Friedman-Hill:
Hi Beth,
It needs to use the "return" keyword to return a value. It should be the job of the code that calls getValue() to print the results. So getValue should end with "return total;" .
Originally posted by Ernest Friedman-Hill:
There are lots of things you could do to improve this and make it a better example of proper object-oriented principles. For example, member variables should almost always be -- with very few exceptions -- private. Instead of setting one class's member variables from another class, as you've done here, it's more appropriate to pass the values as arguments to a method. After making those members private, there are two different things you might do to get the program working again. First, you could give Carinderia a constructor that took two arguments, ans and qty, and set the member variables from those argments inside the constructor. The other idea would be to eliminate the member variables entirely, and instead make ans and qty be arguments to the getAmount() method. This second one actually makes more sense, since those variables are only used by one method, and there's no reason for the values to exist between calls to getAmount().
The other thing is the "one responsibility" principle. In general, a method should do one thing, and do it well. Your getAmount() method does two things: compute a value, and display a report about the value. If you left qty, ans, and total as member variables, then you might have two methods, computeAmount() and printReport(), which shared those member variables and the caller is intended to call first one, then the other. Another way to change things would be to have getAmount() not print a report, but instead use the "return total" I suggested earlier; then main() can capture the result in a variable and print the report itself.
This is all a good learning experience. You're welcome to come back and show us your changes for further discussion.
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|