CS 1401, Exam #2, MW version

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

As you probably remember, in the ancient time, Spain was a province of Roman Empire called Hispania. The end of the Roman rule started on October 13, 409, when several tribes (Visigoths, Alans, and others) crossed the Pyrenees and settled in what is now Spain.

1-2. Every year on October 13, people of Spain celebrated the anniversary of their fight for independence. Use a loop to write a piece of code that asks the user for a year and then prints celebratory statements up to this year. For example, if the user inputs 414, your piece of code should print the following sequence of statements:

On October 13, 410, we celebrate the 1st anniversary of our fight for independence.
On October 13, 411, we celebrate the 2nd anniversary of our fight for independence.
On October 13, 412, we celebrate the 3rd anniversary of our fight for independence.
On October 13, 413, we celebrate the 4th anniversary of our fight for independence.
On October 13, 414, we celebrate the 5th anniversary of our fight for independence.
For extra credit: make endings (1st, 2nd, etc.) correct. Note that: The last digit of an anniversary can be obtained as a remainder of dividing by 10: 23 % 10 is 3. The second-from-last digit can be obtained if we first divide the anniversary by 10, and then take the remainder. For example, for 123rd anniversary, we first compute 123 / 10 = 12, and then take 12 % 10 = 2.
















3-4. A natural way to celebrate a holiday is to have a feast. For this feast, we need to buy food and drinks. Write a piece of code that asks the user for costs of different items and add them together. Start with a total of 0. In a loop, ask the user for a cost of a new item, and add this cost to the total. Stop when the cost is 0.





















































5-6. For celebration, you baked several (n) tasty pies. The first pie you ate yourself. However, people noticed that you are eating a tasty food, so the next pie you share with a friend -- and thus, eat only half of it. When time came for eating the third pie, yet another friend joined, so you only ate one third, etc. Overall, the total number of pies you ate is equal to the sum
1   1   1         1
- + - + - + ... + -
1   2   3         n
Write a code that, for a given integer n, computes this sum. Trace your code for n = 3. Caution: What will happen if you write 1 / 2 in Java?















































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














































8-9. An inch is defined as a 2.54 cm. To help students transform inches 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 in, 2 in, ..., all the way to n inches. For example, if the number is 3, your program should print the following table:
1 in is 2.54 cm
2 in is 5.08 cm
3 in is 7.62 cm