## CS 1401, Exam #1, MW version

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

1. You all probably know that our campus's Bhutanese architecture was inspired by the photos of the Kingdom of Bhutan published in National Geographic magazine. Today is the anniversary of this magazine: its first issue appeared on September 22, 1888.

• Geographical discoveries became possible when people learned how to calculate their location. Historically the first people to learn these art were ancient Egyptians. Explain what practical needs motivated ancient Egyptians to develop geography-related computations.
• Describe one more event from history of computing.
```

```
2. 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"):
• NationalGeographic

• main

• 1888

• 22September

• National Geographic

```

```
3. The following formula enables us to compute the volume V of a box with sides x, y, and z: V = xyz. Assuming that x, y, and z are already placed in the corresponding variables of type double, write a Java code statement for assigning the corresponding value to the variable v of type double. Explain, step-by-step, which arithmetic operations will be performed first, which next, etc., and trace the computations on the example when x = 2.0, y = 3.0, and z = 4.0. Explain what happens if you simply write xyz in your Java code.

```

```
4-5. Many people subscribe to National Geographic. Let us assume that subscription costs \$20 per year. One can pay for several years. Write the main method which asks the user for his/her name, asks for how many years he/she wants to subscribe, and prints a memo describing the price. For example, if Ann Gates wants to subscribe for 4 years, your program should print the following message:
```From: National Geographic
To: Ann Gates

To subscribe for National Geographic for 4 years, you need to pay
\$20 X 4 = \$80.
```
Declare 20 as an integer constant, so that it will be easy to change if needed.

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

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

```
6. Suppose that you need to add 3 to the number of years. If the number of years is stored in the integer variable years, which of the two lines of code leads to a correct increase:
• years = years + 3.0;
• years = years + 3;
If originally, before each of these two lines, we had 4 years, explain what will happen after each of these lines is implemented by Java. What is a clearer way (different from those above) to add 3 to the variable years?

```

```
7. National Geographic is one of the most popular magazines. Write a piece of code that decides which of the three given magazines is the most popular. The titles of three magazines are stored in the variables mag1, mag2, and mag3, and the numbers of subscribers to each of these magazines is stored in the variables sub1, sub2, and sub3. Use if-then statements to write down a piece of Java code that prints the title of the most popular of the three magazines.

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

```

```
8. To pay for the National Geographic subscription, one can either send a check or pay by credit card. Write down a Java statement that uses the known truth values sentACheck and paidByCreditCrad to assign, to a boolean variable paid, true or false depending on whether the subscription was paid or not. Draw the truth tables for "and", "or", and "not". Use these truth tables to find the truth value of your expression when a subscriber sent a check (and did not pay with a credit card).