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

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

1. History of computing:
(a) How did the invention of zero helped computing?
(b) Briefly describe the contribution of ancient Egyptians to computing.
(c) For extra credit: describe one more event from the history of computing.


Answer:

(a) The invention of 0 made a positional system possible, in which 2,
2 tens, and 2 hundreds are denoted by the same symbol 2. This makes
computations much easier. Before the invention of 0, there were
different symbols for 1, ten, hundred, etc., which made computations
difficult. For example, adding (or even multiplying)
2007 and 207 is easy, but adding MMVII and CCVII is not easy at all.

(b) Egypt was the main breadbasket of the ancient world, most bread was
produced in the fertile land around the river Nile. This land was
fertile because it was flooded every year. Flooding eliminated the
borders between the farms. The only way to reconstruct these borders
was to write down the angles and distances from different points on
these borders to points outside the flooded zone, and then to
reconstruct the locations of the border points from these angles and
distances. So, Egyptians came up with sophisticated geometric
algorithms for reconstructing these borders.
2. Fall is coming soon, so this weekend is a good time for the last summer barbecues. To organize a big BBQ, students need to rent a big grill, and to buy enough food and drinks for everyone. 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 grill, and the cost of food and drinks per student. For example, if renting a grill costs $20, the food for each student costs $3, and the drinks $2, then the cost of the party for 20 students is $20 + 20 * ($3 + $2) = $120. Thus, each student has to pay $120 / 20 = $6.

Solution:

1) ask for the cost of renting a grill

2) ask for the cost of food per student

3) ask for the cost of drinks per student

4) ask for the number of students

5) add the costs of food and drink per student

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

7) add the cost of renting a grill

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

4-5. Write a main method that asks a person for the cost of renting a big grill, the cost of food 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 barbecue party, you need to collect $(amount) per student.
(Do not worry about formatting the amount.) For example, if Roberto Araiza is a contact person for the party of 20 in which renting a grill costs $20, the food for each student costs $3, and the drinks $2, then each student has to pay $6, so the computer should print:
 Dear Roberto Araiza,

For your barbecue party, you need to collect $6.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 grill");
  double costOfRenting = keyboard.nextDouble();
  System.out.println("Please enter the cost of food per student");
  double foodPerStudent = 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 * (foodPerStudent + drinksPerStudent);
  double amount = overallCost / numberOfStudents;
  System.out.println("Dear " + name);
  System.out.println("");
  System.out.println("For your barbecue party, you need to collect $" +
    amount + " per student.");
}
6. Students decided to extend a good tradition of fortune cookies to the BBQ party. To make individual fortune statements, write down a line of code that, given a string name (that contains the name of the person who will receive this cookie), places the message "Dear (name here), you will do very well on Test 1!" into a string message. For example, for a student whose name is Vladik, the contents of the string message should be "Dear Vladik, you will do very well on Test 1!".

Solution:

message = "Dear " + name + ", you will do very well on Test 1!";