Write a class StockHolding. The purpose of a StockHolding object is to represent
ID: 3863661 • Letter: W
Question
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:
Below is class StockHoldingMain that contains main() method that should be used to test your program.
public class StockHoldingMain {
public static void main(String[] args) {
StockHolding apple = new StockHolding("AAPL", 19, 103.97);
StockHolding ibm = new StockHolding("IBM", 10, 160.8);
StockHolding oracle = new StockHolding("ORCL", 25, 40.76);
System.out.println("apple initial cost: " + apple.getInitialCost());
System.out.println("ibm initial cost: " + ibm.getInitialCost());
System.out.println("oracle initial cost: " + oracle.getInitialCost());
apple.setCurrentSharePrice(105.5);
ibm.setCurrentSharePrice(150.1);
oracle.setCurrentSharePrice(45.5);
System.out.println("apple profit: " + apple.getCurrentProfit());
System.out.println("ibm profit: " + ibm.getCurrentProfit());
System.out.println("oracle profit: " + oracle.getCurrentProfit());
System.out.println(apple.toString());
System.out.println(ibm.toString());
System.out.println(oracle.toString());
}
Explanation / Answer
JAVA PROGRAM :
import java.io.*;
import java.util.*;
class StockHolding {
private String ticker;
private int numberShares;
private double initialSharePrice, currentSharePrice;
public StockHolding(String ticker, int numberShares, double initialSharePrice) {
this.ticker = ticker;
this.numberShares = numberShares;
this.initialSharePrice = initialSharePrice;
this.currentSharePrice = initialSharePrice;
}
public String getTicker() {
return ticker;
}
public int getShares() {
return numberShares;
}
public double getInitialSharePrice() {
return initialSharePrice;
}
public double getCurrentSharePrice() {
return currentSharePrice;
}
public double getInitialCost() {
return numberShares * initialSharePrice;
}
public double getCurrentValue() {
return numberShares * currentSharePrice;
}
public double getCurrentProfit() {
return numberShares * (currentSharePrice - initialSharePrice);
}
public void setCurrentSharePrice(double currentSharePrice) {
this.currentSharePrice = currentSharePrice;
}
@Override
public String toString() {
return "Stock " + ticker + " , " + numberShares + " Shares bought at " + initialSharePrice + " Current Price " + currentSharePrice;
}
}
public class Main{
public static void main(String[] args) {
StockHolding apple = new StockHolding("AAPL", 19, 103.97);
StockHolding ibm = new StockHolding("IBM", 10, 160.8);
StockHolding oracle = new StockHolding("ORCL", 25, 40.76);
System.out.println("apple initial cost: " + apple.getInitialCost());
System.out.println("ibm initial cost: " + ibm.getInitialCost());
System.out.println("oracle initial cost: " + oracle.getInitialCost());
apple.setCurrentSharePrice(105.5);
ibm.setCurrentSharePrice(150.1);
oracle.setCurrentSharePrice(45.5);
System.out.println("apple profit: " + apple.getCurrentProfit());
System.out.println("ibm profit: " + ibm.getCurrentProfit());
System.out.println("oracle profit: " + oracle.getCurrentProfit());
System.out.println(apple.toString());
System.out.println(ibm.toString());
System.out.println(oracle.toString());
}
}
OUTPUT :
apple initial cost: 1975.43
ibm initial cost: 1608.0
oracle initial cost: 1019.0
apple profit: 29.07000000000002
ibm profit: -107.00000000000017
oracle profit: 118.50000000000006
Stock AAPL , 19 Shares bought at 103.97 Current Price 105.5
Stock IBM , 10 Shares bought at 160.8 Current Price 150.1
Stock ORCL , 25 Shares bought at 40.76 Current Price 45.5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.