[Bluej-discuss] bluej-discuss Digest, Vol 45, Issue 1

Stephen Bloch sbloch at adelphi.edu
Thu Mar 1 22:35:13 GMT 2007


Jason writes:
>Okay, my programme that calculates pi based on a number of terms 
>entered is nearly finished but all the reading and everything I've 
>done is not helping me to understand how to make variables cross 
>between classes.
>
>Wallis's infinite product series ; pi = 2 x 2/1 x 2/3 x 4/3 x 4/5 x 
>6/5 x 6/7...... etc.
>
>Confirms to the following class diagram
>
>Picalculator
>_________________
>pi:double=0.0
>_________________
>+main(args:String[]
>+getTerms():int
>+calculatePi( terms:int):double
>+displayPi(terms:int):void
>_________________
>
>
>'Method main() should call the other 3 methods in turn. All the 
>methods are static. Note what parameters are passed and returned, if 
>any. .... (stuff to do with how to programme the bit that calculates 
>pi that I've already done)....
>A static attribute pi is present to hold the value of pi; this can 
>be assigned either once at the end of the kmethod call/ calculation, 
>or repeatedly updated on each pass through the loop; note that the 
>attribute has class scope and therefore visible throughout the 
>class. Method displayPi() should simply use the value stored in the 
>attribute pi and display it to screen, along with the number of 
>terms used to arrive at this approximation and an appropriate 
>message.'

I agree with Andrea that this is a lousy example of object-oriented 
programming; if this is an assignment given by your instructor, your 
instructor is teaching 1960's-vintage procedural programming in a 
modern, object-oriented language.  Your instructor, alas, would be 
far from alone in this... :-(

The idea that the three non-"main" methods have to be called in a 
particular order particularly bothers me.  (displayPi won't produce a 
correct answer unless calculatePi has already been called, and 
calculatePi won't produce a correct answer unless getTerms has 
already been called.  Yecch!)
When you make a method public, you are allowing anybody in the world 
to call it whenever they choose; if it doesn't work correctly unless 
a particular other method has been called first, that's YOUR fault, 
not the fault  of the people who called it.  Public methods should be 
callable in any order, without affecting their correctness.

Andrea suggests an alternative view of the problem:
>Suppose you want to: create a PiCalculator, set the precision once and call
>the display() method.
>The first time you call display, the computation is carried out and you see
>the result, from
>the second time, you get the answer from the stored value PI in your object.
>If you set another precision (number of terms), the computation should be
>carried out again, next time you call the display().
>
>Suggestions:
>Avoid passing the precision to all methods: use a local variable.
>And make only the main to be static.
>
>In this way you could have 2 or 3 PiCalculators, set to different
>precisions, in your application, and display them repeatedly.

I like that approach.  I might go a step farther and get rid of the 
"display" method entirely.  If you're using BlueJ, you really don't 
need I/O until several months into a first programming course, 
because BlueJ allows you to call methods directly on specified 
parameters, and see the return values directly.

But what about "user-friendliness"?  Well, that depends on what 
"user" means.  The vast majority of methods in the world are not used 
by humans, but rather by other methods; the most "user-friendly" way 
to interact with such a "user" is NOT to take in input from the 
keyboard and print it to System.out, but rather to take in parameters 
and return values, as your "calculatePi" method does.

-- 
					Stephen Bloch
					sbloch at adelphi.edu


More information about the bluej-discuss mailing list