[Bluej-discuss] Help me with classes - I suck continued........

andrea Valente andrea270872 at gmail.com
Thu Mar 1 10:17:37 GMT 2007


Your programming style looks quite imperative/procedural, and not very
object-oriented...
Nothing wrong with it, but if your point is to get the idea of OO, you
should try more modeling tasks and less algorithmic ones.

For instance: take you program that calculates PI.
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 guess this should "force" you to think more with objects ;)
   Andrea


On 3/1/07, Jason Sharpe <jason.sharpe1 at ntlworld.com> wrote:
>
>  *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.' *
>
>
>
> cheer
>
> chopp188
>
> --MIMEStream_1+234892_7535315332737_16222073693--
> _______________________________________________
> mailing list bluej-discuss at bluej.org
> To unsubscribe or change your preferences, go to
> http://lists.bluej.org/mailman/listinfo/bluej-discuss
>
>


-- 
=============
Andrea Valente
www.aaue.dk/~av
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.bluej.org/pipermail/bluej-discuss/attachments/20070301/faa2ec0d/attachment.html


More information about the bluej-discuss mailing list