## CS 1401, Final Exam, version a

Date: Friday, December 9, 2005
Name (please type legibly, ideally in block letters): ______________________________________________________________________
Each problem is worth (approximately) the same number of points. Extra credit for a problem will be given only if the problem itself is solved correctly. If you use extra sheets of paper, place your name on each sheet.

On December 9, 1793, the first daily newspaper appeared in New York City.

1. Describe at least two events from the history of computing, and how these events affected the field of computing.

```

```
2. Every day, some copies of the daily newspaper are sold in stores and vending machines, and some by subscription. Write a method that takes, as input, the total number of copies sold (in stores and vending machines), the price of a newspaper, the total number of subscriptions, and the per-day price of a subscription, and returns the total amount of money earned by the newspaper. Trace this method on the example of a small newspaper "CSfolks" that sold 10 copies for \$0.50 each at a store and has 6 subscriptions, at \$0.25 per day. Hints:
• to trace a method, first write a line of code (to be placed in the method main) that call 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: design a GUI for reading the information about the newspaper sales; design an algorithm and/or write some code.

```

```
3. (related to Problem 2) Define a class Sales in which each object has four private fields: numberSold, price, numberOfSubscriptions, and perDay. Your code for defining the class should contain:
• a constructor method,
• methods for returning and changing the field values, and
• a method for computing the overall revenue of a newspaper.

```

```
4. The newspaper "CSfolks" prepared a story about how good CS students are; this story said that students from CS 1401 submitted 120 research reports. While this story went to print, 12 more reports were submitted, making the total of 132. Write a method that, given a string story that contains the number 120 exactly once, replaces this number with 132. Trace your method on the example of a string

"Computer Science students submitted 120 research reports."
```

```
5. A student in charge of printing the newspaper accidentally saved its page 1 in a string titled page2, and its page 2 is a string titled page1. Help the student to recover from this error: write a method that swaps the values of two globally defined string variables page1 and page2. Trace your method on a toy example when originally the contents of the string variable page1 is

"Computer Science students submitted 132 research reports"

and the contents of the string variable page2 is

"CSFolks, a newspaper".
```

```
6. The newspaper editors mark different stories depending on where they should go: a letter 'N' marks general news, 'S' marks sports section, 'C' marks comics section, and 'W' marks weather reports. Write a piece of code, based on a char variable letter, assigns, to the string variable section, the name of the section to which the corresponding story must go. Write two versions of this code:
• by using if statements, and
• by using switch statements.
What are the relative advantages and disadvantages of using if and switch? For extra credit: what are the relative advantages and disadvantages of using ?: operators?

```

```
7. The contents of each story, as typed in by the journalists and reporters, is contained in a file. Write a piece of code that asks the user for the file name, and then places the story from the corresponding file into an array story of strings, so that:
• the first line of the file is placed in the first element story[0] of the array,
• the second line of the file is placed in the second element story[1] of the array,
• etc.
Your piece of code should also return the overall number of lines in the original story. Use try-catch to catch the situation when there is no file with this name, and produce a corresponding error message.

```

```
8. (related to Problem 7) Write a method that takes, as the input, the array of strings story and the number of lines in the original story, and returns the average length of the line. Assuming that a line can be from 1 symbol to 80 symbols long, and the story can be from 1 line long to 100 lines long, what are the reasonable test cases for this method? Give specific examples of the corresponding test arrays.

```

```
9. Use the algorithms that we learned in class to translate 9 and 12 into binary, multiply these numbers in binary, and translate the product back into decimal (to check the correctness of the result). Suppose that a computer has only one decimal significant digit, and it uses round-off; if you ask this computer to multiply 0.9E1 and 1.2E+01, what will be the result?

```

```
10. Students who write and launch computer viruses claim that they are doing a useful job for the community by discovering the defects of operating systems and anti-virus software and by pointing out these defects to the community. Is it ethical? Explain your answer. Give two other examples of unethical computer-related behavior (more for extra credit); for each example, explain how students justify their behavior and why it is harmful to these students and/or to others.