MET CS 565 Mid Term, Spring 2003 (100 Points), 6 - 9 PM
1. (20 Points) Write only the paint method of an applet, which produces the following pattern, given any value of n, an odd number, the total number of rows (n = 7, in the example)
```                    *
*   *
*   *   *
*   *   *   *
*   *   *
*   *
*
```
Use only the drawString(String s, int x, int y) of the Graphics class to draw one "*" at a time.
2. (30 Points) Consider the problem of placing N Queens on a chessboard of N rows and N columns, so that the queens do not attack each other. Assuming each of the N queens will be placed in distinct rows, we need to keep track only of the column values for each queen. The column values for the N queens can then be represented using an integer array of length N, where the array elements are in the range 1..N.  In the first example, this array will be {1, 3, 5, 2, 4}. In the second example, this array will be {1, 2, 5, 3, 4}. In the first case, the queens do not attack each other. In the second case, Q1 and Q2 attack each other. So, any two queens will attack each other if they are in the same column, or if they are in the diagonal positions. So, if Qi and Qj are any two queens, they attack each other if column(Qi) == column(Qj), or if abs(i - j) == abs( column(Qi) - column(Qj) ).

Write a boolean method, isValidConfiguration(int[] columns), which takes the column configuration as the argument, and returns true only if no two queens attack each other. Hint: Assume Q1 is in position, now check for each of the queens Q2 ... QN in that order, if they are safe with the already placed queens.

 Q1 Q2 Q3 Q4 Q5

3. (50 Points)

Consider a stock brokerage firm which handles the stock transactions for its customers. Let us assume that each customer has a single account with the firm. A customer will open an account with an initial deposit. The customer will be allowed to buy and sell stocks. The customer can buy any quantity of a particular stock at the market price as long as the required funds are available. The customer can sell a   stock (that they own, either fully or partially) at the market price. The customer can deposit funds into their account at any time.

The Brokerage firm charges a fee (commission) for each buy and sell transaction. Let this fee be a static variable of the Brokerage class. The firm keeps a list (use a Vector or an ArrayList) of its customers. The data about the customers is stored persistently in a text file with the following format:

```  CustomerName,CustomerId,Day,TrasactionType,StockSymbol,Quantity,Price
```
 CustomerName Name of the customer CustomerId Unique ID for each customer Day An Integer. A value of 1 will be used for the first day of the firm's existence TransactionType Deposit/Buy/Sell StockSymbol The stock ticker symbol (No value if Deposit) Quantity A number, the stock quantity (No value if Deposit) Price A number, the stock price for the transaction (The Deposit amount if Deposit)

The data will be sorted by the Day when saved to the text file. Write the Brokerage class and the Account class with the following funcationality:

Class Brokerage

• Write the method, void processTransaction(String line), which processes a single line from the text file. Use Hashtable or HashMap to do an efficient lookup if a Customer exists or not.
• Write the method, double computeRevenue(int day), which returns the revenue generated for the firm as a result of the transaction commissions, till the end of any given day.

Class Account

• Write the constructor which takes the CustomerName, CustomerId, and InitialDeposit as the arguments. The InitialDeposit will be the initial cash balance of the account.
• Write the method, boolean isValidBuy(double quantity, double stockPrice), which will return true only if the funds are available.
• Write the method, boolean isValidSell(String stockSymbol, double quantity), which will return true only if the customer has at least that many shares of that stock.
• Write the method, void buy(String stockSymbol, double quantity, int day), which will check the validity before buying the stock. Assume you are given a class Stock with a static method: double getStockPrice(String stockSymbol), which will return the current price of the given stock. The cash balance should be updated.
• Write the method, void sell(String stockSymbol, double quantity, int day), which will check the validity before selling the stock. The stock price can be retrieved as above. The cash balance should be updated.
• Write the method, void deposit(double amount, int day), which deposits the given amount into the customer's account.
• Write the method, double getCashValue(int day), which returns the current cash balance at the end of any day. Note that there could be transactions after this day for the customer which should not be used in calculating the cash value.
• Write the method, double getStockValue(int day), which returns the total value of the stock holdings for the customer at the end of any day. The static method: double getStockPrice(String stockSymbol, int day), in the Stock class, will return the price of the given stock at the end of that day.