## CS 1401, Exam #1, 11:30 am version

Date: Friday, September 21, 2007
Name (please type legibly, ideally in block letters): ______________________________________________________________________

1. History of computing:
(a) How is Java - the language that we study in this class - different from previous programming languages?
(b) Briefly describe the importance of logarithms for computing.
(c) For extra credit: describe one more event from the history of computing.

```
(a) Previous programming languages translated high-level code directly
into machine code. This caused a difficulty when computers needed to
collaborate. Indeed, since different computers have different
machine code, they could only exchange the high-level code, and it
took some time to translate it into machine code on each of the
computers. In Java, different computers can exchange code in JVM;
JVM code is close to machine code, so translating from JVM to
machine code is much faster than translating from, say, C++.

(b) Logarithms were invented to reduce multiplication to an easier
operation of addition. This reduction is based on the known fact that
log(a*b) = log(a) + log(b). Thus, to compute the product a*b of two
numbers a and b, we can do the following:

* use the logarithms table (ro slide rule) to find log(a) and log(b)

* add log(a) and log(b); the resulting sum is log(a*b);

* use the logarithms table to find the number whose logarithm is
equal to the sum log(a*b) = log(a) + log(b).

```
2. Exactly 70 years ago, on September 21, 1937, J. R. R. Tolkien published The Hobbit book, which started the Lord of the Rings series. To celebrate, the students decided to rent a movie theater and watch all the Lord of the Ring movies. Describe, step-by-step, an algorithm that would allow to compute how much we need to collect from each student to cover the costs. As part of this algorithm, you should ask for the cost of renting the movie theater, and the cost of popcorn and drinks per student. For example, if renting a movie theater costs \$1,000, popcorn for each student costs \$4, and the drinks \$2, then the cost of the party for 100 students is \$1,000 + 100 * (\$4 + \$2) = \$1,600. Thus, each student has to pay \$1,600 / 100 = \$16.
```
Solution:

1) ask for the cost of renting a movie theater

2) ask for the cost of popcorn per student

3) ask for the cost of drinks per student

4) ask for the number of students

5) add the costs of popcorn and drinks per student

6) multiply the number of student by the sum from 5)

7) add the cost of renting a movie theater

8) divide the resulting cost by the number of students

```
3. For each of the following sequences of symbols, describe which can be valid Java identifiers and which cannot be; if you believe they cannot be, briefly explain why (e.g., "is a reserved word" or "does not start with a letter"):
• King Aragorn
not valid: has a blank space in between, and blank spaces are not allowed
• KingAragorn!
not valid: has a symbol !, and this symbol not allowed
• KingAragorn
valid
• double
not valid: reserved word of Java
• popcornIs\$4PerPerson
valid but not recommended

4-5. Write a main method that asks a person for the cost of renting a movie theater, the cost of popcorn per student, the cost of drinks per student, the number of students, and the name of the contact person, and then computes and prints the statement:

```Dear (name here),

For your Hobbit party, you need to collect \$(amount) per student.
```
(Do not worry about formatting the amount.) For example, if Sheline John is a contact person for the party of 100 in which renting a movie theater costs \$1,000, the popcorn for each student costs \$4, and the drinks \$2, then each student has to pay \$16, so the computer should print:
``` Dear Sheline John,

For your Hobbit party, you need to collect \$16.00 per student.
```

Reminder: to read from the keyboard, you can define the reader as follows:

```Scanner keyboard = new Scanner(System.in);
```
the header of the main method is:
```public static void main(String[] args){

Solution:

public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the cost of renting a movie theater");
double costOfRenting = keyboard.nextDouble();
System.out.println("Please enter the cost of popcorn per student");
double popcornPerStudent = keyboard.nextDouble();
System.out.println("Please enter the cost of drinks per student");
double drinksPerStudent = keyboard.nextDouble();
System.out.println("Please enter the number of students");
int numberOfStudents = keyboard.nextInt();
System.out.println("Please enter the name of the contact person");
String name = keyboard.next();
double overallCost = costOfRenting +
numberOfStudents * (popcornPerStudent + drinksPerStudent);
double amount = overallCost / numberOfStudents;
System.out.println("Dear " + name);
System.out.println("");
System.out.println("For your Hobbit party, you need to collect \$" +
amount + " per student.");
}
```
6. To make the Hobbit party a great memory, the organizer decided to give every student an official certificate. To design such certificates, write down a line of code that, given a string name (that contains the name of the person who will receive this certificate), places the message "This certificate confirms that (name here) participated in the 2007 Hobbit party." into a string message. For example, for a student whose name is Vladik, the contents of the string message should be "This certificate confirms that Vladik participated in the 2007 Hobbit party."
```
Solution:

message = "This certificate confirms that " + name +
" participated in the 2007 Hobbit party.";
```