CS 1401, Exam #1, Typical mistakes

Date: Thursday, February 8, 2007

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.


Typical mistakes:

(a) mentioning that it comes from the name of Al Horezmi without
explaining who he was, what he did, and what is the relation of what
he did to computing and algorithms

(b) claiming that Ada was the first female programmer; while
technically this is true, in reality, she was the first programmer ever

(c) mentioning the events without explaining what they have to do with
computing: Egyptians invented geometry, Napier invented algorithms --
you need to explain what these are events from the history of computing

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.

Typical mistakes:

* writing things like "find out" or "get" which are not algorithmic

* forgetting to ask for prices or amounts (the formulation of the problem
requires that this should be included in the algorithm)

* writing an algorithm which only works on the example but not in
general

* describing an example only and not a general algorithm

* writing a Java-like code without explaining in English what each
step means

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"):


Typical mistakes:

* ! is not allowed, several students confused it with $ which is allowed

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){


Typical mistakes:

* declaring name as a double or as an integer;

* declaring the delivery cost as an integer; in the example, we had
$1.50 which clearly is not an integer

* computations are done before the variables are read from the screen;
in this case, the computations are based on garbage values and thus
produce a useless result

* when printing a message, some students broke the printed string
between the two lines, e.g., as

System.out.println("Dear" + name + ", for your Valentine Day
  delivery, please pay $" + total);

whatever is inside the quotes, the computer reproduces it literally,
so all the spaces between "Day" and "delivery" will be dutifully
reproduced; it is better to split the string, i.e., make it like this:

System.out.println("Dear" + name + ", for your Valentine Day " +
  "delivery, please pay $" + total);

* instead of asking the user for the price of a rose, some students
included the value $1.50 from the example as a constant; as a result,
their programs only work on this particular example, but not in the
general case.

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!".

Typical mistakes:

* not using the input string aName at all

* not assigning anything to the string named message; instead, some
lines of code print the result

* describing a line of code that works only on the given example but
not in the general case

* describing a large piece of code (e.g., the whole main method) instead
of what we asked for -- a single line of code;

* adding an extra line of code like

String aName;

right before the correct line of code which does the required assignment;
this is a serious mistake because after this line of code, the
string aName will be undefined, so you will never get Mary as its
contents