CS 1401, Exam #2, 9:30 am version

Date: Friday, October 12, 2007
Name (please type legibly, ideally in block letters): ______________________________________________________________________

On October 12, 1492, Chistopher Columbus landed in the Caribbean -- mistakenly believing that he is in Asia.

1. To better estimate his latitude, Columbus crew made three measurements of the same latitude. Since the results were slightly different, they selected the middle value. Use if-statements to write down a code that, given three numbers lat1, lat2, and lat3 describing the three measurement results, computes and prints the middle value.

Hint: You do not need to read anything from the keyboard or from a file, just compute and print. Assume that lat1, lat2, and lat3 have been assigned some values already.







































2. Columbus was original trying to get financial support from Italy, but ended up supported by Spain. Just in case, he has originally prepared several proclamations proclaiming the new lands to be the property of Italy and Spain. Write down a code that, given two strings s1 and s2 and the name of the country (stored in the string country), finds the string which contains this name and places it first. Assume that the names are always spelled in a normal way: first letter capitalized, all other letters small.

Example: if s1 is "I proclaim these lands the property of Italy", s2 is "I proclaim these lands the property of Spain", and country is "Spain", then after your code, s1 should contain "I proclaim these lands the property of Spain" and s2 should contain "I proclaim these lands the property of Italy".

Trace your code, step by step, on this example: draw all the boxes and show how their values change after each operation.

For extra credit: make the code work also if the name is spelled in all caps.












































3. In Spanish empire, two conditions must be satisfied for person to have the right to wear his hat in the presence of the King. First, this person must be either a Spanish nobleman or an Indian nobleman. Second, this person must be not on the Inquisition list of heretics. Describe a Java expression for having this right. Draw the truth tables for "and", "or", and "not" operations. Use these truth tables to find the truth value of your expression for Senor Torquemada who is not a Spanish nobleman, not an Indian nobleman, and who is not on the Inquisition list (he is the Grand Inquisitor himself). Explain precedence of different logical operations, and how this precedence is related to precedence of different arithmetic operations.


















































4. Suppose that Columbus diaries are stored in the file columbus.txt. Use the while loop to find and print the line that contains "1492". Trace your code on the example of a file in which the first line is "1491: looking for funding" and the second is "1492: found America".

Reminder: to create an object for reading from a file, you need to use the following code:

Scanner fromFile = new Scanner(new FileReader("columbus.txt"));


















































5. To convert a decimal integer n to binary form, you repeatedly divide n by 2 and keep the remainder until you get 0. Reading the remainders from bottom to top provides the desired binary number. Use this algorithm to convert 92 to binary code. Check the result by converting the binary number back into the decimal code.

Reminder: 137 in decimal code means 1 * 10^2 + 3 * 10^1 + 7 * 10^0. Similarly, 101 in binary code means 1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 1*4 + 0*2 + 1*1 = 4 + 1 = 5.