Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Download: http://solutionzip.com/downloads/stock-java/
- 1. Define a class named Stock that contains the following variables and methods (note that none of the methods for this step should be static):
- a. A String variable named symbol for the stock symbol.
- b. A String variable named name for the stock’s name.
- c. A double variable named previousClosingPrice that stores the previous day’s stock price.
- d. A double variable named currentPrice that stores the stock price for the current time.
- e. A constructor that creates a new Stock. The constructor takes the stock’s symbol, name, and previous price as its arguments.
- f. A method named setPrice() that updates the value of the currentPrice variable with its argument. This method does not return any value.
- g. A method named getChangePercent() that returns the percentage difference between previousClosingPrice and currentPrice. In other words:
- percentage difference = 100 * (currentPrice – previousClosingPrice) / previousClosingPrice
- This method returns a double value, and does not take any arguments.
- h. A method named toString() that does not take any arguments, but returns a String value. This String should contain the stock symbol, its name, its current price, and the change percentage. For example, calling toString() on a Stock object might return a String like the following:
- MSTK My Stock, Ltd. 34.56 22.8
- 2. Add a main() method (or create a second class with a main() method). In your main() method:
- a. Create three Stock objects with the following information:
- i. Stock symbol “JAVA”, name “Sun Microsystems, Inc.”, and previous closing price of 4.5
- ii. Stock symbol “GOOG”, name “Google Inc.”, and previous closing price of 1007.95
- iii. Stock symbol “AAPL”, name “Apple Inc.”, and previous closing price of 512.49
- b. Set
- i. JAVA — 4.35
- the current price of each stock as follows:
- ii. GOOG — 1012.45
- iii. AAPL — 509.12
- c. Finally, print each stock’s information by passing each Stock variable to System.out.println(). This will automatically call the stock’s toString() method.
- Download: http://solutionzip.com/downloads/stock-java/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement