The Artima Developer Community
Sponsored Link

Java Answers Forum
Write

1 reply on 1 page. Most recent reply: Dec 10, 2002 3:39 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Candace Eichor

Posts: 6
Nickname: candee
Registered: Dec, 2002

Write Posted: Dec 9, 2002 9:52 PM
Reply to this message Reply
Advertisement
Write an applet that accepts temperature (F) and wind speed (MPH) from text fields. Calculate the wind chill and display it in another text field or text area. There should be a calculate and clear button. The formula for calculating wind chill is as follows:

Wind Chill = 35.74 + 0.6215(T) - 35.75(V ^ 0.16) + 0.4275(T)(V ^ 0.16)

Where V = wind velocity in MPH and T = temperature in Fahrenheit
-----------------------------------------------------------

Modify the above applet to accept temperature in both Fahrenheit and Celsius based on checkbox group C and F and wind speed in both MPH and KPH (Kilometers Per Hour) also based on checkbox group. The user will select the appropriate checkbox indicating the unit of measure for both temperature and wind velocity.


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Write Posted: Dec 10, 2002 3:39 PM
Reply to this message Reply
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)

Flat View: This topic has 1 reply on 1 page
Topic: delete values from a hastable Previous Topic   Next Topic Topic: swing, reflection

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use