SIBT COMP 125

Week 2 Tutorial Exercises

1. What does the following Eiffel program do ?

class MAIN
creation 
        make

feature
        make is
        local
                number : INTEGER
                i : INTEGER
        do
                io.read_integer
                number := io.last_integer
                from
                        i := 1
                until
                        i > number
                loop
                        if (number \\ i = 0) then
                                io.put_integer(i)
                                io.put_character(' ')
                        end
                        i := i + 1
                end
                io.new_line
        end
end
	

Try stepping through it assuming the number read in is 5 or 6.

2. Newton's method for finding the square root of a number k works as follows. Start with x = k. Figure out ((x^2)+k)/(2x) and use this for x. Put x back into the formula, and get a new x. Keep doing this, and the value of x approaches the square root of k

  1. Write Eiffel code to read a double from input and compute its square root by doing 100 iterations of Newton's method.
  2. Modify your code so that instead of doing 100 iterations, it keeps going until x^2 is within 1 millionth of k

3. If we were writing software for a bank, we might have an ACCOUNT class. What sort of features should an ACCOUNT have ?

Think about what sort of things we need to find out about accounts (functions), and what sort of things we need to do to accounts (procedures).