15110 Spring 2012 [Cortina/von Ronne]

Lab 1 - due Friday, May 25th

Deliverables

  1. force.rb (demonstration)
  2. circle_area.rb (demonstration)
  3. answers.txt
  4. sphere_vol.rb
  5. my_func.rb (time permitting)

Demonstration

Activities

  1. Type each of the following expressions into irb. What value do each of the following Ruby expressions evaluate to? Is that value an integer or a floating point?

    1. 250
    2. 28 % 5
    3. 2.5e2
    4. 3e5
    5. 3 * 10**5
    6. 20 + 35 * 2
      Why is this different from (20 + 35) * 2?
    7. 3.0 * 2 / 3
    8. 2 / 3 * 3.0
      Why is this different from 3.0 * 2 / 3?
    9. 25 - 5 * 2 - 9
      Is this different from ((25 - 5) * 2) - 9 and/or 25 - ((5 * 2) - 9)? Why?

    Write your answers in the file answers.txt. (Review Unit 2A lecture slides if necessary.)

  2. In sphere_vol.rb, define a Ruby function sphere_vol(r) that calculates and returns the volume of a sphere with a radius r. This can be calculated using the formula:

    \[ V = \frac{4}{3}\pi r^3\]

    Place in answers.txt a copy of irb lines in which you call sphere_vol(r) to compute the volume of sphere with a radius of 7 and irb shows you the result.

  3. Pick a mathematical formula that is used in your major. In a file called my_func.rb, create Ruby function that calculates some value using that formula.

    At the beginning of your my_func.rb include a comment (one or more lines of English text that start with "#" and are ignored by irb) that describes what the fomula the function computes and what units the parameters and results are in.