// This class represents a stock to trade on the market consisting of a name, a symbol, the current price per share // and the previous day's closing price per share. There is a no-arg constructor, a 2-arg constructor which gets // the name and symbol only (both constructors initialize the prices to 0) and a 3-arg constructor (which initializes // the previousClosingPrice to 0). There are accessors and mutators, a toString and a method to compute the // percent change between the last day's closing and the current price. Note that there is no mutator for // the previousClosingPrice, that is automatically set with the currentPrice whenever the day ends using the // endOfDay method. public class Stock { private String name, symbol; // Strings describing the name and abbreviation (symbol) of the stock private double previousClosingPrice, currentPrice; // yesterday's cost per share and the current cost per share public Stock() // no-arg constructor, no info, do the best we can to initialize a new stock { name = "unknown"; symbol = "xxx"; previousClosingPrice = currentPrice = 0.0; } public Stock(String n, String s) // new stock with no known costs { name = n; symbol = s; previousClosingPrice = currentPrice = 0.0; } public Stock(String n, String s, double p) // typical constructor for a new stock (no previous cost since its brand new) { name = n; symbol = s; previousClosingPrice = 0.0; currentPrice = p; } public void setName(String n) // mutators for name and symbol, currentPrice and previousClosingPrice will only change via endOfDay method { name = n; } public void setSymbol(String s) { symbol = s; } public String getName() { // accessors return name; } public String getSymbol() { return symbol; } public double getPrice() { return currentPrice; } public double getPreviousClosingPrice() { return previousClosingPrice; } public String toString() // toString to describe this stock (does not provide previousClosingPrice value) { return name + "\t" + symbol + "\t" + currentPrice; } public void endOfDay(double p) // at the end of each day, set the new value for currentPrice while assigning previousClosingPrice what today's currentPrice was { previousClosingPrice = currentPrice; currentPrice = p; // note: we should ensure p > 0 before doing this } public double getChangePercent() { return (currentPrice - previousClosingPrice)/currentPrice; } }