## CS 1401, Final Exam

Date: Thursday, May 5, 2005
Name (please type legibly, ideally in block letters): ______________________________________________________________________
UTEP ID # (if you do not remember it, it is OK): ______________________________________________________________________

Comment: Each problem is worth (approximately) the same number of points. Problems marked by two numbers are worth twice as many points as problems marked by a single number.

Today is Cinco de Mayo, a holiday that started as a celebration of victory over the French but which has now turned into a celebration of the cultural heritage of the people of the Americas. From the computational viewpoint, one thing to be proud of was an independent discovery of zero in pre-Columbus America.

1a. Explain how the discovery of zero helps in computations (hint: it has something to do with the positional system). Outside the Americas, who was the first to discover zero?

```

```
1b. Describe, in detail, at least one more event from the history of computing (feel free to describe one more event for extra credit).

```

```
2. After writing an essay about the Mayans discovering zero, you realized that the only time you mentioned the word "zero", you accidentally typed in the corresponding Spanish word "cero".

2a. Write a method that, given an arbitrary string essay in which the word "cero" appears exactly once, replaces it with "zero". Hint: your method must not read or print anything, just take the string as an input and return a corrected string as a result.

```

```
2b. What are the reasonable test cases for the method you described in Problem 2a? Hint: the name can appear in the beginning of the essay, at the end, at a random place, etc.

```

```
3. One of the advantages of a positional system is that we can use the same symbols for ones, for tens, for hundreds, etc. So, if we accidentally swapped the hundreds and the thousands, we can always swap back. Let us assume that the digits are stored as character variables digit0 (for ones), digit1 (for tens), digit2 (for hundreds), digit3 (for thousands), etc. It turns out that the digits corresponding to the hundreds and to the thousands need to be swapped.

3a. Write down a code fragment that would swap the values of the corresponding digits.

```

```
3b. Trace, step by step, how this code fragment swaps them. Draw boxes corresponding to variables and show how their contents will change at each step of the computations. Hint: are characters a reference type?

```

```
4. To enthuse kids to study math, educators decided to design a game in which each kid contributes a digit to the number. To facilitate this process, they decided to define a class Digit in which every object has a name of the kid who proposed it and the digit itself.

4a. Write down a code that defines such a class. Hint: make sure that the values of the fields are private, and that there are appropriate get and set methods to return and change the values of these fields; do not forget the constructor!

```

```
4b. From some kids, the educators want to solicit more information, namely, the kid's gender. Write down a code that defines the corresponding class Additional as subclass of the class Digit.

```

```
5-6. For the gender question, kids have three options:
• M for male,
• F for female, and
• R for "refused to tell".
Write down two code fragments that, based on this letter, explicitly print out the corresponding gender; for example, the input F should lead to printing the word "female" etc. In both code fragments, do not forget to print an error message if the letter does not correspond to any of the above options.

5a. In the first code fragment, use if.

```

```
5b. In the second code fragment, use switch.

```

```
6a. Explain the comparative advantages and disadvantages of using if, switch, and ?:

```

```
6b. For extra credit: Write the third code fragment for solving the same problem, by use ?: instead of if or switch.

```

```
7-8. To speed up the process, the educators decided to carry a laptop with a simple Java program. Each kid types in his or her name and the corresponding digit; this information is recorded into the datafile. To end the program, the educator types in the word "done" instead of the name.

7. Write a code fragment that prompts a kid for this information, and places this information, line by line, into the file digits.dat:

• the first kid's name goes into the 1st line,
• his or her digit goes into the second line;
• the second kid's name goes into the 3rd line,
• etc.

```

```
8. Design a GUI interface that makes it easier for the user to enter this data. Feel free to assume that the necessary objects like a JFrame object have already been defined.

```

```
9. Once a file digits.dat (as in Problem 7) is ready, we need to transfer the corresponding information into the corresponding arrays name[] (of type String) and digit[] (of type character).

9a. Write a code fragment for transferring the data into these arrays.

```

```
9b. Write a method that, based on the arrays name[] and digits[], computes (and returns) the average digit that the kids typed in. Use += and for loop. Catch the division-by-zero exception that will be raised if we try to divide by 0.

```

```
10a. Let us assume that kids typed in digits 8, 9, and 3. If a computer has one (decimal) digit of precision, how would it estimate the average number of digits?

```

```
10b. Write down and explain the main ACM requirements for ethical professional behavior. Give one example of unethical behavior related to computing (feel free to give one more example for extra credit).