CS 1401, Final Exam

Date: Tuesday May 10, 2007
Name (please type legibly, ideally in block letters): ______________________________________________________________________

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 papers, please place your name on each sheet.

On May 10, 1924, J. Edgar Hoover became the Director of FBI. Under his leadership, FBI successfully fought against the Mafia, who was at that time smuggling alcohol into the USA. FBI succeeded in jailing Mafia's leader Al Capone. Hoover remained the FBI Director until his death in 1972.

1. Describe the contribution of Ada Lovelace to computing.

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

2. Al Capone is planning to smuggle a load of whisky and beer into the US. Write a method called smuggling that takes as inputs the number of bottles of whisky and the number of bottles of beer and returns the overall expected profit. Each whisky bottle brings a profit of \$5.00 and each bottle of beer bring \$0.50.

Trace this method for smuggling of 20 bottles of whisky and 10 bottles of beer. Your method should return 10 * \$5.00 + 10 * \$0.50 = \$105.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 number of bottles of whisky and the number of bottles of beer and displaying the overall expected profit; design an algorithm and/or write a code.

3. Define a class Smuggling in which each object has three private fields: smugglerName (of type String), numberOfWhiskies, and numberOfBeers. 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. Al Capone prepared a letter to send to the bars about the incoming shipment. His advisors advised him to replace the word "whisky" (which may get him in trouble) with the words "Irish milk". Write a piece of code that, given a string letter that contains the word whisky exactly once, replaces this word with Irish milk.

5. Al Capone originally planned for his friend Bugsy Siegel to smuggle whisky from Canada, and for his other friend Scarface to smuggle alcohol from Mexico via El Paso. So, he placed the value "Bugsy Siegel" into the variable canada, and the value "Scarface" into the variable mexico. It turned out that Bugsy was in Las Vegas, closer to El Paso, so Al Capone decided to swap the assignments. Write a method that swaps the values of the two above variables. Trace your method on the above example; in this example, as a result of the swap, the variable mexico will now have the value "Bugsy Siegel", and the variable canada should have the value "Scarface".

Hint: your program 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 make sure that FBI did not find out their secrets, the gangsters decided to use one-letter abbreviations: W means whisky, B means beer, and A means Al Capone. Assuming that the variable secretLetter 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 W, not B, 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. To keep track of all their profits, the gangsters decided to store all the information about smuggling in a file as follows:
• The first line of the file contains the name of the person responsible for the first smuggling.
• The second line of the file contains the number of whiskies smuggled during the first smuggling.
• The third line of the file contains the number of beers smuggled during the first smuggling.
• The fourth line of the file contains the name of the person responsible for the second smuggling.
• The fifth line of the file contains the number of whiskies smuggled during the second smuggling.
• The sixth line of the file contains the number of beers smuggled during the second smuggling.
• etc.
Assume that there have been no more than 10 smugglings.

Write a piece of code that asks the user for the name of this file and then places the smuggler names from the input file into an array named smuggler, the corresponding number of whiskies into an array named whiskies, and the corresponding number of beers into an array named beers, so that:

• the name of the first smuggler is placed in the first element smuggler[0] of the corresponding array, the number of whiskies smuggled at this smuggling is placed into whiskies[0], and the number of beers is placed into beers[0];
• the name of the second smuggler is placed in the second element smuggler[1], the number of whiskies smuggled at this smuggling is placed into whiskies[1], and the number of beers is placed into beers[1];
• etc.
The total number of smugglings should be placed into the variable smugglings. 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 array whiskies and the number smugglings described in Problem 7, and returns the total number of whiskies smuggled into the US. Assuming that the number of smugglings can be between 0 and 10, 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 5 and 10 into binary, add and multiply these numbers in binary, and translate the results 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 5.0 and 10.0, what will be the result?

10. To help fight crime, a student interning at FBI wrote a code enabling people to tap into phone conversations, and suggested to this bosses that they use without asking for an official approval. Is it ethical?

Since FBI does not have the latest software to process its databases, the same student suggested to download an illegally posted copy of a new code cracking software. Is such downloading ethical?

For each of these two cases, present:

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

For extra credit: present more arguments.