CS 1401, Exam #2

Date: Thursday, March 1, 2007
Section (9 am or 10:30 am): ____________________________
Name (please type legibly, ideally in block letters): ______________________________________________________________________

On March 1, 1790, the first United States Census was authorized. 100 years later, in 1890, punch cards were invented to help process census data. IBM, the company that produced the first punch cards, is now one of the main computer companies.

1. To estimate the average population of a city, the organizers of the first census decided to take the average of the largest and the smallest population values. Use if-statements to write down a code that, given three numbers p1, p2, and p3 describing the population of three different cities, computes and prints the average of the largest and the smallest values.

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

2. To promote the census, the census organizers decided to send to each citizen a nice leaflet in which this citizen's data is listed first, and his neighbor's data is listed second. Write down a code that, given two strings s1 and s2 and the name of the citizen (stored in the string name), 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 "Xiaojing Wang", s2 is "Carlos Acosta", and name is "Acosta", then after your code, s1 should contain "Carlos Acosta" and s2 should contain "Xiaojing Wang".

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. A person is eligible for a census either if he or she is a US citizen or if this person is not a US citizen and resides in the US. Let us assume that a Boolean variable citizen is true or false depending on whether a person is a US citizen, and a Boolean variable resides is true or false depending on whether the person resides in the US. Describe a Java expression for being eligible for a US census. Draw the truth tables for "and", "or", and "not" operations. Use these truth tables to find the truth value of your expression for X. Wang who resides in the US but who is not a US citizen. Explain precedence of different logical operations, and how this precedence is related to precedence of different arithmetic operations.

4. Suppose that the results of the census are stored in the file census.dat. Use the while loop to find and print the line that contains your name. Trace your code on the example of a file in which the first line is "Carlos Acosta" and the second is the line about you.

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

Scanner fromFile = new Scanner(new FileReader("census.dat"));

5. To process census data in a computer, we must convert it into the binary code. 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 28 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.