## CS 1401, Exam #1, Solutions

Date: Thursday, February 8, 2007
Section (9 am or 10:30 am): ___________________________
Name (please type legibly, ideally in block letters): ______________________________________________________________________

1. History of computing:
(a) Where does the word "algorithm" come from?
(b) Briefly describe contribution of Ada Lovelace to computing.
(c) For extra credit: describe one more event from the history of computing.

```
Solution:

(a) In the 9th century, a mathematician named Al Khorezmi (meaning "from
Khorezm") wrote a book in which he described in detail how to
multiply, how to divide, how to solve simple equations. His
descriptions were so detailed and good that every time someone would
write a good step-by-step description of some procedure, others
would say: "This is as good as the book by Al Khorezmi". When
translated from one language to another, an Oriental-sounding
"Al Khorezmi" became a more European-sounding "algorithm".

(b) Ada Lovelace was the world's first programmer, she showed how to
write code for the Analytical Engine that Charles Babbage was designing.

```
2. As you know, the Valentine Day is coming soon, so flower shops are getting ready. Suppose that a florist charges a certain amount per rose and a certain fixed amount for delivery. A student who orders the roses is not good in calculations, and a young man who helps the florist is not good in calculations either. You need to help them. Describe, step-by-step, an algorithm that would allow to compute how much the delivery will cost. As part of this algorithm, you should ask for the prices and for the amounts. For example, if each rose costs \$1 and delivery costs \$1.50, then the cost of delivering a dozen roses is 12 * 1.0 + 1.50 = \$13.50.
```
Solution:

1) ask a florist for a price per rose and for the cost of delivery

2) ask a student how many roses he or she wants to send

3) multiply the number of roses by a price per rose

4) add the cost of delivery

```
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"):
• Valentine Day
not valid: has a blank space in between, and blank spaces are not allowed
• ValentineDay!
not valid: has a symbol !, and this symbol not allowed
• ValentineDay
valid
• main
actually, it is valid; however, if you answered "not valid: reserved word of Java", you still get a full credit for this part of the question
• i_am_happy
valid

4-5. Write a main method that asks a person for the price per rose, price of delivery, number of roses, and the name of the client, and then computes and prints the statement:

``` Dear (name here), for your Valentine Day delivery, please pay \$(amount).
```
(Do not worry about formatting.) For example, if John orders 12 roses at \$1 each and the delivery costs \$1.50, the computer should print:
` Dear John, for your Valentine Day delivery, please pay \$13.50.`

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 price per rose");
double pricePerRose = keyboard.nextDouble();
System.out.println("Please enter the price of delivery");
double priceOfDelivery = keyboard.nextDouble();
System.out.println("Please enter the number of roses");
int numberOfRoses = keyboard.nextInt();
String name = keyboard.next();
double amount = numberOfRoses * pricePerRose + priceOfDelivery;
System.out.println("Dear " + name + ", for your Valentine Day "+
"delivery, please pay \$" + amount);
}
```
6. Help a florist to write messages which come with the flowers. For that, write down a line of code that, given a string aName (that contains the name of the person who will receive the roses), places the message "Dear (name here), be my Valentine!" into a string message. For example, if the roses are intended for Mary, the contents of the string message should be "Dear Mary, be my Valentine!".
```
Solution:

message = "Dear " + aName + ", be my Valentine!";
```