CS 1401 Introduction to Computer Science
Fall 2012 Test 3, MW class section, December 3, 2012

Name: __________________________________________________________

Background: On this day, in 1911, the famous composer Nino Rota was born. He is best known for his music for the first two movies in the Godfather trilogy, and for the music to the 1968 Romeo and Juliet movie. This movie ends tragically; let us imagine what would have happened if both Romeo and Juliet lived happily ever after.

1. Upon receiving a monthly bank statement, Romeo and Juliet decided to check how well their bank manages their money. For that, they divide the interest by the initial amount; if the ratio is 0.02 or more, the result is good, if the ratio is smaller than 0.02, the result is not so good. Define a Java method bank(double interest, double initial) that, given the amount of interest and the initial amount, returns the statement "good" or "not so good". For example:

Use exceptions to make sure that when we try to apply this method to the case when no money was deposited, the method should return a meaningful error message. In the main program, apply this method to the above case of $1,000 and $25, to check whether the result is indeed good.









































2. During Romeo and Juliet's official wedding, their friends presented them with many beautiful roses, so many that they could not keep track. Let us design a program to help them count the roses. Let us assume that the best man has already placed rose amounts from individual guests into an array roses: Let guests store the number of guests. Write a method that, given an array a of integers and an integer m, computes the sum of the first m elements of this array. Trace your method step-by-step on the example when we had 3 guests and a 5-element array in which roses[0] = 12, roses[1] = 24, and roses[2] = 12.





















































3-4. The names of guests are stored, in alphabetic order, in an array guests.
  public static int binarySearch(String[] a, String elem){
    boolean found = false; int lower = 0; int upper = a.length - 1;
    int mid = 0;
    while(!found && lower <= upper){
      mid = (lower + upper) / 2;
      if(a[mid].equals(elem)){
        found = true;
      }
      else if (a[mid].compareTo(elem) > 0}{
        upper = mid - 1;
      }
      else{
        lower = mid + 1;
      }
    }
    if(found){
      return mid;
    else{
      return -1;
    }
  }
Here, a[mid].compareTo(elem) > 0 means that the string a[mid] follows the desired string elem in the alphabetic order.






































































































5. Design a class Guests describing wedding guests, each element of which contains three fields: the guest's name, age, and the number of roses the guest brought. Do not forget to add a constructor method and accessor ("get") and modifier ("set") methods. In the main program, create a new object with your own name.
































































6. Write a program that would help Romeo and Juliet generate nice thank-you letters to their wedding guests. This program should: The program should also count the number of guests and record it into the variable number.