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:


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:

Solution

StockHoldingMain.java

import java.util.Scanner;


public class StockHoldingMain {
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print(\"Enter the ticker: \");
       String ticker = scan.next();
       System.out.print(\"Enter the shares: \");
       int shares = scan.nextInt();
       System.out.print(\"Enter the initial price: \");
       double initialPrice = scan.nextDouble();
       StockHolding s = new StockHolding(\"AAPL\",shares, initialPrice );
       System.out.print(\"Enter the current price: \");
       double currentSharePrice = scan.nextDouble();
       s.setCurrentSharePrice(currentSharePrice);
       System.out.println(\"Initial Cost: \"+s.getInitialCost());
       System.out.println(\"Profit Cost: \"+s.getCurrentProfit());
       System.out.println(s.toString());
   }
}

StockHolding.java


public class StockHolding {
   private String ticker ;
   private int shares ;
   private double initialSharePrice;
   private double currentSharePrice;
   public StockHolding(String ticker, int numberShares, double initialPrice) {
       this.ticker = ticker;
       this.shares = numberShares;
       this.initialSharePrice = initialPrice;
   }
   public String getTicker() {
       return ticker;
   }
   public void setTicker(String ticker) {
       this.ticker = ticker;
   }
   public int getShares() {
       return shares;
   }
   public void setShares(int shares) {
       this.shares = shares;
   }
   public double getInitialSharePrice() {
       return initialSharePrice;
   }
   public void setInitialSharePrice(double initialSharePrice) {
       this.initialSharePrice = initialSharePrice;
   }
   public double getCurrentSharePrice() {
       return currentSharePrice;
   }
   public void setCurrentSharePrice(double currentSharePrice) {
       this.currentSharePrice = currentSharePrice;
   }
   public double getInitialCost() {
       return shares * initialSharePrice;
   }
   public double getCurrentValue() {
       return shares * currentSharePrice;
   }
   public double getCurrentProfit() {
       return shares * (currentSharePrice-initialSharePrice);
   }
   public String toString(){
       return \"Stock \"+ticker+\", \"+shares+\", shares bought at \"+initialSharePrice+\", current price \"+currentSharePrice;
   }
}

Output:

Enter the ticker: AAPL
Enter the shares: 19
Enter the initial price: 103.97
Enter the current price: 105.5
Initial Cost: 1975.43
Profit Cost: 29.07000000000002
Stock AAPL, 19, shares bought at 103.97, current price 105.5

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 t
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 t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site