## CS 1401, Final Exam

Date: Monday, May 5, 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 5, 1891, the Music Hall in New York (now known as Carnegie Hall) was officially opened. At the opening, Tchaikovsky, the famous Russian composer, served as a guest conductor.

1. The classical music scale is logarithmic. Describe, in detail, how 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.

```

```

Your method should return \$2.00 + 9 * \$1.00 = \$11.00.

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 price of CS, the price of downloading a song, and the number of songs, and for displaying the overall cost of producing the CD.

```

```
3. Define a class CD in which each object has three private fields: costOfBlankCD, costPerSong, and numberOfSongs. 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. A student prepared an email bragging about a nice Beatles CD that he compiled but he accidentally misspelled the band's name as Beetles. Write a piece of code that, given a string email that contains the misspelled name Beetles, replaces the first occurrence of this word with the correct spelling Beatles.

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

```

```
5. A student prepared two classic-rock CDs, with the Beatles and with the Rolling Stones songs, and placed the descriptions of these CDs into two strings named beatles and rollingStones. While working on the CDs, she was enjoying the music so much that she accidentally placed the description of the Beatles CD into the string rollingStones, 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 like Paul" into the string rollingStones, and the words "Mick Jagger rocks!" into the string beatles.

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 his archive of CDs, a student decided to use one-letter abbreviations for commonly used rock groups: B means "Beatles", R means "Rolling Stones", and Q means "Queen". 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 B, not R, nor Q, 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 his self-made CDs in a file, as follows:
• The first line of the file contains the name of the first CD.
• The second line of the file contains the number of songs recorded on the first CD.
• The third line of the file contains the name of the second CD.
• The fourth line of the file contains the number of songs recorded on the second CD.
• etc.
Assume that he has no more than 1,000 self-made CDs.

Write a piece of code that asks the user for the name of this file and then places the names of the CDs (from this file) into an array named name and the corresponding number of songs into an array named numberfOfSongs, so that:

• the name of the first CD is placed in the first element name[0] of the corresponding array, the number of songs in the first CD is placed into numberOfSongs[0];
• the name of the second CD is placed in the second element name[1] of the corresponding array, the number of songs in the second CD is placed into numberOfSongs[1];
• etc.
The total number of CDs should be placed into the variable numberOfCDs. 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 name and numberOfSongs, and
• the number numberOfCDs
(as described in Problem 7), and returns the average number of songs on the student's CD. Assuming that the number of CDs can be between 0 and 1,000, 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 add 2 and 9. Use the algorithms that we learned in class to translate 2 and 9 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:

• Suppose that a computer has only one decimal significant digit, and it uses round-off; if you ask this computer to add 2.0 and 9.0, what will be the result?
• If downloading each song cost \$2, we would also have to multiply 9 by 2. Multiply two binary numbers corresponding to 9 and 2 and translate the result back into decimal.
```

```
10. To prevent people from downloading music for free, the music company uses a special encryption algorithm to store the music files. A group of students cracked the encoding algorithm and placed the easy-to-use decoding algorithm on the web. Is this ethical?