CS 1401, Exam #2, TR version

Date: Thursday, October 10, 2013
Name (please type legibly, ideally in block letters): ______________________________________________________________________

Yesterday, October 9, was Leif Erikson Day, in honor of the first European who came to the Americas.

1. Several vikings came to different parts of North America in different years. Write a piece of code that decides which of the vikings came earlier. The names of three of these vikings are stored in the variables name1, name2, and name3, and the years of their visits are stored in the variables year1, year2, and year3. Use if-then statements to write down a piece of Java code that prints the name of the earliest traveler.

Comment: There is no need to read anything, assume that all six variables have already been assigned values.

2. Tests make students nervous, their blood pressure goes up. The measurement of blood pressure consists of two numbers, the higher and the lower ones (e.g., 120/70). According to the American Heart Association, a blood pressure is considered normal if the higher pressure does not exceed 140 and the lower does not exceed 90. Write down a Java statement that uses the known values higher and lower to assign, to a boolean variable normal, true or false depending on whether the student has normal blood pressure or not. Draw the truth table for "and", "or", and "not". Use these truth tables to find the truth value of your expression when a student's blood pressure is 135/95.

3-4. For the first travelers to North America, food was scarce, often, rats were the only source of meat. Write a main method that asks the viking for his name and for a number of days he spent in America, and prints a diary describing what he ate every day. For example, if the traveler's name is Leif Erikson, and he spent 5 days in America, your program should print the following diary:
Hi, my name is Leif Erikson, this is my first time in this place.
I plan to call it "North America". Here is what I eat.

Day 1: I ate a juicy and tasty rat.
Day 2: I ate a juicy and tasty rat.
Day 3: I ate a juicy and tasty rat.
Day 4: I ate a juicy and tasty rat.
Day 5: I ate a juicy and tasty rat.
Hint: be careful with using quotes inside the print statement.

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

6. Once you define a new raster img by using a command
JRaster img = new JRaster();
you can make a point with coordinates (x,y) red by using a command
img.set(x, y, JRaster.red);
Use a for-loop to draw a horizontal green line corresponding to y = 60.