
%% (0) Download this document. Open with the Emacs text editor:
%%       emacs qa140-23jan2015.tex &
%%     As you type, end lines with a carriage return to
%%     keep lines of text from wrapping around.
%% (1) Replace ``Write your name here'' with your name.
%% (2) Develop and test your solutions to the problems in NetBeans
%%     and then cut and paste your answers into this document.
%% (3) Typeset this document by typing:
%%       pdflatex qa140-23jan2015.tex
%%     Typeset the document frequently. Add a little to
%%     your answers, typeset and check your work, and
%%     answer more questions.
%% (4) View the typeset document by typing:
%%       evince qa140-23jan2015.pdf &
%% (5) Submit your finished work on Moodle.

\documentclass[twosides]{article}
\title{Examination 2}
\author{Write your name here.}
\date{23 January 2015}

\usepackage{listings}
\lstset{language=java}
\usepackage{amsmath}

\newenvironment{answer}
  {\vspace*{0.2cm} \rule{12cm}{0.04cm} \vspace*{0.2cm}}
  {\vspace*{0.2cm}}

\begin{document}
\maketitle

\begin{enumerate}
  \item Write a method that, given an array of floating point
    values, returns the sum of those values.

  \begin{answer}
  \begin{lstlisting}{}

    // Write your answer here.

    \end{lstlisting}
    \end{answer}

  \item Write a method that, given an array of floating point values
    and a floating point value $x$, returns an array in which the $i^{th}$ element equals
    the $i^{th}$ element of the given array divided by $x$.

  \begin{answer}
  \begin{lstlisting}{}

    // Write your answer here.

    \end{lstlisting}
    \end{answer}

  \item Write a method that, given an array of floating point values,
    returns an array in which the $i^{th}$ 
    element equals the sum of the first $i$
    elements of the given array.

  \begin{answer}
  \begin{lstlisting}{}

    // Write your answer here.

    \end{lstlisting}
    \end{answer}

  \item Write a method that, given an array of floating point values and
    a floating point value $x$, returns the index of the first element in
    the array whose value is greater than $x$. If the array does not contain
    an element whose value is greater than $x$, then the array should return
    the length of the array.

  \begin{answer}
  \begin{lstlisting}{}

    // Write your answer here.

    \end{lstlisting}
    \end{answer}

  \item Supply the missing \lstinline+if+ statement
    to complete the definition of this method.

\begin{lstlisting}{}
public double findMinimum( double [] data ) {
  double bestGuessSoFar = data[0];

  for( int i = 1; i < data.length; i++ ) {

    // Need if statement here.

  } // for

  return bestGuessSoFar;
} // findMinimum( int [] )
  \end{lstlisting}

\begin{answer}
\begin{lstlisting}{}
// Write your answer here.
  \end{lstlisting}
  \end{answer}


  \item Supply the missing assignment statement to
    complete the definition of this method.

\begin{lstlisting}{}
public int findPositionOfMinimum( double [] data ) {
  int bestGuessSoFar = 0;

  for( int i = 1; i < data.length; i++ ) {
    if( data[i] < data[bestGuessSoFar] ) {

      // Need assignment statement here.

    } // if
  } // for

  return bestGuessSoFar;
} // findPositionOfMinimum( double [] )
  \end{lstlisting}

\begin{answer}
\begin{lstlisting}{}
// Write your answer here.
  \end{lstlisting}
  \end{answer}


  \item Write a method that returns \lstinline+true+
    if a given array of integers contains at least one
    element with a specified value and returns
    \lstinline+false+ otherwise.

    Use this method as a model. You can solve this problem
    by making small changes to this method.

\begin{lstlisting}{}
public int countMatches( int [] data, int valueToMatch ) {
  int count = 0;

  for( int i = 0; i < data.length; i++ ) {
    if( data[i] == valueToMatch ) {
      count++;
    } // if
  } // if

  return count;
} // countMatches( int [], int )
  \end{lstlisting}

\begin{answer}
\begin{lstlisting}{}
// Write your answer here.
  \end{lstlisting}
  \end{answer}


  \item Complete the \lstinline+for+ loop in this
    method. Be careful! The answer does not
    begin with \lstinline+int i = 0+.

\begin{lstlisting}{}
// Return true if each element in data
// is less than or equal to the element 
// that follows.
// Return false otherwise.
public boolean inOrder( int [] data ) {
  boolean result = true;

  for(                           ) {
    result = result && data[i - 1] <= data[i];
  } //

  return result;
} // inOrder( int [] )
  \end{lstlisting}

\begin{answer}
\begin{lstlisting}{}
// Write your answer here.
  \end{lstlisting}
  \end{answer}


  \item Write a method that computes the weighted
    average of two floating point numbers. This method
    will have three parameters: the two numbers to be
    averaged and the weight. The weight will be a value
    in the range $0.0$ to $1.0$. The method will return
    a floating point value to its caller.

    If $a$ and $b$ are the numbers to be averaged
    and $t$ is the weight, then the weighted
    average is\ldots

  \begin{align*}
    weighted-average & = (1 - t) \cdot a + t \cdot b
    \end{align*}

  \begin{answer}
  \begin{lstlisting}{}

    // Write your answer here.

    \end{lstlisting}
    \end{answer}

  \item I wrote a class that models fractions and
    then scrambled the order of the lines of code.
    Reorder these lines to make the class named Rational. 

\begin{lstlisting}{}
line # 0:   } // Rational( int, int )
line # 1: public class Rational {
line # 2:   private int num;
line # 3:       return a;
line # 4:     if( b == 0 ) {
line # 5:     } // if
line # 6:   } // gcd( int, int )
line # 7:   private int den;
line # 8:     return new Rational( n, d );
line # 9:   } // toString()
line #10:     this.den = den/gcd;
line #11: } // Rational
line #12:   public Rational( int num, int den ) {
line #13:   public String toString() {
line #14:   public Rational add( Rational r ) {
line #15:     } // else
line #16:     int d = this.den * r.den;
line #17:     this.num = num/gcd;
line #18:   private int gcd( int a, int b ) {
line #19:   } // add( Rational )
line #20:     else {
line #21:     int gcd = gcd( num, den );
line #22:     int n = this.num * r.den + this.den * r.num;
line #23:       return gcd( b, a % b );
line #24:     return num + "/" + den;
  \end{lstlisting}

  \begin{answer}
  \begin{lstlisting}{}

    // Write your answer here.

    \end{lstlisting}
    \end{answer}

  \end{enumerate}

\end{document}
