\documentclass{article}[10pt]

\usepackage{amsmath}
\usepackage{amssymb}

%\usepackage{graphics}

\setlength{\parskip}{8pt}
\setlength{\parindent}{0cm}

\begin{document}

\section*{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 \texttt{hw2} in your computer science
home directory (the same directory that contains \texttt{lab1},
\texttt{lab2}, etc.). Then create a file in that directory called
\texttt{MorePractice.java}.

You will write a batch of functions, all of which will be declared
\texttt{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, \texttt{Practice.java}, for guidance
as to how to structure the file.

\begin{itemize}

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

\item \texttt{min3}, like \texttt{max3}. Example: \texttt{min3(10,-4,22) -> -4}.

\item \texttt{med3}, which should return the median integer among three
integers. Example: \texttt{med3(10,-4,22) -> 10}.

\item \texttt{fact} 

The factorial function is recursively defined as follows:
\begin{itemize}
\item (assumption: n $\ge$ 0)
\item fact(0) = 1
\item fact(n) = n * fact(n-1), for n $>$ 0
\end{itemize}
Note that factorial is a partial function. Therefore there 
will be cause to throw an \texttt{IllegalArgumentException}.

\item \texttt{fib}

The Fibonacci numbers are the sequence \texttt{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:
\begin{itemize}
\item (assumption: n $>$ 0)
\item fib(1) = 1
\item fib(2) = 1
\item fib(n) = fib(n-2) + fib(n-1), for n $>$ 2
\end{itemize}
Write the function \texttt{fib} following this definition.

\end{itemize}

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

\begin{itemize}

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

\item Once logged on, start two programs: ``emacs'' under ``Editors'',
and ``terminal session'' under ``System''.

\item Edit the file in emacs, then click over to the ``terminal
session'' window. In the terminal session window, type \texttt{cd hw2}
to ``change to'' the directory \texttt{hw2}, which should contain your
\texttt{MorePractice} program. Type \texttt{javac MorePractice.java}
to make your program ready to run, then \texttt{java MorePractice} to
run it.

\end{itemize}

A last word: if you try running your program with \texttt{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 \texttt{ctrl-c} to interrupt it.

Submit your homework by typing \texttt{cd}, which will put you
in your home directory, and then \texttt{hwsubmit cs102 hw2}.

\hrulefill

\subsection*{Extra Credit Problem}

There is a non-recursive definition of the Fibonacci numbers which is
as follows. To find the $n$th 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
\texttt{binet} which consumes an integer and produces an integer
according to the given formula.

Use \texttt{Math.sqrt(double)} for square root, and
\texttt{Math.pow(double,double)} for raising one number to another.

(Example: \texttt{Math.pow(9.0, 2.0) -> 81.0}.)

\end{document}
