CS 1401, Exam #2, Typical mistakes

Date: Thursday, March 1, 2007

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.


Typical mistakes:

1) If you write

int average = smallest + largest /2;

Java will first divide largest by 2 and then add smallest; this will not
be the average; for average, you need

int average = (smallest + largest)/2;

2) Missing cases when some values pi are equal. For example, if you
compute largest as follows:

if (p1 > p2 && p1 > p3)
  {largest = p1;}
else if (p2 > p1 && p2 > p3)
  {largest = p2;}
else
  {largest = p3;}

then, when p1 = p2 > p3, your program will erroneously return p3 as
largest. You should write

if (p1 >= p2 && p1 >= p3)
  {largest = p1;}
else if (p2 >= p1 && p2 >= p3)
  {largest = p2;}
else
  {largest = p3;}

3) Not considering all the cases. For example, if you wrote

if (p1 >= p2 && p2 >= p3}
  {average = (p1 + p3) / 2;}
else if (p2>= p1 && p1 >= p3)
  {average = (p2 + p3) / 2;}
else
  {average = (p1 + p2) / 2;}

your program will produce wrong result when, e.g., p1 < p2 < p3.

4) Starting your code with

int p1;

Once you do that, to the variable p1, we assign a garbage value. We
assumed that the values are given, so you do not need to define them.

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.


Typical mistakes:

1) instead of checking which string contains "name", compare strings
s1 and s2 in alphabetic order

2) no tracing

3) wrong swap, which overwrites one of the strings and loses information

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.


Typical mistakes:

wrong truth tables

no Java expression at all.

Common misconception: that && has the same preference as multiplication.
Not true, multiplication comes first: e.g.,

3 * 4 > 5 && 1 < 2

we first multiply. The confusion is with the fact that && is performed
before || because && is like multiplication and || is like addition,
and multiplication has higher priority than addition -- hence && has
higher priority than ||.
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"));

Typical mistakes:

1) comparing strings by using ==; == only compares addresses

2) the line is supposed to contain your name, not be equal to it

3) not reading from a file

4) not using while loop

5) not tracing; tracing means on an example, describing, step by step,
the values of all the variables and how they change; tracing is NOT about
rephrasing the program in plain English

6) assigning integer values to a string variable and vice versa

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.


Typical mistakes: not following algorithm correctly, forgetting to check.