CMSC 10200: Homework 2

assigned: June 22, 2005; due: June 24, 2005 (before 5 PM)

The following homework assignment should afford some more practice writing functions along the lines of the first lab.

First, create a folder called hw2 in your computer science home directory (the same directory that contains lab1, lab2, etc.). Then create a file in that direc-

tory called MorePractice.java.

You will write a batch of functions, all of which will be declared static as in the lab, for reasons which will be discussed later in the course.

Write the following five functions, and write tests for them as well. Use the first lab exercise, Practice.java, for guidance as to how to structure the file.

• max3, which should take three integers and return the largest one. (Note there is a clever way to accomplish this with the help of max2 from the lab.) Example: max3(10,-4,22) -> 22. Note that “logical and” is && in Java, and can be used like the double-bar “logical or” symbol ||.

• min3, like max3. Example: min3(10,-4,22) -> -4.

• med3, which should return the median integer among three integers. Ex-

ample: med3(10,-4,22) -> 10.
• fact

The factorial function is recursively defined as follows:

– (assumption: n ≥ 0)

– fact(0) = 1

– fact(n) = n * fact(n-1), for n > 0

Note that factorial is a partial function. Therefore there will be cause to

throw an IllegalArgumentException.
• fib

The Fibonacci numbers are the sequence 1, 1, 2, 3, 5, 8, ... where the first two numbers are 1 and all numbers thereafter are the sum of the two previous Fibonacci numbers. We can define a function to give the nth Fibonacci number as follows:

– (assumption: n > 0)

– fib(1) = 1

– fib(2) = 1

– fib(n) = fib(n-2) + fib(n-1), for n > 2

Write the function fib following this definition.

If you decide to work in the CS lab, here are a few reminders as to how to go about it:

• Choose a Linux machine (as opposed to a Mac). Before logging on, click the question mark that says “Session”, and choose item 5: KDE.

• Once logged on, start two programs: “emacs” under “Editors”, and “terminal session” under “System”.

• Edit the file in emacs, then click over to the “terminal session” window. In the terminal session window, type cd hw2 to “change to” the directory hw2, which should contain your MorePractice program. Type javac MorePractice.java to make your program ready to run, then

     java MorePractice to run it.

A last word: if you try running your program with java MorePractice and it doesn’t finish running, it may be caught in an infinite loop. If your program doesn’t finish running in a second or so, you can use ctrl-c to interrupt it.

Submit your homework by typing cd, which will put you in your home directory,

and then hwsubmit cs102 hw2.

Extra Credit Problem

There is a non-recursive definition of the Fibonacci numbers which is as follows. To find the nth Fibonacci number, where n > 0, use the formula

\frac{(1 + \sqrt{5})^{n} − (1 − \sqrt{5})^{n}}{2^{n}\sqrt{5}}

This is known as Binet’s Fibonacci Number Formula. Write a function binet which consumes an integer and produces an integer according to the given formula.

Use Math.sqrt(double) for square root, and Math.pow(double,double) for

raising one number to another.

(Example: Math.pow(9.0, 2.0) -> 81.0.)