RU beehive logo ITEC Department logo
ITEC 120
2007spring
ibarland,
jpittges

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

hw01
hw01
calling functions (from functions)

Due Date: Jan.22 (Mon.), 17:00. 1

Instructions:

  1. (2pts) For each of the following pieces of information, what is the most appropriate Java type to represent them? (The Java types we have covered are int, double, String).
    1. The number of PS-3s in stock, at a certain store.
    2. The current sale price of a PS-3 at that store, in dollars.
    3. The current sale price of a PS-3 at that store, in cents.
    4. The serial number of a particular PS-3 (something like 4QJ7B93).
  2. (3pts) Suppose class Blizgrffblop contains a method with whose signature is
      int fizzle( String s )
    
    If bz is an instance of class Blizgrffblop,
    1. Call fizzle, passing it the string "hmmm".
    2. Call fizzle, passing it the empty string.
    3. Call fizzle, passing it the empty string, and dividing the result by 12.3.
  3. (1pt) Write the signature for a method named flarg, which takes in a (possibly-fractional) number, and returns a number (which is always a whole number).
  4. (1pt) Write the signature for a method named shlark, which takes in a two pieces of text, and returns a (possibly fractional) number.
  5. (3pts) Conventions and comments:
    1. True/False: in the examples in lecture, the name of each class starts with a capital letter.
    2. True/False: in the examples in lecture, the name of each function within a class starts with a capital letter.
    3. A Javadoc comment begins with what special 3-character sequence?                 
    4. What Javadoc “@” keyword indicates that you are describing one of the parameters of the function?
    5. What immediately follows that keyword?
  6. (8pts) 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 temperatures2 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.

    Careful: Because of how Java does arithmetic, 9/5 evaluates to 1 (recall hw00). Instead, write 9.0/5.0. We'll discuss what's happening in a week or two.

    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.


  7. (8pts) thermometer illustration

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

    Write a function kelvToFahr to convert temperatures in Kelvins to temperatures in °F.
    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?

    Be sure to spend time getting your test cases right; they will help you figure out how to solve the general problem.

    Your function can either have just a single return statement, or it can use a named constant local variable to store an intermediate result (presumably: the temperature in °C), before calculating and returning the final result.

  8. (4pts) Write kelvToFahr_v2, which gives exactly the same answers as kelvToFahr, except that it uses a local variable (if your first version didn't), or doesn't use a local variable (if your first version did). Follow the syntax in class for local variables: one line declares the variable, and the very next line initializes it.

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


For these last problems, where you write functions, put them inside

class TempConverter {




  }
When writing a function, remember there is a process to follow:
  1. Write 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
      ...
    
  2. Then write the signature for the function (the name of the function, the name&type of any input(s), and the function's return type), followed by…
  3. Then write the javadoc comment (including a short one-line description, an @param line, and an @return line.)
  4. Finally, write the body of the function (which will include return expression;)
  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 Laws of Programming:
    1. No repeated code. (Only one place in your code should mention 9.0, 5.0, and 32.)
    2. Human-readable (good indenting, spacing, meaningful names);


1But you are expected to finish homeworks over the weekend and bring them to class on Monday morning; the 17:00 deadline is there in case you run in to unexpected snags, there is still a chance to come to office hours before the due-date.      

2 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.      

3Interestingly, 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.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2007, Ian Barland, Radford University
Last modified 2007.Aug.27 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme