CS 2102 Homework Assignment #6 [READY!]

Assigned: Tuesday December 5, 2006, 10:00 AM
Due: Tuesday December 12, 2006, 10:00 AM              *** new date: extra day on assignment ***

Guidelines:

Now available.

Description

The problems in this homework are independent of each other, and test a variety of concepts that you have learned in CS2102. It also gives you the opportunity to act as an experimenter, trying to compare performance of different solutions. In the experiments, it doesn't matter what the absolute performance is, but rather the relative improvement that one approach has over another one.

Initial Design

Point Allocation

  1. [25 pts.] Write a program Problem1 that takes a two-dimensional array double [][]ar and computes the saddle point for the matrix, if it exists. A saddle point is defined as the array entry ar[r][c] which is the minimal unique value in its row and the maximal unique value in its column (i.e., use "<" to determine if a number is smaller, not "£").

    Here are a set of questions that you must run some experiments to answer:

    1a) How often does a random square array contain a saddle point?  Given the nature of random computations, you cannot definitively answer this question. However, you can run the following set of trials to determine trends (much like you could flip a coin 100 times and get a number near 50).
     
    for sizes s=3 up to and including s=15

        int count = 0;
        run 10,000 trials of the following

           create a double[][] ar = new double [s][s]
           populate ar with random double values.
           if ar has a saddle point, then increment count

         print percentage value for (count/10,000)
     

    Expected output:

    Something like:

    3   somePercentage
    4   somePercentage
    5   somePercentage
    6   somePercentage
    7   somePercentage
    . . .

    Note that the statement Math.random() returns a random double in the range 0..1, which will be perfect for this assignment.

    1b) Are saddle points more frequent in rectangular arrays than in square arrays? Consider the following arrays, with the same number of elements:

    36 6x6 4x9
    64 8x8 4x16
    100 10x10 4x25

    Compute over 10,000 trials (as in 1a) the percentage of random square arrays that have a saddle point as compared to a random rectangular array (with an equal number of elements, though arranged as a rectangle). Your output should look like the following:

    Expected output:

    Something like:

     

       
    N Square Array Rectangle Array
    36 SomePercentage SomePercentage
    64 SomePercentage SomePercentage
    100 SomePercentage SomePercentage

    Based on your empirical evidence, are saddle points more frequent in rectangular arrays?
     

  2. [30 pts.] This question will test your knowledge of java.util.Hashtable and java.util.ArrayList. The specific context is Financial Trading Data.  Using a somewhat-hard-to-obtain historical generator program, I have constructed financial information for 30 securities across a range of industries for a period of 30 days.

    You are given a securities.dat and prices.dat data file [a README.txt explains more of the format]. Your goal is to write a program Problem2 that produces a report that processes all of the information as found in the prices file. In particular, over the 30 days, you want to record aggregate information for each of the securities, namely the low price, high price, opening value at the beginning of the 30 day period, and closing value at the end of the 30 day period. You also need to compute the percentage profit (or loss) in price over that 30 day period. See Sample report below.

    In addition, each security belongs to a sector of industry (see the securities.dat file). You must produce an aggregate report within each sector that records the total number of securities that sold for that industry within the 30 day period. See Sample report below.

    Produce a report that looks like the following. The top table contains one line per security, showing the opening price at the beginning of the 30 days, the lowest seen over the thirty days, the highest seen over the thirty days, and the last closing price. The profit is calculated by the formula (close-open)/open. The bottom table aggregates securities within an industrial sector and each row reports the total number of securities sold within that sector over the 30 day period.
     
    Security Open Low High Close Profit Sector 
    -------- ---- --- ---- ----- ------ ------
    Security_0 75.0 64.61 82.84 74.0281 -1% MEDICAL
    Security_1 67.0 59.8 79.92 76.7932 15% BANKING
    Security_2 34.0 29.12 37.8 35.5924 5% MEDICAL
    Security_3 43.0 39.56 52.32 46.8932 9% ENTERTAINMENT
    Security_4 59.0 52.78 75.21 69.5568 18% BANKING
    Security_5 25.0 20.46 27.0 22.5531 -10% MEDICAL
    Security_6 72.0 65.52 87.48 78.5179 9% MEDICAL
    Security_7 19.0 16.74 23.76 22.6392 19% ENTERTAINMENT
    Security_8 61.0 53.36 66.95 65.1726 7% CHEMICALS
    Security_9 77.0 70.68 104.64 94.5287 23% INDUSTRIAL
    Security_10 7.0 5.46 7.63 6.70204 -4% COMPUTERS
    Security_11 39.0 37.05 51.23 48.3431 24% PHARMACEUTICALS
    Security_12 65.0 57.33 73.03 67.3899 4% COMPUTERS
    Security_13 1.0 0.0 1.09 1.13325 13% CONSTRUCTION
    Security_14 41.0 37.31 51.94 49.4056 21% SOFTWARE
    Security_15 5.0 3.68 5.45 4.69052 -6% SOFTWARE
    Security_16 94.0 88.27 114.45 102.45 9% INDUSTRIAL
    Security_17 55.0 50.96 73.44 67.5606 23% ENTERTAINMENT
    Security_18 46.0 42.77 59.95 55.4361 21% CONSTRUCTION
    Security_19 69.0 62.56 79.92 70.8087 3% MEDICAL
    Security_20 79.0 67.16 90.72 84.3288 7% INDUSTRIAL
    Security_21 19.0 17.29 22.68 20.0968 6% CONSTRUCTION
    Security_22 32.0 28.83 40.66 36.9896 16% BANKING
    Security_23 50.0 43.68 56.16 48.3558 -3% CONSTRUCTION
    Security_24 30.0 28.2 37.06 35.4026 18% SOFTWARE
    Security_25 53.0 47.84 61.56 53.2588 0% COMPUTERS
    Security_26 9.0 7.28 9.81 8.88172 -1% CONSTRUCTION
    Security_27 53.0 40.48 56.16 44.0786 -17% SOFTWARE
    Security_28 92.0 80.04 106.82 98.2253 7% BANKING
    Security_29 81.0 65.52 84.24 76.8155 -5% SOFTWARE

    Sector Total Volume
    ------ ------------
    MEDICAL 158925503582
    BANKING 132455298194
    ENTERTAINMENT 53923326500
    CHEMICALS 5919933998
    INDUSTRIAL 77504289736
    COMPUTERS 91801435033
    PHARMACEUTICALS 73108401702
    CONSTRUCTION 123129163362
    SOFTWARE 63635998914
    Sector with least volume:CHEMICALS[5919933998]
    Sector with most volume:MEDICAL[158925503582]

    Note that the TAs will execute your program on some hidden data that is also generated in the same way as this sample data was generated. All classes that you create for this problem must be properly documented using the JavaDoc standard.

    Hint: consider storing key information in an ArrayList and information about a security in a Hashtable.
    Hint: use NumberFormat pformat = NumberFormat.getPercentInstance(); to get an object that can be used to format percentages (pp. 62-78 of the book)

     

  3. [20 pts.] This question covers debugging. Sometimes the only way to make any progress on a program is to execute it line by line. You have been given some sample code for a Sudoku implementation. The goal is not to solve Sudoku, but only ensure that you place digits in empty squares. This code used to work fine. Then, I inserted ten (10) defects into the code.

    Your responsibility is to locate and fix the defects.

    SudokuDriver is supposed to load up an initial Sudoku board as defined within the "sudoku\\board.sar" file. You can assume that there is nothing incorrect about the format of this file. Note that spaces represent empty squares in the Sudoku board. Also note that the Sudoku board is composed of a set of rows and files (the columns). The files are labeled left to right as file 'a' to file 'i'. The rows are labeled from top to bottom, where the top row is 9 and the bottom row is 1. Once you remove the defects from the program, you can execute the program and enter the sequence of moves as found in the README.txt file in the sudoku package (found in the Examples directory in Sourceforge/Eclipse) which will fill in the particular Sudoku board and exit the program.

    However, there are ten defects in the code (in either the SudokoDriver class or the SudokuBoard class). Your task is to debug the program to identify the ten defects and fix them (you will list your ten found defects in a file called Q3-DEFECTS.txt and explain how you solved the problem). Once then verify that you are able to run the code on the given input files.

    All files can be found within SourceForge/Eclipse under the Examples project/Sudoku. Happy Hunting! I recommend that you:
     
    1. Print out a copy of the two class files to read over while you execute the code within the debugger.
    2. Set a breakpoint within the main method of the SudokuDriver class and then execute the program line by line. Over time you will identify the defects and, one by one, they will be removed.
    3. Be sure that you write down each defect as you find it, otherwise you might forget! Remember, we are looking for a list of TEN defects that you find in this assignment.
       
  4. [25 pts.] This question covers test case design. You are to write a JUnit class LinesTestCases with a set of tests for the following static method. You do not have access to the source code for the method. Your job is to write a full, robust set of test cases that you believe will properly test the execution of the method.  Place your test cases into a JUnit class LinesTestCase. We will execute our program against your test cases to determine whether your cases detect all of the errors in our (hidden) program. The more errors your tests uncover
     
    /**
     * Lines are a geometric construct within a two-dimensional plane.
     *
     * Two points (x1,y1) and (x2,y2) define a line, which stretches to infinity in
     * both directions.
     *
     * Given two lines in a two-dimensional Euclidian plane, it must be the case that
     * they are either:
     *
     * a) parallel to each other
     * b) coincident with each other (they actually represent the same infinite line)
     * c) intersect each other
     *
     * @author heineman
     */
    public class Lines {

     /** Declares that two lines are parallel. */
     public static final int PARALLEL = 1;
     
     /** Declares that two lines are, in fact, the exact same line. */
     public static final int COINCIDENT = 2;
     
     /** Declares that two lines intersect. */
     public static final int INTERSECTING = 3;
     
     /**
     * Given two infinite lines represented by the points (p1-p2) and (q1-q2)
     * determine the relationship between the lines:
     *
     * PARALLEL if the lines are parallel
     * COINCIDENT if the lines represent the same line in the plane
     * INTERSECTING if the lines intersect each other
     *
     * @param x1 x coordinate of point p1
     * @param y1 y coordinate of point p1
     * @param x2 x coordinate of point p2
     * @param y2 y coordinate of point p2
     * @param u1 x coordinate of point q1
     * @param v1 y coordinate of point q1
     * @param u2 x coordinate of point q2
     * @param v2 y coordinate of point q2
     *
     * @exception IllegalArgumentException if the points do not properly represent
     * two lines (p1-p2) and (q1-q2).
     *
     * @return type of relationship between two lines.
     */
     public static int intersection (int x1, int y1, int x2, int y2,
                                              int u1, int v1, int u2, int v2) throws IllegalArgumentException {

       // Note: You will not be able to run your JUnit Test cases! They will be run
       // on our own implementation of this method.
       throw new IllegalArgumentException ("Not Yet Implemented");

    }

All classes must be fully documented, according to the JavaDoc standard introduced in class and used throughout all class examples.

Deliverables

Your goal is to turnin the Project files by Tuesday December 12th at 10:00 AM  Further details will be posted HERE showing the preferred means of uploading your solution to the TAs.

Please be aware that no late homeworks will be accepted. This means that we will grade as zero any homework not submitted by the above turnin means.

Advanced

  1. Want to try some interesting coding? Implement the Lines.intersection method.

 


Notes

  1. [12/08/06 12:29 AM] updated sample output for Problem1 to start with 3 instead of 2
  2. [12/06/06 1:04 AM] Ready to go!
  3. [12/05/06 5:35 PM] Almost done with the assignment.

©2006 George T. Heineman