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. All these values are given.





















































































































































































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:


























































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.
  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: 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: The program should record the prediction into an array predictions: 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.