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

Lab Assigned Design Due
(Mondays 2 PM)
Lab Due
(Fridays 2 PM)
29 Jan 1 Feb 5 Feb

### Overview:

The value of an investor in the stock market can be represented and maintained in a portfolio for that investor. The portfolio contains all information needed to buy and sell stocks that are held by the investor. In our simple world, each portfolio will contain exactly two stocks. Each portfolio also has a reserve of money. Initially, the money is deposited in the portfolio when it is created. Subsequently, the money is affected by the buying and selling of shares for the two stocks that are held.

### Some particulars:

• The current value of a portfolio is its cash reserve plus the current market value of the shares of stock that are held.
• The current value of a single share of a particular stock can be obtained by
```  Quote  Lab3.getStockQuote(String stockName)
```
An API for `Quote` is given below.
• You will also find it useful to convert from a `String` that contains a numeral to the `int` number for the `String`. The following methods are provided in Lab3.java.
• ```boolean Lab3.stringToIntOK(String)
```
returns `true` if conversion can occur without error. Otherwise, `false` is returned.
• ```int Lab3.stringToInt(String)
```
returns the integer value of the supplied string. If the string has an error, then 0 is returned.
• In our system, money and stock worth are represented by MarketValue---an object type (class). We follow Wall Street in representing money by dollars and eighths of dollars.
• In the implementation, you will show the value of a portfolio in a `SegmentDisplay` window from Lab 2.

The system you design and build will contain at least the following kinds of objects (classes):
`Portfolio`
A portfolio contains
• a cash reserve
• some number of shares of one stock (Stock 1)
• some number of shares of another stock (Stock 2)
• the notion of a current stock -- the stock (Stock 1 or Stock 2) that is currently being bought or sold.
You must provide methods for the buying and selling of a portfolio's current stock. You must also provide a method that sets the current stock to Stock 1 or Stock 2.

Things to consider:

• What do the constructors look like?
• What instance variables are needed?
• How do you name your methods?
• What is the type signature for your methods that buy and sell stock? In particular,
• Does one buy and sell stocks by specifying a number of shares, or an amount of money that should be spent on the transaction?
• Can you allow for both?
• How do you represent the number and value of the shares of Stock 1 and Stock 2 that are held? For example, do you use `int`s or some class of your own design?
`MarketValue`
This class represents money in the form of dollars and eighths of dollars. For example
```  MarketValue v1 = new MarketValue(3,4);
```
creates a value that is 3 dollars and 4/8 dollars, or \$3.50. The `toString` method for `MarketValue` must print out the value in stock form: 3 4/8.

Although `MarketValue` may appear to be similar to `Quote`, you should view `Quote` as an interface for obtaining stock quotes, while `MarketValue` is much more general. In particular, think about methods you need to do the following.

• Let's say you have n shares of a stock, each with `MarketValue` v. What is the `MarketValue` of the n shares? Where does the method live that computes this? What is its type signature? Does it mutate to return the right value or does it create a new object to represent the value?
• You might think about having mutator and nonmutator methods that do similar things. For example, you might think of
```  MarketValue plus(MarketValue addend)
```
that returns a new `MarketValue` that represents the sum of `this` and the `addend`'s `MarketValue`. You might also have
``` void increase(MarketValue howmuch)
```
that increments `this` `MarketValue` by `howmuch`.

### What to turn in:

1. The cover sheet, properly completed
2. For each class in your design, including the `Portfolio` and `MarketValue` classes, provide the following.
• A brief explanation of the intention and behavior of the class.
• The API:
• Instance Variables
• Constructors
• Accessors
• Mutators
• Other methods
• Any useful invariants for the class. Recall in class that a bank account had the invariant
```balance >= 0
```
3. Be sure to do the same for any extra classes in your design.
Choose names and type signatures (that is, the API) for the methods in the classes of your design, paying attention to
• The names you use--they should be informative but not overly verbose.
• The style you use in spelling the names.
• The extent to which your methods will have unclear side-effects.
• The width of the methods' interface. For example, instead of passing a pair of values (such as dollars and eighths of dollars) around as separate parameters, you might invent an object to represent such pairs.

### `Quote` API

As an example of an API, and to assist you in developing your solution in Lab, the API for the `Quote` class is as follows.
Constructors
• `Quote(String dollars, String eighths)` constructs an immutable pair of Strings. Blanks are trimmed from the strings. If dollars or eighths is not positive, or not a number, then the associated instance variable is set to "0".
• `Quote( )` constructs a `Quote` for 0 dollars and 0 eighths.
• `Quote(String dollars)` constructs a `Quote` for the specified dollars and 0 eighths.
Public Accessors
• `String getDollars()` returns the dollars.
• `String getEighths()` returns the eighths.
Other Public Methods
• `String toString( )` returns a string with the dollars and eighths.
Public Fields
none
Private Methods
none
Private Fields
• `dollars` is the trimmed string containing dollars
• `eighths` is the trimmed string containing eighths
Invariants
• getDollars() returns a nonegative numeral
• getEighths() returns a nonnegative numeral whose value is less than 8.
Exceptional behavior
none