## CS 1401 Introduction to Computer Science Fall 2012 Final Exam, TR 12-1:20 pm class section, December 11, 2012

Name: __________________________________________________________

Background: On this day, in 1972, exactly forty years ago, astronauts from Apollo-17 mission landed on the moon. This was the last lunar landing.

1. Apollo-17 spaceship used primitive computers, in which only integer arithmetic operations were hardware supported. To test a computer, astronauts:

• asked the computer to calculate the value of a simple arithmetic expression,
• calculated the value of this expression by hand, and
• compared the results.
Help the astronauts by computing, step-by-step, the value of the following expression (as Java would compute it):
3 / 2 / 2 + 3 / (2 / 2).

```

```
2. Each Apollo mission considered several possible landing sites. For each site, scientists estimated how interesting this site will be, while engineers estimated how difficult it will be to land in this site. The Mission Control then selected a site for which the difference between the interest score and the difficulty score is the largest. Write a method that, given two numbers, returns their difference.

```

```
3-5. Let us first consider the case when there are three possible landing sites.
• The first site has scores interest1 and complexity1.
• The second site has scores interest2 and complexity2.
• The third site has scores interest3 and complexity3.
All these values are given.
• Write a piece of code that uses these values and if-then statements to compute the number (1, 2, or 3) of the best site -- i.e., of the site which has the largest value of the difference.
• One possible way to describe the conditions of the corresponding if-then statements is to use and-operation &&. Draw the truth table that describes the values of A && B for all four possible combinations of truth values of A and B.
For extra credit: draw similar tables for "or" and "not".
• Describe combinations of values that you can use to test your code. Explain why you selected these values; what is the general principle of testing a code that contains if-then statements?
For extra credit: describe other rules of testing; explain the difference between black-box and white-box testing.

```

```
```

```
```

```
6. We may have more than three possible landing sites. In this case, it is reasonable to store the scores of different sites in two arrays:
• the i-th element interest[i] of the array interest is the interest score of the i-th site;
• the i-th element complexity[i] of the array complexity is the complexity score of the i-th site.
Write a method that, given the two arrays interest and complexity, returns the index i for which the difference interest[i] − complexity[i] is the largest. Trace your method on the example of the following two sites:
• site 0 with interest score 5 and complexity score 3, and
• site 1 with interest score 4 and complexity score 1.

```

```
7. To compute the speed of the spaceship, the program divided the distance d by the time t that it took to travel this distance. Define a Java method velocity(double d, double t) that computes this velocity. Use exceptions to make sure that when we apply this method to the case when t = 0, we get a meaningful error message. What is the main advantage of exceptions?

```

```
8-9. As the Apollo-13 flight has shown, spaceflights are risky. To help astronauts, Mission Control prepared a manual describing what to do if each components of the spaceship is malfunctioning. The instructions are placed in an array instructions, with elements instructions[0], instructions[1], ... The names of the corresponding components are stored, in alphabetic order, in the array names. Given the name of the component, we need to find its index i in the array names, and then print the corresponding instruction instructions[i].
• Write a method for linear search, and trace, step-by-step, how your method will search for the index of parachute in the array consisting of three components engine, parachute, and radio.
• Trace, on the example of an extended array consisting of six components engine, food, light, parachute, radio, and toilet, how the following binary search method will look for parachute.
```  public static int binarySearch(String[] nam, String comp){
boolean found = false; int lower = 0; int upper = a.length - 1;
int mid = 0;
while(!found && lower <= upper){
mid = (lower + upper) / 2;
if(nam[mid].equals(comp)){
found = true;
}
else if (nam[mid].compareTo(comp) < 0}{
lower = mid + 1;
}
else{
upper = mid - 1;
}
}
if(found){
return mid;
else{
return -1;
}
}
```
Here, nam[mid].compareTo(comp) < 0 means that the string nam[mid] precedes the desired string comp in the alphabetic order.

```

```
```

```
10. Design a class Astronaut for describing astronauts. Each object of this type should contain three fields: the astronaut's name, age, and the number of training hours. Do not forget to add a constructor method and accessor ("get") and modifier ("set") methods. In the main program, create a new object describing you yourself as an astronaut.

```

```
11. Let us design a dialog program to help astronauts report possible spaceflight problems. This program should:
• ask an astronaut whether there are still some problems to report, by asking him or her to type Yes or No;
• if there are more problems to report, the program should ask for the description of the next problem and record it.
The program should record these descriptions into an array problems:
• the description of the first problem should be stored in the element problems[0];
• the description of the second problem should be stored in the element problems[1];
• etc.
The program should also count the number of reported problems and record it into the variable numberOfProblems.

```

```
12. Describe the contribution of Ada Lovelace to computing.
For extra credit: describe one or two more events from history of computing.

```

```
13. Instead of buying software (like Windows) or music, some students copy this software or music from their friends.
• Provide two arguments why this practice is not ethical; explain how such copying affects the authors of this software (music).
For extra credit: provide additional arguments.
• Provide two arguments that students use to explain their copying.
For extra credit: provide additional arguments.