I'm writing a little app to calculate the roots of quadratic equations. The
first step is to build the model and then I would like to link this model
with a Spec GUI.
So far the model is almost done (I have to solve yet imaginary roots).
Would you consider this good code?
Would you change something? or re-arrange the code?
This is what I have so far:

"The idea is to have a class to calculate quadratic equations.
This first draft only calculates real roots (leaving aside imaginary roots).

A QuadraticEquation is a class used to solve Quadratic Equations of the
form: ax^2 + bx + c = 0, where a is not 0.

You can set the coefficients one by one using:
self quadraticCoefficient: aNumber
self linearCoefficient: aNumber
self constant: aNumber."

Object subclass: #QuadraticEquation
        instanceVariableNames: 'quadraticCoefficient linearCoefficient constant
checkTerm root1 root2'
        classVariableNames: ''
        category: 'IS-Math'

"I create the accessors and then the following methods"

QuadraticEquation>>calculateRoots
        checkTerm := ((linearCoefficient  squared) - ( 4 * quadraticCoefficient 
 *
constant)) .
        self checkNegativeSqrt.
        
QuadraticEquation>>checkNegativeSqrt
        self checkTerm  >= 0 
                ifTrue: [ 
                        root1 := ((linearCoefficient  negated) + (checkTerm 
sqrt)) / (2 *
quadraticCoefficient).
                        root2 := ((linearCoefficient  negated)  - (checkTerm 
sqrt)) / (2 *
quadraticCoefficient) ]
                ifFalse: [ ^ 'Error: Negative square root'].
                        
                        
"Example:  -3x^2+2x+2=0
                        root1 =  -0.5485837703548636
                        root2 =   1.2152504370215302

print(it)

| anEquation |
anEquation := QuadraticEquation new.
anEquation quadraticCoefficient: -3. 
anEquation linearCoefficient: 2. 
anEquation constant: 2.
anEquation calculateRoots.
anEquation root1. 

Then change the last line to 'anEquation root2' to get the other root."

Thanks so much for the input.
Best regards
Nacho




-----
Nacho
Smalltalker apprentice.
Buenos Aires, Argentina.
--
View this message in context: 
http://forum.world.st/Question-on-style-tp4752165.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.

Reply via email to