## CS 1401, Exam #2, TR version

Date: Tuesday, October 14, 2014
Name (please type legibly, ideally in block letters): ______________________________________________________________________

On October 14, 1066, the invading Norman army defeated the English king at the Battle of Hastings. Eventually, the conquerors mixed with the local population and formed the modern English nation.

1-2. Every year on October 14, British historians have seminars devoted to the Battle of Hastings. Use a loop to write a piece of code that asks the user for the starting and the ending years and then prints statements about the seminars which occurred during these years. For example, if the user inputs 1967 and 1970, your piece of code should print the following sequence of statements:

```On October 13, 1967, there was a seminar devoted to the Battle of Hastings.
On October 13, 1968, there was a seminar devoted to the Battle of Hastings.
On October 13, 1969, there was a seminar devoted to the Battle of Hastings.
On October 13, 1970, there was a seminar devoted to the Battle of Hastings.
```
For extra credit: improve the previous program by taking into account that there were no seminars during the second world war, i.e., between 1939 and 1944. As a result, if the starting year is 1938 and the ending year is 1946, you should only print statements corresponding to 1938, 1945, and 1946.
```

```
3-4. Let us find out the total attendance of these seminars. Write a piece of code that, give the starting and the ending years, asks the user for the number of attendees in each of the years, and adds them all together. We start with the total equal to 0, and then, for each year, add the number supplied by the user to this total. For example, if we had 10 attendees in the first year and 12 in the second year, we should first compute 0 + 10 = 10, then 10 + 12 = 22.
```

```
5-6. It turns out that the number of seminar attendees grows quadratically with time: at year i, we had i2 attendees. So, between years m and n, we have m2 + (m + 1)2 + ... + n2 attendees. Write a code that, for given integers m and n, computes this sum. Trace your code for m = 1 and n = 3.
```

```
7. Trace, step-by-step, what will be the result of following Java code:
```int n = 11;
String bin = "";
while(n > 0){
if (n % 3 == 1)
{bin = "1" + bin;}
else if(n % 3 == 2)
{bin = "1" + bin;}
else
{bin = "0" + bin;}
n = n / 3;
}
{System.out.println(bin);}
```
Draw the boxes corresponding to all the variables, and show all the changes of their values.

```

```
8-9. A foot is defined as 12 inches, i.e., 12 * 2.54 = 30.48 cm. To help students transform feet to centimeters, write a main method that asks the user for a number n, and prints a table describing how many cm are in 1 ft, 2 ft, ..., all the way to n feet. For example, if the number is 3, your program should print the following table:
```1 ft is 30.48 cm
2 ft is 60.96 cm
3 ft is 91.44 cm
```