## CS 1401, Exam #2, MW 12-1:20 version

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

On October 9, 1940, John Lennon, one of the Beatles, was born.

1. Write a piece of code that decides which of the Beatles' songs is the most popular. The titles of three songs are stored in the variables song1, song2, and song3, and the numbers of times each song is downloaded are stored in the variables times1, times2, and times3. Use if-then statements to write down a piece of Java code that prints the title of the most popular of the three songs.

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

```

```
2. According to the syllabus, to successfully pass this class, you need to get at least 70 overall and at least 60 points on the final exam. Write down a Java statement that uses the known value overall of the overall grade and the the known value final of the grade on the final exam to assign, to a boolean variable passed, true or false depending on whether the student passed 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 has 80 overall and 71 on the final exam.

```

```
3-4. One of the Beatles's songs asks: "Will you still need me, will you still feed me, when I'm sixty four?". Write a main method that asks the user for his/her age and then prints statements confirming that in the future, he will still be needed and fed until he/she is 64. For example, if the age is 59, your program should print the following statements:
```You will still need me, you will still feed me, when I'm 60.
You will still need me, you will still feed me, when I'm 61.
You will still need me, you will still feed me, when I'm 62.
You will still need me, you will still feed me, when I'm 63.
You will still need me, you will still feed me, when I'm 64.
```
Hint: be careful with using apostrophe ' inside the print statement.
```

```
5. Trace, step-by-step, what will be the result of the following Java code:
```int i = 10;
int j = 4;
while(i > 0 && j > 0){
if (i >= j)
{i = i%j;}
else
{j = j%i;}
}
if(i == 0)
{System.out.println(j):}
else
{System.out.println(i);}
```
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) green by using a command
```img.set(x, y, JRaster.green);
```
Use a for-loop to draw a vertical green line corresponding to x = 60.