CS 1401, Final Exam, MW version

Date: Wednesday, December 10, 2014
Name (please type legibly, ideally in block letters): ______________________________________________________________________

Each problem is worth 10 points, except if indicated otherwise.

Topic: On December 10, 1815, Ada Lovelace was born.

Question 1:

Question 2:

Question 3:

Question 4:

Question 5:

Question 6:

Question 7:

Question 8:

Question 9:

Question 10:

Total:

1. Explain the role of Ada Lovelace in the history of computing. Who did she help? What was the purpose of the whole project? How did Ada Lovelace influence the field of computing?

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

```
```

2. Ada Lovelace's work was related to computing trajectories of the shells fired by the warship's guns. To property perform these computations, we need to know the warship's speed. This speed can be determined if we measure the distance passed by the ship in a certain time, and then divide the distance by the time.

a. Write a method called speed that takes as argument the distance (in kilometers) and the time (in hours), and returns the speed. Your method should throw an "IllegalArgumentException" if any of the arguments are not positive, otherwise it should computes and return the communication time.

b. Write a statement that calls your method on an example.

c. Trace your method call step by step.

```
```

3. Once a ship is observed, the captain needs to decide whether it is our country's ship, the adversary's warship, the adversary's trade ship, or a ship from a third (neutral) country. Based on the captain's finding, the following method prints the corresponding statement. As input, this method receives a code, where 'o' means our country's ship, 'a' means adversary's warship, 't' means adversary's trade ship, and 'n' means a ship from a neutral country; it also receives the ship's name.

``` public static void printRecommendation(char code, String name){
switch (code) {
case 'o':  System.out.println("\"" + name + "\" is our country's ship");
break;
case 'a':  System.out.println("\"" + name + "\" is an adversary's warship");
break;
case 't':  System.out.println("\"" + name + "\" is an adversary's trade ship");
break;
case 'n':  System.out.println("\"" + name + "\" is a ship from a neutral country");
break;
default: System.out.println("invalid code");
}
```
Write an equivalent method that uses if or if-else statements instead of a switch statement.

Which version of the two methods do you prefer? Explain why.

```
```

4. Suppose you have an array of Strings named ship containing the names of all the ships, and an array of double named speed (of the same size) containing the speeds of the corresponding ships. You also have a double variable named targetSpeed.

Write a piece of code that prints the names of all the ships whose speed is equal to or above the target speed.

```
```

5. In many applications of computing, it is very important to get all the numbers right, so testing a code is extremely important. Describe at least five general rules for testing software (e.g., it is necessary to test all branches), and explain, on the example of one of the methods you wrote in Problems 2-4, what exactly values you should use when testing this method. (Describe exact values, not just general descriptions like "boundary values".)

```
```

6. Different warships have different speeds and different number of guns. Define a class Warship in which each object has three private fields: name, speed, and numGuns. 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.

Write a statement that creates an object of type Warship named "Ada" with a speed of 50.1 km/h that has 12 guns.

```
```

7. Let us help museum workers record the data about historic warships. Write a method named enterData that returns an array of objects of type Warship that are created by prompting the user to enter appropriate data. In particular, your method:

• asks the user for the number of ships to be entered,
• creates the array,
• enters a loop that prompts for the ship's name, its speed and number of guns, and creates an object to be stored in the array.
For example, if the user wants to enter 2 ships, the program creates an array of size 2, then:
• prompts for the name of the first ship and the first ship's speed and number of guns,
• then creates an object of type Warship which is stored at index 0 of the array.
Then it prompts for the data for the second ship, creates another object and store it at index 1 of the array.

Reminders

To create a Scanner: Scanner input = new Scanner(System.in);
To read an integer value: i = input.nextInt();
To read a double value: d = input.nextDouble();
To read a String: s = input.nextLine();

```
```

8. You want to appropriately write and test your enterData method.

• Write the statement that you would use to call the enterData method from the main method. Put your method call statement in a try/catch structure and print appropriate error messages if a InputMismatchException or an IndexOutOfBoundsException occurs
• Name three things you can do to make it easy for other team members to understand the code in your method.

```
```

9. Today is 12/10. Use the general algorithm that we learned in class to transform 12 and 10 into binary code, add these numbers in binary, and transform, step-by-step, the resulting binary number back into decimal; you should get 22.

For extra credit:

• Multiply 12 and 10 in binary, convert the result back to decimal.
• Subtract 10 from 12 in binary, convert the result back to decimal.

```
```

10. Many students, instead of buying or renting a textbook, use a free softcopy that someone posted on the web -- without the publisher's permission.

a. Is it ethically acceptable to do it? Give at least one argument as to why some may think it is acceptable, and at least one argument as to why some may think otherwise (more for extra credit).

b. Explain the difference between what is ethical and what is prohibited by laws and/or regulations.