## CS 1401 Introduction to Computer Science Fall 2012 Test 2, TR 12-1:20 pm class section, November 13, 2012

Name: __________________________________________________________

Background: Today is the day of a total solar eclipse in Australia and New Zealand.

1. A student who turns in his or her second research report by Monday December 1 can potentially receive full credit (10 points), while a student who turns in the report by November 26 gets 3 extra points, so he or she can receive 13 points. Define a Java method points(int day) that, given a day in November, returns the number of points. In the main program, apply this method to compute the number of points for today (i.e., for day = 13).

```

```
2. Professional and amateur astronomers from all over the world have come to Australia and New Zealand to watch the solar eclipse. Help the organizers compute the total number of visitors by adding up the number of visitors who came on each plane. The number of people who came on each plane is stored in the array astr, so that:
• astr[0] stores the numbers of astronomers that came on the first plane,
• astr[1] stores the numbers of astronomers that came on the second plane,
• etc.
Write a method that, given an array of integers, computes the sum of all its elements. Trace your method step-by-step on the example of an array that has three elements: astr[0] = 100, astr[1] = 150, and astr[2] = 50.

```

```
3-4. The names of the astronomers are stored, in alphabetic order, in an array names.
• Write a method for linear search, and trace, step-by-step, how your method will search for Kreinovich in the array consisting of three names Kreinovich, Longpre, Roy.
• Explain, on the example of an extended array consisting of eight names Kader, Khan, Kreinovich, Longpre, Myers, Roy, Vazquez, and Vela, how the following binary search method will look for the name Longpre.
```  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 Visitor, each element of which contains three fields: the astronomer's name, the cost of his airticket to Australia or New Zealand, and the number of nights this astronomer plans to stay. 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. Students often have good ideas for test questions. Help us solicit good ideas for problems for tests. Write a program that would ask the users whether they want to contribute an idea by typing Y (meaning yes) or N (meaning no), and if yes, store this idea in the next element of the array ideas:
• the first idea is stored in the element ideas[0];
• the second idea is stored in the element ideas[1];
• etc., until the user says "no" (N).