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.



























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

    Class Account