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

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

On October 12, 1709, La Villa de San Francisco de Cuellar was founded. This town later became San Felipe del Real Chihuahua and is now known as Cd. Chihuahua.

1. The Vice-Roy received three reports about the new city, with slightly different estimates of its population. To brag to the King of Spain, he selected the largest of these estimates. Use if-statements to write down a code that, given three numbers pop1, pop2, and pop3 describing the three estimates, computes and prints the largest value.

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







































2. The name of the town was decided by voting. Some people wanted to name is San Diego, some San Francisco. Just in case, the city founders prepared two reports to the Vice-Roy, with two different names. Write down a code that, given two strings s1 and s2 containing these reports and the final name of the town (stored in the string town), finds the string which contains this name and places it first.

Example: if s1 is "We called the town San Diego", s2 is "We called the town San Francisco", and town is "San Francisco", then after your code, s1 should contain "We called the town San Francisco" and s2 should contain "We called the town San Diego".

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. To participate in the voting in 1709, a person must be male (female did not vote until late 19 and 20 century), not younger than 16, and spend at least 1 year in America. Describe a Java expression describing the right to vote. Draw the truth tables for "and", "or", and "not" operations. Use these truth tables to find the truth value of your expression for Senora Malinche III, who is female, 18 years old, and spent all these 18 years in the Americas. Explain precedence of different logical operations, and how this precedence is related to precedence of different arithmetic operations.




















































4. For tax purposes, the data about the citizens of the town is stored in a file tax.asc. Use the while loop to find and print the line that contains a given name. Trace your code on the example of a file in which the first line is "Senor Cortes III: 100 pesos" and the second is "Senora Malinche: 60 pesos". (Women could not vote but they still had to pay taxes.)

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

Scanner fromFile = new Scanner(new FileReader("tax.asc"));


















































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 17 and 9 to binary code. Check the results 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.