PUT IN JAVA PROGRAMMING
The Stock class:
Design a class named Stock that contains:
• A string data field named symbol1 for the stock’s symbol.
• A string data field named name for the stock’s name.
• A double data field named previousClosingPrice that stores the
stock price for the previous day.
• A double data field named currentPrice that stores the stock
price for the current time.
• A constructor that creates a stock with the specified symbol and
name.
• A method named getChangePercent() that returns the percentage
changed from previousClosingPrice to currentPrice.
• Write a test program that creates a Stock object with the stock
symbol ORCL, the name Oracle Corporation,
and the previous closing price of 34.5. Set a new current price to 34.35 and display the price-change percentage.
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! =========================================================================== public class Stock { private String symbol1; private String name; private double previousClosingPrice; private double currentPrice; public Stock(String symbol1, String name) { this.symbol1 = symbol1; this.name = name; previousClosingPrice = 0; currentPrice = 0; } public void setPreviousClosingPrice(double price) { this.previousClosingPrice = price; } public void setCurrentPrice(double price) { this.currentPrice = price; } public double getChangePercent() { double change = currentPrice -previousClosingPrice ; return change * 100 / previousClosingPrice; } } ================================================================= public class TestStock { public static void main(String[] args) { Stock aStock = new Stock("ORCL","Oracle"); aStock.setPreviousClosingPrice(34.5); aStock.setCurrentPrice(34.35); System.out.printf("aStock.getChangePercent() = %.2f%%\n", aStock.getChangePercent()); } }
=================================================================
Get Answers For Free
Most questions answered within 1 hours.