Formulas in methods or constructors?
Hi all. I've just started learning how to program and I have some very basic questions that I would be grateful if anyone could answer for me.
So, in my Java textbook there is a problem that asks me to implement a class that accepts the diameter and height of a cylinder in its constructor and has two methods that find volume and surface area, respectively. The class was actually very easy to implement but I checked the website for my textbook that has solutions to selected exercises and their answer was slightly different than mine.
Namely, they had used volume and surface area as their instance variables whereas I had used radius and height. Also, I had defined volume and surface area within my methods that returned volume and surface area where they had defined volume and area within the constructor. i.e. their getSurfaceArea method simply returned surface area.
My questions are, was the book's program more efficient or sound than mine? What exactly does it matter what you define as an instance variable and is it better to create formulas (in this case mathematical formulas) in a constructor?