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:
 if Romeo and Juliet
invested $1,000 and received $25 of interest, so that the ratio
25 / 1,000 = 0.025 is larger than 0.02, then bank(25, 1000)
should return
"good";
 if Romeo and Juliet
invested $100 and received $1.50 of interest, so that the ratio
1.50 / 100 = 0.015 is smaller than 0.02, then bank(1.5, 100)
should return
"not so good".
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:
 roses[0] stores the number of roses presented by the
first guest,
 roses[1] stores number of roses presented by the
second guest,
 etc.
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
stepbystep on the example when we had 3 guests and a 5element
array in which roses[0] = 12, roses[1] =
24, and roses[2] = 12.
34. The names of guests are stored, in alphabetic order,
in an array guests.  Write a method for linear
search, and trace, stepbystep, how your method will search
for Macbeth in the array consisting of three names
Hamlet, Macbeth, and Othello.
 Explain, on the example of an
extended array consisting of six names Desdemona, Hamlet,
Lear, Macbeth, Ophelia, and Shylock,
how the following binary search
method will look for the name Macbeth.
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
thankyou letters to their wedding guests. This program should:
 ask Romeo and Juliet whether there are still more
thankyou letters to write, by
asking them to type Yes or No;

if there are more letter to write, the program should ask for
the name of the next guest and for the appropriate thankyou
statement. The program should record the names into an array
names and the thankyou statements into an array
thanks:
 the name of the first guest should be stored in the element
names[0], and a thankyou statement for this guest in the element
thanks[0];
 the name of the second guest should be stored in the element
names[1], and a thankyou statement for this guest in the element
thanks[1];
 etc.
The program should also count the number of guests and record it
into the variable number.