Radford University ITEC

ITEC120

lab quiz 1:
writing and calling functions

At the end of lab, submit your .java file via WebCT, under the assignment “lab-quiz1”. The deadline is 50 minutes after the hour, sharp! (I'll give 5-min and 2-min warnings.)
This lab-quiz is individual work, but you can use any notes you like, and you can use a web browser to access anything on the itec120 web page (previous labs, etc). Some handy ones might include lab01 and lab01-soln. Do not search the web for other people's programs. (And duh: no chat windows, IM, email, etc.)

As usual, you can use BlueJ to make a new Project and new Class, but you'll want to gut the template it gives you and replace it all with something like

class TempConverter {




  }
For each function you write, be sure to include (as usual):
  1. A short comment describing what the function takes in and what it returns (mention your input parameters by name; where applicable, mention what units they are in). That is, it should describe the meaning of the inputs and the return value, not just their type.
  2. Test cases.
    It's convenient to write these test cases in the same style you'd use to call the function:
      h(5) = 23   /* Well, instead of 'h', use your particular function name. */
      h(0) = 14
      ...
    
  3. The signature for the function (the name of the function, the name&type of any input(s), and the function's return type)
  4. The body of the function, which will consist of
    1. (optional) some partial results, stored into a local final variable,
    2. and
    3. a return statement which involves the function's input paramaters, any local variables you may have made, and any pertinent named constants.
  5. You'll want to check your test cases, to make sure your function really works as advertised. (If your function fails any test cases, indicate so in your comments.)
  6. Go back and double-check whether your functions meets the 4 laws of programming:
    1. Human-readable (good indenting, spacing, meaningful names);
    2. Include test cases, before you start coding.
    3. No repeated code.
    4. No magic numbers.


  1. 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).

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

    F(C) = 95 · C + 32

    Write a java function celcToFahr, which takes a temperature in °C, and returns that temperature expressed in °F.

  2. You read in a science article that the surface of the sun is 5700K (“kelvins”2), and that absolute zero is (conveniently) 0K. 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 0K (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.

    Write a function kelvToFahr to convert temperatures in Kelvins to temperatures in °F.
    Hint: For a test case -- what is 373.15K, in °C and in °F? What steps did you follow, to figure out the answer? Can you write code which follows those same steps?


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 Centigrade set 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.      back

2Interestingly, you don't write or say “degrees” with the Kelvin scale; the unit of temperature has just a one-word name rather than a two-word name.      back


©2006, Ian Barland, Radford University
Last modified 2006.Sep.21 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme