## CS 1401, Final Exam

Date: Wednesday, December 12, 2007
Name (please type legibly, ideally in block letters): ______________________________________________________________________
Section (9:30 am or 11:30 am): _________________________________

Each problem is worth (approximately) the same number of points. Extra credits for a problem will be given only if the problem itself is solved correctly. If you use extra sheets of paper, please place your name on each sheet.

On December 12, 1901, Guglielmo Marconi receives the first trans-Atlantic radio signal. Thus, fast wireless communications started, communication which are now truly ubiquitous and widely spread.

1. In his engineering computations, Marconi (as all the engineers of his time) used the slide rule, the device that uses logarithms to speed up computations. Describe, in detail, how exactly logarithms speed up computations.

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

```

```
2. In the early 20th century, users had to pay for sending information :-). In Russia, people sending telegrams paid 3 kopecks per word + 10 kopecks for the telegram. Sending a picture meant a separate cost of 100 kopecks (= 1 rouble). Write a method called telegram that takes as inputs the number of words and the number of pictures and returns the overall cost. Trace this method for a telegram with 12 words and 2 pictures.

Your method should return 12 * 3 + 10 + 2 * 100 = 246 kopecks.

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 number of words and the number of pictures and displaying the overall cost; design an algorithm and/or write a code.

```

```
3. Define a class Telegram in which each object has three private fields: message (of type String), numberOfWords, and numberOfPictures. 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.
```

```
4. The post office prepared a letter inviting people to send telegrams, but the person preparing this letter misspelled Marconi's last name as Marcony. Write a piece of code that, given a string misspelled that contains the misspelled word Marcony exactly once, replaces this word with the correct spelling Marconi.

For extra credit: write a piece of code which performs the same replacement even when there may be several occurrences of the misspelled word Marcony.

```

```
5. A student wanted to send two telegrams: a telegram to his girlfriend describing how much he loves her, and a telegram to his professor explaining why he cannot come to the final exam. He was supposed to place the message of the love telegram in a string iLoveYou and the message to be sent to his professor into a string cannotCome. In a hurry, the student placed the love message into the cannotCome string, 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 cannot come" into the string iLoveYou, and the word "Love" into the string cannotCome.

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 simplify communications, the telegram office decided to use one-letter abbreviations for commonly used words: L means "love", M means "meet", and A means "arriving". Assuming that the variable abbrev 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 L, not M, nor A, 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 telegram office stores all the telegrams sent during a day in a file, as follows:
• The first line of the file contains the message of the first telegram.
• The second line of the file contains the number of words in the first telegram.
• The third line of the file contains the number of pictures in the first telegram.
• The fourth line of the file contains the message of the second telegram.
• The fifth line of the file contains the number of words in the second telegram.
• The sixth line of the file contains the number of pictures in the second telegram.
• etc.
Assume that every day, no more than 100 telegrams are sent.

Write a piece of code that asks the user for the name of this file and then places the messages from the input file into an array named message, the corresponding number of words into an array named numberfOfWords, and the corresponding number of pictures into an array named numberOfPictures, so that:

• the message of the first telegram is placed in the first element message[0] of the corresponding array, the number of words in the first telegram is placed into numberOfWords[0], and the number of pictures is placed into numberOfPictures[0];
• the message of the second telegram is placed in the second element message[1] of the corresponding array, the number of words in the second telegram is placed into the element numberOfWords[1], and the number of pictures in the second telegram is placed into numberOfPictures[1];
• etc.
The total number of telegrams sent during the day should be placed into the variable numberOfTelegrams. 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 numberOfWords and numberOfPictures and
• the number numberOfTelegrams
(as described in Problem 7), and returns the total price paid for all the telegrams which were sent during the day. Like in Problem 2, assume that the charge per word is 3 kopecks, the charge per picture is 100 kopecks, and the additional charge per telegram is 10 kopecks. Assuming that the number of telegrams sent during the day can be between 0 and 100, what are the reasonable test cases for this method? Give specific examples of the corresponding test data.

```

```
9. Use the algorithms that we learned in class to translate 7 and 12 into binary, to add these numbers in binary, and to translate the result back into decimal (to check the correctness of the result).

For extra credit:

• Multiply two binary numbers corresponding to 7 and 12 and translate the result back into decimal.
• Suppose that a computer has only one decimal significant digit, and it uses round-off; if you ask this computer to subtract 7.0 from 12.0, what will be the result?
```

```
10. A student came to the telegram office; he wants to send, to his friend, a telegram with the copyrighted lyrics of a new song that he downloaded for free from a pirate website. Is this ethical?

Another student came to send his solution to a CS1 Lab (not yet due) to his friend who is also taking this class. Is this ethical?

For each of these two cases, present:

• at least one argument that students may give justifying this behavior, and
• at least one argument explaining why such behavior is harmful.

For extra credit: present more arguments.