Using Java: Write a class StockHolding. The purpose of a StockHolding object is
ID: 3864980 • Letter: U
Question
Using Java: 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
Explanation / Answer
import java.util.*;
class StockHolding
{
private String symbol;
private int shareCount;
private double initialPrice;
private double currentPrice;
public StockHolding(String ticker, int numberShares, double initialPrice)
{
symbol = ticker;
shareCount = numberShares;
this.initialPrice = initialPrice;
currentPrice = initialPrice;
}
public void setCurrentSharePrice(double sharePrice){
currentPrice = sharePrice;
}
public String getTicker()
{
return symbol;
}
public int getShares()
{
return shareCount;
}
public double getInitialSharePrice()
{
return initialPrice;
}
public double getCurrentSharePrice()
{
return currentPrice;
}
public double getInitialCost()
{
return (shareCount * initialPrice);
}
public double getCurrentValue()
{
return (shareCount * currentPrice);
}
public double getCurrentProfit()
{
return (shareCount * (currentPrice - initialPrice));
}
public String toString(){
return ("Stock " + symbol + " , shares bought at " + initialPrice + ", current price " + currentPrice);
}
}
public class StockHoldingMain
{
public static void main(String []args){
int i = 0;
StockHolding st[] = new StockHolding[3];
st[0] = new StockHolding("Apple",10, 1001.32);
st[0].setCurrentSharePrice(1100.99);
st[1] = new StockHolding("Microsoft",14, 892.12);
st[1].setCurrentSharePrice(987.32);
st[2] = new StockHolding("Samsung",22, 679.43);
st[2].setCurrentSharePrice(721.11);
for(i=0; i<3 ;i++)
{
System.out.println("Creating a StockHolding for " + st[i].getShares() + " shares of " + st[i].getTicker() + " at $" + st[i].getInitialSharePrice());
System.out.println("and a current price of : " + st[i].getCurrentSharePrice());
System.out.println( st[i].getTicker() + " intial cost : $" + st[i].getInitialCost() );
System.out.println( st[i].getTicker() + " profit : " + st[i].getCurrentProfit());
System.out.println(st[i]);
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.