| Lab | Assigned | Design Due (Mondays 2 PM) |
Lab Due (Fridays 2 PM) |
|||
|---|---|---|---|---|---|---|
| 29 | Jan | 1 | Feb | 5 | Feb | |
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.
Lab3.java
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()
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)
Portfolio constructor.
testStock
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()
MarketValue class.
MarketValue.java
MarketValue(int dollars, int eighths) and the accessors.
The other methods are as follows.
void increase(MarketValue howmuch)
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)
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)
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)
true if the object's value is less than the supplied
value. Otherwise, false is returned.
StockHeld.java
void increaseShares(int byHowMany)
void decreaseShares(int byHowMany)
MarketValue getCurrentValue()
Portfolio.java
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.
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
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
stock2
buy(int shares)
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.
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.
MarketValue's
lessThan method useful in obtaining this answer.
curStock
sell(int shares)
curStock) to cover the sale.
MarketValue getCurrentMarketValue()
DirectionVector.java and SegmentDisplay
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.
Put some testing code in testMarketValue
in Startup.java so you can thoroughly test the
MarketValue class.
showPerformance
activity for last -- it is tricky and counts as extra credit.
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.
Startup.java
(contains your test code for StockHeld and MarketValue)
MarketValue.java
StockHeld.java
Portfolio.java