## CS 1401, Final Exam

Date: Tuesday, May 6, 2008
Name (please type legibly, ideally in block letters): ______________________________________________________________________
Grading policy: Our intent is that all the problems be equally difficult, so that each problem is worth approximately the same number of points. However, if it turns out that to the class, some problems are more difficult, we will adjust the points given for the problems, so as to give more points to students who successfully solve these difficult problems.

Topic: On May 6, 1840, the first post stamp appeared in England. This post stamp was a part of the reform which made communications cheaper and available for everyone. This started the era of mass communications.

1. In the Middle Ages, communication was difficult, and long-distance travel was almost impossible. Explain how this communication difficulty led to the term "algorithm" and what Al-Khorezmi had to do with it.

For extra credit: Describe one more event from the history of computing, and how this event influenced the field of computing.

```

```
2. In 1840, a letter cost 1 penny. At present, a letter costs 41 cents for the first ounce + 17 cents for each extra ounce. On May 12, 2008, the postage rates will increase even further. Write a method called postageRate that takes as input the rate for the first ounce, the rate for each extra ounce, and the number of ounces, and computes and returns the rate of mailing the corresponding letter. Trace this method for the case when the rate for the first ounce is 41 cents, each extra ounce costs 17 cents, and the letter weighs 3 ounces.

Your method should return \$0.41 + (3 - 1) * \$0.17 = \$0.75.

Hints:

• to trace a method, first write a line of code (to be placed in the method main) that calls this method;
• please notice that in the method, we are not asking you to print anything or read anything from the screen or from a file.

For extra credit: describe a GUI design for entering the rate for the first ounce, the rate for extra ounce, and the weight of the letter in ounces.

```

```
3. Define a class letter in which each object has three private fields: rateForFirstOunce, ratePerExtraOunce, and weight. Your code for defining the class should contain:
• a constructor method,
• methods for returning and changing the field values, and
• a toString method which returns an understandable description of each object.
Make sure that all the letters have the same rate for the first ounce and the same rate per extra ounce.
```

```
4. An out-of-town student prepared a letter to her parents bragging about her excellent grades in CS 1401, but she got confused with calculus (MATH 1411) and accidentally called this class CS 1411. Write a piece of code that, given a string letter that contains the misspelled term CS 1411, replaces the first occurrence of this word with the correct name CS 1401.

For extra credit: write a piece of code which performs the same replacement even when there may be several occurrences of the erroneous name CS 1411.

```

```
5. In addition to a letter to her parents bragging about her success in CS 1401, the student also prepared a letter to the local newspaper complaining about the hot weather in El Paso. She placed the texts of these letters into two strings named parents and newspaper. She got so excited that she accidentally placed the letter to her parents into the string newspaper, and vice versa. Write a method that swaps the values of these two global variables. Trace your method on the example when the student placed the words "I did great!" into the string newspaper, and the words "El Paso is too hot!" into the string parents.

Hint: your method should work (i.e., swap the values) for all possible values of these two variables, not just the ones used in the example.

```

```
6. To keep track of her classes, a student decided to use one-letter abbreviations for commonly used rock groups: C means "Computer Science", M means "Math", and E means "English". Assuming that the variable abbr contain one of these abbreviation, write a piece of code that, based on the value of this variable, prints the meaning of the corresponding abbreviation. If the letter is neither C, not M, nor E, print a blank space.

Write two versions of this code:

• a version that uses if statements, and
• a version that uses a switch statement.
What are the relative advantages and disadvantages of using switch and if statements?

For extra credit: what are the relative advantages and disadvantages of using ?: instead of if or switch statements?

```

```
7. The student decided to store all the information about her classes in a file, as follows:
• The first line of the file contains the name of the first class.
• The second line of the file contains the letter grade for this first class.
• The third line of the file contains the name of the second class.
• The fourth line of the file contains the letter grade for this second class.
• etc.
Assume that she has taken no more than 45 classes.

Write a piece of code that asks the user for the name of this file and then places the names of the classes (from this file) into an array named className and the corresponding grades into an array named grade, so that:

• the name of the first class is placed in the first element className[0] of the corresponding array, the letter grade for this class is placed into grade[0];
• the name of the second class is placed in the second element className[1] of the corresponding array, the letter number for the second class is placed into grade[1];
• etc.
The total number of classes should be placed into the variable numberOfClasses. Use a try-catch statement to handle the situation when there is no file with the input file name, and print out the corresponding error message.

```

```
8. Write a method that takes as input:
• the arrays className and grade, and
• the number numberOfClasses
(as described in Problem 7), and returns the student's GPA. Assuming that the number of classes can be between 0 and 45, what are the reasonable test cases for this method? Give specific examples of the corresponding test data.

```

```
9. In Problem 2, the computer had to multiply 2 and 17. Use the algorithms that we learned in class to translate 2 and 17 into binary, to multiply these numbers in binary, and to translate the result back into decimal (to check the correctness of the result).

For extra credit:

• Suppose that a computer has only one decimal significant digit, and it uses round-off; if you ask this computer to multiply 2.0 and 17.0, what will be the result?
• The computer also had to add 41 and 34. Add two binary numbers corresponding to 41 and 34 and translate the result back into decimal.
```

```
10. A small company for which you work is designing a software that helps to automatically trace registered mail packages. Its testing has shown that this software is still not always working well. Your manager is strongly recommending that you stop testing and release this software. Describe two arguments why the manager believes that his recommendation is ethical, and two arguments explaining why you may feel that this recommendation is unethical.