RU beehive logo ITEC dept promo banner
ITEC 120
2010fall
ibarland

homeinfolectslabsexamshws
tutor/PIsjava.lang docsjava.util docs

hw01
Temperatures on the Moon
a first function

due Sep.13 (Mon)

Instructions: Type into a BlueJ file, and submit both a hardcopy in class, plus electronically via Blackboard: Blackboard > Assignments > hw01 > Add attachment; attach TempConverter.java, which is inside the project folder (which in turn should be inside your itec120 folder inside your H drive).

Windows users: Windows Explorer hides the “.java” suffix, so if you see a file TempConverter with no suffix, that's really the .java file. Do not submit files with other suffixes (no “.class” or “.ctxt” files).

In some odd countries (namely Canada, and most of the rest of the world), temperatures are measured in degrees Celcius, instead of degrees Fahrenheit. So when you are traveling and you look in the newspaper and see a forecast for 22°C, it might sound chilly until a friendly Canadian citizen tells you that this is a balmy 71.6°F.

Some other notable temperatures1 are freezing (0°C, which is 32°F), and boiling (100°C, which is 212°F). (However: if you want to internalize metric, you're better off thinking in terms of new reference points than converting.)

The general case for converting degrees-Celcius into degrees-Fahrenheit is given by the arithmetic formula

F(C) = 95 · C + 32

Futurama: Fry and Leela chat with Moon farmer From Futurama The Series Has Landed:

Fry: Ooh, nighttime on the moon!
Old coot: It git down to minus one hundr'd, sev'nty-three.
Fry, worriedly: Fahrenheit, or Celsius?
Old coot: First one, then th' other.


  1. (0 pts) Make a new BlueJ project (named something like hw01, in the itec120 folder you made on your H-drive). Within the project, make a new class named TempConverter, delete all BlueJ's provided text, and paste in the following:
    /** Itec120 hw01.  
     * See http://www.radford.edu/itec120/2010fall-ibarland/Homeworks/hw01.html
     * @author your name here
     */
    class TempConverter {
    
    
    
    }
    
  2. (4pts) Write the signature for celcToFahr (and a stub body, so that you can compile the file.).
  3. (3pts) Create a test-driver function, which calls celcToFahr on 22, 37 and at least two other values. Be sure to compute and print the expected value as well. (Choose values which are easy to come up with answers w/o needing a calculator.)
  4. thermometer illustration

    You read in a science article that the surface of the sun is 5700K (“kelvins”2), and that absolute zero is (conveniently) 0 Kelvin. These numbers seem even more baffling, until that same cheerful Canadian tells you that to convert a temperature from Kelvins to °C, you simply subtract 273.15.
    So 0 K (absolute zero) is -273.15°C, which in turn is -459.67°F, brr.
    Similarly, 273.15K (freezing) is 0°C which (as we've seen earlier) is 32°F.

  5. (4pts) Write the signature (and a stub body) for kelvToFahr.
  6. (3pts) Add calls to your test-driver, checking that kelvToFahr correctly converts 5700 K to (approx) 9800°F, as well as testing three other values. Be sure to compute and print the correct, expected answer as well as what your function actually returns. (Choose values which are easy to come up with answers w/o needing a calculator.)

    This function must be inside the curly-brackets for class TempConverter, but it can be either before or after celcToFahr.

  7. (Update: an earlier version had a cut-and-paste error asking for an additional submission here; you'll actually only submit once after completing the following steps.)

  8. (3pts) Now, go ahead and complete the body of the function celcToFahr. That is, translate the arithmetic formula above into Java, just like we did for ufoHeightAt, pizzaArea, or bookstoreAd. For this function, we'll require that the body must be one single return statement, without any helper variables.

    Run the test cases you figured out in part (b).

    Careful: Because of how Java does arithmetic, 9/5 evaluates to 1. Instead, write 9.0/5.0. We'll discuss what's happening soon.
  9. (3pts)

    Finally, go ahead and complete the function kelvToFahr. For this function, we'll require that your solution must declare a helper variable and initialize it to something which not the final answer, but contributes to the final answer.

    Hint: If somebody gives you a temperature in Kelvins, converting that temperature into degrees Celsius is pretty easy. How can you then leverage code from the preceding problem, to convert that many degrees Celsius amount into degrees Fahrenheit?

    (Run your test driver, to make sure it works.)


1 One other intriguing test case is that -40°C = -40°F. Is it some mystical coincidence, that two scales exactly meet at such a nice round number, instead of some random number with a bajillion decimal places?

Well, it is a small bit of luck that the answer is an integer, but every integer celcius temperature corresponds to an even fifth-of-a-fahrenheit temperature, because of the 9/5 in the formula. Where does 9/5 come from?

The factor of 9/5 stems from the difference between freezing and boiling in the two systems: (212-32)/(100-0) = 180/100 = 9/5. Both scientists Fahrenheit and Celsius chose integers for the freezing and boiling of water, so that there'd be an integer number of notches on their thermometer between the two. This is the ultimate reason why conversions between the two systems use rational numbers, and not some long irrational real number arising randomly from nature.

If some martians came to Earth and (fascinated by all the ambient water) decided that they would invent a new temperature scale where the freezing point of water was declared as -27°Martian and the boiling point as 123°Martian, we'd still end up with a conversions which used fractions, not irrationals.

     

2Interestingly, you never say “degrees” with the Kelvin scale; the unit of temperature has just the one-word name “kelvins” rather than a two-word name “degrees fahrenheit”.      

homeinfolectslabsexamshws
tutor/PIsjava.lang docsjava.util docs


©2010, Ian Barland, Radford University
Last modified 2010.Sep.17 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme