Introduction to Computer Science I

Lab 5


Some preliminary setup

This is a two-part lab. For each part, I have already written part of the program, although I am going to provide it to you in a form where you use use it, but you cannot read it. For each part of this lab, you will need to write a new method (described in detail below). To test your code, you will use mine (also described below). Thus, your code must behave exactly as I describe here, or else my code will not work properly with yours.

To begin this lab, start by creating a lab-5 directory for yourself. Change into that directory, and then copy some files of mine that you will need, being sure not to forget the trailing period on the following command:

cp ~sfkaplan/public/cs11/lab-5/* .

Part 1 -- A bit of array manipulation

Open the file MyReverseArray.java with emacs. In it you will see a single method whose body is empty (and a comment that reads FILL IN YOUR CODE HERE. This method, named reverseArray, takes a pointer to an array of int. What it should do is reverse the contents of that array. For example, consider an array that is 3 elements long and has the following contents:

      [0] = 5
      [1] = 25;
      [2] = 8;
    

Once this method has done its work, the array should look like this:

      [0] = 8;
      [1] = 25;
      [2] = 5;
    

Notice that this method does not return anything (its return type is void). Therefore, you must reverse the elements of the array in-place -- you should not make an additional array of the same length and copy the contents. Insteadm you must shuffle around the contents of the array that was passed into the method.

Testing your solution: Notice that MyReverseArray.java does not contain a main method. That is on purpose -- it should not have its own main. Instead, I have written my own code, contained (in compiled form) in the file ReverseArray.class, that calls on your method. It is my program that you should run, and it will in turn use the code that you write in your reverseArray method within MyReverseArray.java.

My program creates two copies of an array full of random int values from 1 to 10,000. The first copy it reverses all on its own. The second copy is passed to your reverseArray method. It then compares its own reversed array to yours, and tells you whether your reversal is correct. To run my program, first try the following:

      java ReverseArray
    

The program will print the following message in response:

      USAGE: java ReverseArray  
    

This is the program's way of telling you that it expects command-line arguments; you must provide additional pieces of information on the line that you type to run the program. Specifically, this program needs to know both how long of an array to create, as well as whether you want the contents of the arrays to be printed along the way. To see how it works, try entering the following command:

      java ReverseArray 5 yes
    

The program will create two copies of a random array, 5 elements long. It will print one of the copies for you in its original order. It will then reverse its own copy and show you that reversal. Next, it will print the array that was passed to your reverseArray method. Finally, it will perform a comparison of the two reversed arrays, and tell you whether they match.

Your first step: Compile and run the code just as it was provided. That is, compile MyReverseArray.java, and then run my program as described above. Since the original reverseArray that I provide you has no body, it will not reverse the array at all, and you will see its failure to do so when my program runs.

Your assignment: Write the body of this method so that it performs an in-place reversal of the contents of the array passed into it. Test your code using my ReverseArray program, which should report that your reversed array and matches its own.


Part 2 -- Brute-force combinations

Remember, from class, our contrived problem of a list of future stock quotes for one specific stock? We sought to buy on one day, sell on a later day, and make the biggest profit possible.

This part of the assignment will be quite similar to the previous one. Specifically, I have provided two files: StockProfit.class and MyStockProfit.java. The former is a program that I have written to generate an array of stock prices, find the maximum profit that one can obtain, and print the results. It also calls on the method calculateMaxProfit in MyStockProfit.java, providing it the same array of stock prices and expecting it to search for the maximum profit. It then prints the results of calling that method, and compares the two searches.

Running the program: As above, you should first try running the program like this:

      java StockProfit
    

It will show you the command line arguments this program requires -- not coincidentally, they are the same as for the ReverseArray program. Go ahead and compile MyStockProfit.java, and then run the program like this:

      java StockProfit 5 yes
    

You will see the stock prices for 5 days, the correct answer, and then the answer generated by the calculateMaxProfit in MyStockProfit.java. Since that method has only a stub -- a little piece of code that allows the method to compile, but that doesn't really do any work -- the answer that it produces will be wrong.

Your assignment: Fill in the body of calculateMaxProfit in MyStockProfit.java. Have it perform a brute-force search of every possible buy and sell day, finding the one that yields the maximum profit. Note that this method should return an array of int. Specifically, it must return a two-element array, where the 0th entry contains the day on which to buy the stock, and the 1st entry contains the day on which to sell it in order to maximize profit. That is, you are returning the buy and sell days, but wrapping them up in an array whose length is 2.


Submitting your work

Submit your work like this:

cs11-submit lab-5 MyReverseArray.java MyStockProfit.java

This lab is due on Thursday, March 27th at 11:59 pm

Scott F. H. Kaplan
Last modified: Fri Feb 22 12:21:01 EST 2008