## CS 1401 Introduction to Computer Science Fall 2012, Sample Test 2 (posted November 2, 2012)

Background: On November 2, 1815, George Boole was born. When George Boole was young, his father trained him in commerce. George did not follow in his father's path, he became a scientist, but the computational skills he learned from his father helped him in his research.

George Boole was the first to show that simple logical reasoning does not require high intellectual abilities, it can be performed by simple computations. In his honor, variables whose values are "true" and "false" are called boolean, and operations on these variables (like "and" and "or") are called boolean operations.

1. A normal price for a copy of George Boole's original book on logic was 3 shillings. However, a customer who decided to buy 10 or more copies got a wholesale price of 2 shillings per book. Define a Java method bill(int numOfCopies) that, given a number of copies, returns the total amount that one needs to pay for that many copies. In the main program, apply this method to compute the total amount that needs to be paid for buying 12 copies.

2. In addition to logic, Boole also studies how to analyze sequence of records made at different moments of time. For example, if we have temperatures recorded at different days, it is important to find the highest of these temperatures. Let us assume that the recorded temperatures are stored in the array temp, so that:

• temp[0] stores the temperature recorded on the first day,
• temp[1] stores the temperature recorded on the second day,
• etc.
Write a method that, given an array of real numbers (of type double), computes the largest of all its elements. Trace your method step-by-step on the example of an array that has three elements: temp[0] = 70, temp[1] = 72, and temp[2] = 68.

3-4. The names of all the customers of the store owned by Boole Sr. are stored, in alphabetic order, in an array customers.

• Write a method for linear search, and trace, step-by-step, how your method will search for Viete in the array consisting of three names Descartes, Napier, Viete.
• Explain, on the example of an extended array consisting of six names Atanassov, Boole, Descartes, Napier, Pascal, Viete, how binary search method will look for the name Boole.
To compare two strings a and b, you can use the following Java operations:
• a.equals(b) means that the strings a and b are identical;
• a.compareTo(b) < 0 means that the string a precedes the string p in alphabetic order; and
• a.compareTo(b) > 0 means that the string a follows the string p in alphabetic order.

5. Design a class Customer, each element of which contains three fields: the customer's name, the amount of money the customer spent on his or her purchases, and the customer's address. Do not forget to add a constructor method and accessor ("get") and modifier ("set") methods. In the main program, create a new object with your own name as a customer.

6. When George Boole was young, his father taught him how to compute the total amount of money earned from different customers during a day. Let us automate this task. Write a program that would:

• ask the user whether the store is still open, by typing Y (meaning yes) or N (meaning no), and
• if yes, ask the user to type in the amount earned from the latest customer.
This amount would then added to the total (in the beginning of the day, the total is 0). This process repeats until the user says "no" (N). At this point, the program should print the total amount earned during the day.

For example, if during the day, three customers came, the first bought 4 shillings worth of stuff, the second 5.5 shillings worth, and the third 1.5 shillings worth, then:

• before the first customer, the total is 0;
• after the first customer, the total becomes 0 + 4 = 4;
• after the second customer, the total becomes 4 + 5.5 = 9.5;
• after the third (and final) customer, the total becomes 9.5 + 1.5 = 11.0.
This value 11.0 should then be printed.