Write a class StockHolding The purpose of a StockHolding obj
Write a class StockHolding. The purpose of a StockHolding object is to represent a single stock in someone\'s investment portfolio. The StockHolding class has the following specification: instance variable of type String for the ticker symbol of the stock instance variable of type int for the number of shares held instance variable of type double for the initial price of one share instance variable of type double for the current price of one share constructor StockHolding(String ticker, int numberShares, double initialPrice) // current share price is initialized to the same value as initialPrice accessors String getTicker() int getShares() double getInitialSharePrice() double getCurrentSharePrice() double getInitialCost() // number of shares * initial price double getCurrentValue() // number of shares * current price double getCurrentProfit() // number of shares * (current price - initial price) String toString() // returns \"stock , shares bought at , current price mutators void setCurrentSharePrice(double sharePrice) Write a class StockHoldingMain that contains a main method. The main method should create three StockHolding objects. For each object, print the initial cost, set the current share price, print the current profit, and print the toString value. The following is sample output from a main method creating a StockHolding for 19 shares of Apple at $103.97 and a current price of 105.5: apple initial cost: 1975.43 apple profit: 29.069999999999936 Stock AAPL, 19, shares bought at 103.97, current price 105.5
Solution
StockHolding.java
public class StockHolding {
private String m_ticker;
private int m_numShares;
private double m_initialSharePrice;
private double m_currentSharePrice;
public StockHolding(String ticker, int numberShares, double initialPrice) {
m_ticker = ticker;
m_numShares = numberShares;
m_initialSharePrice = initialPrice;
m_currentSharePrice = initialPrice;
}
public double getCurrentValue() {
return m_numShares * getCurrentSharePrice();
}
public double getInitialCost() {
return m_numShares * getInitialSharePrice();
}
public double getCurrentProfit() {
return getCurrentValue() - getInitialCost();
}
public double getCurrentSharePrice() {
return m_currentSharePrice;
}
public void setCurrentSharePrice(double currentSharePrice) {
m_currentSharePrice = currentSharePrice;
}
public String getTicker() {
return m_ticker;
}
public int getShares() {
return m_numShares;
}
public double getInitialSharePrice() {
return m_initialSharePrice;
}
@Override
public String toString() {
return \"Stock \" + getTicker() + \", \" + getShares() + \", shares bought at \" + getInitialSharePrice()
+ \", current price \" + getCurrentSharePrice();
}
}
StockHoldingMain.java
public class StockHoldingMain {
public static void main(String args[])
{
StockHolding stockHolding = new StockHolding(\"Apple\", 19, 103.97);
stockHolding.setCurrentSharePrice(105.5);
System.out.println(stockHolding.toString());
}
}


