Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: Write
|
Posted: Dec 10, 2002 3:39 PM
|
|
Here's something to get you started. You'll have to translate to Java, which is easy to do and good exercise for you. You'll also have to add the applet UI, but that is covered well in many previous posts as well as plenty of online turorials (starting with Sun's at http://developer.java.sun.com/developer/onlineTraining/).
Unfortunately, Java doesn't have properties (yet...?), but you can (okay, you must) just use the raw get & set methods.
Be sure and also implement the unit tests; you'll want to use the Artima TestKit (http://www.artima.com/testkit/) for that, of course. There is also a thingy called JUnit (http://www.junit.org/index.htm) that some people use for this purpose...
#----------------------------- Temperature.py -----------------------------
class Temperature(object):
def __init__(self, **kwargs ):
if kwargs.get( 'C', None ) != None:
self.__setC(kwargs['C'])
elif kwargs.get( 'F', None ) != None:
self.__setF(kwargs['F'])
else:
self.__F = 0
def __getF(self):
return self.__F
def __setF(self, F):
self.__F = F
def __getC(self):
return (self.__F - 32.0) * 5.0 / 9.0
def __setC(self, C):
self.__F = C * 9.0 / 5.0 + 32.0
F = property( __getF, __setF )
C = property( __getC, __setC )
#------------------------- TemperatureUnitTest.py -------------------------
import unittest,Temperature
class TemperatureTester(unittest.TestCase):
def setUp(self):
self.verbose = 0
def testSettingCelsius(self):
t = Temperature.Temperature( C = 0 )
self.failUnless( t.F == 32.0 )
self.failUnless( t.C > -0.01 )
self.failUnless( t.C < 0.01 )
self.failUnless( t.C == 0.0 )
def testSettingFahrenheit(self):
t = Temperature.Temperature( F = 0 )
self.failUnless( t.F == 0.0 )
# C is 17.77777...
self.failUnless( t.C < -17.7 )
self.failUnless( t.C > -17.8 )
# Howabout comparing floats like so:
self.failUnless( '%.4f'%t.C == '-17.7778' )
#------------------------------ Velocity.py -------------------------------
class Velocity(object):
def __init__(self, **kwargs ):
if kwargs.get( 'mph', None ) != None:
self.__setMph(kwargs['mph'])
elif kwargs.get( 'kph', None ) != None:
self.__setKph(kwargs['kph'])
else:
self.__kph = 0
def __getKph(self):
return self.__kph
def __setKph(self, kph):
self.__kph = kph
def __getMph(self):
return self.__kph / 1.609344
def __setMph(self, mph):
self.__kph = mph * 1.609344
kph = property( __getKph, __setKph )
mph = property( __getMph, __setMph )
#-------------------------- VelocityUnitTest.py ---------------------------
import unittest,Velocity
class VelocityTester(unittest.TestCase):
def setUp(self):
self.verbose = 0
def testSettingMph(self):
v = Velocity.Velocity( mph = 55 )
self.failUnless( abs(v.mph - 55.0) < 0.0001 )
self.failUnless( abs(v.kph - 88.51392) < 0.0001 )
def testSettingKph(self):
v = Velocity.Velocity( kph = 55 )
self.failUnless( v.kph == 55.0 )
self.failUnless( abs(v.mph - 34.17541557) < 0.0001 )
#------------------------------ WindChill.py ------------------------------
import Temperature, Velocity
def getWindChillEnglishStyle( temp, v ):
return 35.74 + 0.6215*temp.F + (0.4275*temp.F - 35.75)*pow(v.Mph, 0.16)
|
|