## CS 1401 Introduction to Computer Science Fall 2012 Final Exam, MW class section, December 14, 2012

Name: __________________________________________________________

Background: On this day, in 1503, Nostradamus was born. In his lifetime, he made a lot of prophecies. While most of these prophecies were rather vague, many people believe them, and some people still try to use his book to predict the future.

1. Some of Nostradamus's predictions are based on computations. These computations only use integers. Nostradamus did not use any computers in his computations, but some enthusiasts have written programs to perform similar computations. To check whether a program works correctly, compute, step-by-step, the value of the following expression (as Java would compute it): 2012 - 30 / 2 / 2 + 30 / (2 / 2).

```

```
2. Early editions of Nostradamus's book cost of a fortune. In addition to the price of the book, one also has to pay the sales tax. Write a method that, given the price and the tax rate, returns the total cost of the book, i.e., the value price * (1 + rate / 100). For example, if we buy a book in El Paso where the rate is 8.25%, then buying a book priced at \$1,000 would cost \$1,000 * (1 + 8.25 / 100) = \$1,082.50.

```

```
3-5. A person wants to buy Nostaradamus's book. There are three websites that offer this book for sale.
• The first website offers the book at a price price1.
• The second website offers the book at a price price2.
• The third website offers the book at a price price3.
All these values are given.
• Write a piece of code that uses these three values and if-then statements to compute the number (1, 2, or 3) of the best website -- i.e., of the site which has the cheapest price.
• 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 websites selling Nostradamus's book. In this case, it is reasonable to store the prices from different websites in an array prices, so that the i-th element prices[i] of the array prices is the price from the i-th website. Write a method that, given the array prices, returns the index i of the website with the smallest price. Trace your method on the example of the following three websites:
• site 0 with price \$10,000,
• site 1 with price \$9,000, and
• site 2 with price \$12,000.

```

```
7. To check the accuracy of Nostradamus's predictions, we can divide the number of events c that he predicted correctly by the total number e of studied events. Define a Java method accuracy(int c, int e) that computes this ratio. Use exceptions to make sure that when we apply this method to the case when e = 0, we get a meaningful error message. What is the main advantage of exceptions?

```

```
8-9. Researchers who study Nostradamus's work have prepared an index of all the historical figures whom he (supposedly) predicted. The names of these figures are stored, in alphabetic order, in the array figures. Given the name of the historical figure, we need to find its index i in the array figures.
• Write a method for linear search, and trace, step-by-step, how your method will search for the index of Napoleon in the array consisting of three components Luther, Napoleon, and Stalin.
• Trace, on the example of an extended array consisting of six components Babbage, Lovelace, Luther, Napoleon, Turing, and von Neumann, how the following binary search method will look for Napoleon.
```  public static int binarySearch(String[] fig, String nam){
boolean found = false; int lower = 0; int upper = fig.length - 1;
int mid = 0;
while(!found && lower <= upper){
mid = (lower + upper) / 2;
if(fig[mid].equals(nam)){
found = true;
}
else if (fig[mid].compareTo(nam) < 0}{
lower = mid + 1;
}
else{
upper = mid - 1;
}
}
if(found){
return mid;
}
else{
return -1;
}
}
```
Here, fig[mid].compareTo(nam) < 0 means that the string fig[mid] precedes the desired string nam in the alphabetic order.

```

```
```

```
10. Design a class Prophecy for describing prophecies. Each object of this type should contain three fields:
• the year of the event,
• the description of the event, and
• the name of the main historic figure related to this event.
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 such a historic figure.

```

```
11. Let us design a dialog program to help future prophets make predictions. This program should:
• ask a future prophet whether they are still some events to predict, by asking him or her to type Yes or No;
• if there are more events to predict, the program should ask for the next prediction and record it.
The program should record the prediction into an array predictions:
• the first prediction should be stored in the element predictions[0];
• the second prediction should be stored in the element predictions[1];
• etc.
The program should also count the number of predictions and record it into the variable numberOfPredictions.

```

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

```

```
13. Instead of working on a lab assignment, some students simply copy the required program from their friends.
• Provide two arguments (more for extra credit) why this practice is not ethical; explain how such copying affects the knowledge that these students gain.
• Provide two arguments (more for extra credit) that students use to explain their copying.