CS 1401, Exam #1, TR version

Date: Thursday, September 19, 2013
Name (please type legibly, ideally in block letters): ______________________________________________________________________

1. September 19 is International Talk Like a Pirate Day. Nowadays, this is pure fun (and many computer scientists participate in this fun), but in the past, pirates were a serious problems, one of the problems that the British Navy had to deal with. To help the British Navy, Babbage proposed to build a computational device.

For extra credit: describe one more event from the history of computing.

2. For each of the following sequences of symbols, describe which can be valid Java identifiers and which cannot be; if you believe they cannot be, briefly explain why (e.g., "is a reserved word" or "does not start with a letter"):

3. To compute the square root of a given number n, Java starts with some approximation a and then computes the more accurate approximation m as follows:
      1    /    n \
  m = - * | a + -  |
      2    \    a /
(For example, when we compute the square root of n = 2.0 and start with a = 1.0, we get a more accurate approximation m = (1/2) * (1.0 + 2.0/1.0) = 1.5.)

Assuming that n and a are already placed in the corresponding variables of type double, write a Java code statement for assigning the corresponding value to the variable m of type double. Explain, step-by-step, which arithmetic operations will be performed first, which next, etc., and trace the computations on the above example. Describe two different ways to avoid getting 0 as the result of evaluating 1/2.

4-5. To successfully defeat a pirate, you need to send twice as many ships as the pirate. Let us write a program which would help create a memo to the Navy describing how many ships we need. Write the main method which asks the user for the name of the pirate, asks how many ships this pirate has, and creates a memo explaining how many ships we need to successfully defeat this pirate. For example, if the pirate's name is Blue Beard and he has 5 ships, your program should print the following message:
To: The British Navy
From: Charles Babbage

To successfully defeat Blue Beard, we need to send 10 ships.

Reminder: to read from the keyboard, you can define the reader as follows:

Scanner reader = new Scanner(System.in);
the header of the main method is:
public static void main(String[] args){

6. Suppose that we need to send one more ship, and, correspondingly, one more captain. If the number of ships is stored in the variable ships, and the number of captains in the variable captains, which of the two lines of code leads to a correct increase in both: If originally, before each line, we had 5 ships and 5 captains, explain what will happen after each of these lines is implemented by Java. What is a clearer way (different from those above) to increase both values?