CS 1401, Exam #1

Date: Friday, September 15, 2006, 9: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 Francois Viete's contributions to computing.
(c) For extra credit: describe one more event from the history of computing.

(a) The word "algorithm" comes from the name of a scientist
Al-Khorezmi, which literally means "from Khorezm"; Khorezm (now called
Khiva) is an ancient city in what is now Uzbekistan. In the Middle
Ages, Al-Khorezmi wrote a book in which he described all the mathematical
methods known at that time: how to divide numbers, how to solve linear
equations, how to compute areas, etc. His explanations were so detailed,
unambiguous, and clear, that they became a "gold standard" for
explanations of different procedures. When someone wanted to praise
a step-by-step description for being clear, they would say "This is like
in a book by Al-Khorezmi". Eventually, such clear descriptions
became known as algorithms.

(b) Francois Viete (also known by his Latinized name Francisco Vieta)
invented variable. Before this invention, to describe how to compute the
area of a triangle, we would need to write "multiply the first side by
the second side". More modern notations like A = a * b made computations
2. As you may know, this year, the main Independence Day celebration was moved from Mexico City to the city of Dolores Hidalgo about 170 miles away. As a result, some people who planned to celebrate in Mexico City need to travel further away. Suppose that you know the exact distance, you know how much miles per gallon your car makes, and you know the price of a gallon of oil in pesos (for simplicity, let us avoid kilometers and liters). Describe, step-by-step, an algorithm that would compute the overall price of gasoline needed for the trip. For example, if the distance is 200 miles and your car make 25 miles per gallon, then it needs 200/25=8 gallons. At a price of 30 pesos per gallon that would mean spending 8 x 30 = 240 pesos.

1) First, divide the distance by the number of miles per gallon,
producing the number of gallons.

2) Second, multiply the number of gallons by the price per gallon.

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

 4-5. Write a main method that asks a person for the
distance to travel, for the amount of miles per gallon that his or her
car takes, and for the price of gas, and then computes and prints
the statement: 
 For gas, you pay $(amount). 
(Do not worry about formatting.) For example, if your estimates cost is 240 pesos, the computer should print:
 For gas, you pay $240.0. 

Reminder: to read from the screen, you can define the input object as follows:

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

public static void main(String[] args){
  Scanner key = new Scanner(System.in);
  System.out.println("Type in the distance.");
  double distance = key.nextDouble();
  System.out.println("Type in the amount of miles per gallon.");
  double mpg = key.nextDouble();
  double gallons = distance / mpg;
  System.out.println("Type in the price per gallon.");
  double price = key.nextDouble();
  double pay = gallons * price;
  System.out.ptintln("For gas, you pay $" + pay + ".");

6. We hope that you did well on this test. Congratulate yourself on the job well done by writing down a line of code that, given a string yourName (that contains your name), places the corresponding congratulations into a variable congratulations. For example, if your name is Maria, the contents of the string congratulations should be "Congratulations, Maria, on your first CS exam!".
String congratulations = "Congratulations, " + yourName +
  ", on your first CS exam!";