CS 101 (Spring 1999)
Lab 3: Stock Portfolios Implementation

Lab Assigned Design Due
(Mondays 2 PM)
Lab Due
(Fridays 2 PM)
29 Jan 1 Feb 5 Feb
By now, you should be familiar with a design for this lab. If the remarks on your graded lab design are encouraging, you should proceed by implementing that design. On the other hand, if your design appears to be flawed, or if you are unsure how to proceed, then you can take the files as they are given to you and implement them as described in this document.

A sample working version of this lab has been implemented by the instructor . You can run this to get an idea of how the project should work. If you are unsure about how something should work, please get help .

Before beginning, read this document thoroughly and make sure you understand it. Get help if necessary. The step-by-step suggested sequence for implementing the lab is at the end.

[[[ Download PC zip -- includes Attic.prj ]]]
Lab3.java
This is the official launch point for your lab, but this file serves only to invoke Startup, either as an Applet or as a standalone application. It may seem mysterious, but the invocation of Startup.start() causes Startup's run method to execute. For now, trust me--- CS 102 covers threads. You should not modify this file.
Startup.java
void run()
This class's run method is where things really get started for this lab. The method begins by setting up a GUI (graphical user interface) to drive the creation of stock portolios. TextFields are placed in the GUI to accept the portfolio name, two stock names, and an ainitial investment.

Some testing code follows, which invokes testStock and testMarketValue.

Following the tests, the method doesn't appear to do anything. This is because the class is listening to the events that happen in the GUI window, as described next.

void actionPerformed(ActionEvent)
This method is activated when the "Create it" button is pressed. The method retrieves the text strings in the GUI text fields. The investment amount is converted to an integer, and the information is passed to your Portfolio constructor.
testStock
As supplied, this code creates a single StockHeld.java holding (IBM) of 34 shares and prints it out. You will modify this method to do a more thorough test of your StockHeld class (assuming you agree you need one for the implementation).
void testMarketValue()
As supplied, this code creates a single market value price object of 3 5/8 dollars and prints it out. You will modify this method to do a more thorough job of testing the MarketValue class.
MarketValue.java
This object represents and manipulates values that are dollars and eighths of dollars. You have to fill in the (primary) constructor MarketValue(int dollars, int eighths) and the accessors. The other methods are as follows.
void increase(MarketValue howmuch)
This method mutates the objects value to reflect the sume of its current value and the supplied value howmuch. For example:
  MarketValue mv1 = new MarketValue(3,7);  //  3 7/8
  MarketValue mv2 = new MarketValue(4,6);  //  4 6/8
  mv1.increase(mv2);

  Terminal.println(mv1);    //  8 5/8
  Terminal.println(mv2);    //  4 6/8  (unchanged)
MarketValue add(MarketValue howmuch)
This is a non-mutating version of increase, exemplified as follows.
  MarketValue mv1 = new MarketValue(3,7);  //  3 7/8
  MarketValue mv2 = new MarketValue(4,6);  //  4 6/8
  MarketValue mv3 = mv1.add(mv2);          //  8 5/8

  Terminal.println(mv1);    //  3 7/8  (unchanged)
  Terminal.println(mv2);    //  4 6/8  (unchanged)
MarketValue by(int howmuch)
Non-mutating multiplication so we can compute the value of a number of shares.
  MarketValue mv1 = new MarketValue(3,7);  //  3 7/8
  MarketValue mv2 = mv1.by(4);             // 15 4/8  =  (3 7/8) * 4
boolean lessThan(MarketValue mv)
Returns true if the object's value is less than the supplied value. Otherwise, false is returned.
StockHeld.java
This object represents a number of shares of a particular stock. It features the following methods.
void increaseShares(int byHowMany)
Mutates the current number of shares to reflect the increase
void decreaseShares(int byHowMany)
Mutates the current number of shares to refelct the decrease
MarketValue getCurrentValue()
This is filled in for you, returning the current value of the number of shares held.
Portfolio.java
This represents a portfolio. The constructor is called by a press of the "Create it" button for this lab.
Constructor
This is filled in for you, though you may need to add code to it. The constructor saves the parameters in instance variables. Note that stock1, stock2, and the cash reserve are not saved in their passed-in form. Instead, they are saved as instances of the appropriate objects: StockHeld and MarketValue. The initial investment is saved in the instance variable initialinvest so that the graphical display can be scaled properly.

The constructor creates a SegmentDisplay that can be used to plot the value of the portfolio (for extra credit).

Finally, the constructor makes a new ControlPortfolio which is a GUI to control this instance of a Portfolio. Its operation is explained below.

Accessor Methods
These are filled in for you -- so you can concentrate on the other parts of this class.
The "current" stock
In this lab, each portfolio contains exactly two stocks: stock1 and stock2. At any point in time, one of these is designated the "current" stock. The choice of current stock is under control of the ControlPortfolio GUI. The GUI tells your instance how to change the notion of the current stock by calling the following methods.
setCurrentStockToStock1
The GUI calls this method when the current stock should be stock1. In fact, the current stock may already be stock1, but you need not worry about that.

To represent the "current" stock, you need an instance variable, such as the curStock variable discussed in class. Be sure to initailize the variable properly in the class's constructor.

You can change the value of curStock to stock1 by assigning

  curStock = stock1;
setCurrentStockToStock2
Likewise for stock2
buy(int shares)
This method requests the purchase of the the specified number of shares of the current stock. Use the value of curStock to do the work for you. That is, do not do the following:
  if (curStock == stock1) {
     /* use stock1 to buy stuff */
  }
  else {
     /* use stock2 to buy stuff */
  }
You can use curStock to act on behalf of stock1 or stock2, depending on the current value of curStock.

To realize the purchase, do the following steps.

  1. Compute the current purchase price of the specified number of shares of the current stock.

    To do this, you can create an instance of a StockHeld object of the requested number of shares. You can then use its getCurrentValue method to find its value. This may seem an unusual thing to do, since you won't keep this object around, but that's OK.

  2. See if there is enough money in the portofolio's reserve. You may find MarketValue's lessThan method useful in obtaining this answer.
  3. If there is enough money, then decrement the reserve appropriately and increase the number of shares in curStock
sell(int shares)
Similar to buy, but you must make sure the portfolio owns enough shares (of curStock) to cover the sale.
MarketValue getCurrentMarketValue()
This method must return the current value of the portfolio: the cash reserve and the current value of its shares of stocks.
DirectionVector.java and SegmentDisplay
The code is from Lab 2.

Suggested approach:

  1. Read over the Quote class and see how it works. It basically just bundles two strings, maintaining some simple invariants along the way.
  2. Stock quotes or obtained by getStockQuote in Lab3.java. You are not expected to understand the details of this piece of code! But you should understand and be able to use the API: give the method a stock name (String) and it returns a Quote.
  3. Follow the instructions in the MarketValue.java class to get that class working. Be sure to take small steps, adding some code, and then compiling or testing.

    Put some testing code in testMarketValue in Startup.java so you can thoroughly test the MarketValue class.

  4. Repeat the above step for the StockHeld.java class to get that class working. Test sensibly.
  5. Follow the instructions in Portfolio.java to get that class working. Be sure to save the showPerformance activity for last -- it is tricky and counts as extra credit.

What To Turn In:

The following items should be stapled together (in order) and turned into the correct CS101 bin (by your section letter) by 2 P.M. on the due date. (Beginning with Lab 2, all work you turn in should be prepared according to the CS101 Style Guide.) Double check that you have completed all header information.

  1. The proper cover sheet printed on one page, or obtained from the top of the bins.
  2. A paper printout of the following java files
  3. A printout of your transcript file containing your test results.
If you are not in your lab section to give a demonstration, we will assume that your program doesn't work.

Last modified 13:51:16 CST 01 February 1999 by Ron K. Cytron