Computer Science Investing in stocks is a way to create assets that are supposed to provide financial security over time. In solving this problem, we assume that an investor buys several shares of stock at a certain price. These shares are going to be sold later on for a different price. Obviously, if the selling price is higher than the acquisition price, the investor makes a profit, registering capital gain. If the shares are sold at a lower price, the investor has a loss, which marks a negative capital gain. This whole process is done over a period of time, and you are required to create a scenario for buying and selling shares. The assumption is that the investor sells shares in the order in which they were purchased. The goal is to calculate the capital gain over time. Suppose that you buy n shares of stock or mutual fund for d dollars each. Later, you sell some of these shares. If the sale price exceeds the purchase price, you have made a profit—a capital gain. On the other hand, if the sale price is lower than the purchase price, you experience a loss. We will designate a loss as a negative capital gain. Design a way to record your investment transactions chronologically and to compute the capital gain of any stock sale. Your Tasks Design a class, Stock, with instance variable cost. Write accessor and mutator methods, as well as a constructor that receives as argument the share cost, as a double. Design a class, StockTransaction, as a queue. You are free to implement this either array-based or link-based. The class should have a constructor in which you declare and instantiate the queue of stocks, along with a method "buy," which will record the stocks that are added to transactions, and a method "sell," which records the selling transactions, removing the shares that were sold from the queue. Build the transaction history and put it into a StockTransactionHistory.java file, in which you build the stock portfolio by buying a number of shares at various prices and selling some of them in the order they were purchased. This class will define, calculate, and display a variable, capitalGain, throughout the entire process. Display the capital gain after each sale. For a more realistic approach, you are free to research a stock market history, and take the stock value for a specific company of your interest. The history will give you real values of the shares at desired moments in time. If you implement all the required methods properly, the driver program should generate outputs similar to the following:
Computer Science
Investing in stocks is a way to create assets that are supposed to provide financial security over time. In solving this problem, we assume that an investor buys several shares of stock at a certain price. These shares are going to be sold later on for a different price. Obviously, if the selling price is higher than the acquisition price, the investor makes a profit, registering capital gain. If the shares are sold at a lower price, the investor has a loss, which marks a negative capital gain.
This whole process is done over a period of time, and you are required to create a scenario for buying and selling shares. The assumption is that the investor sells shares in the order in which they were purchased.
The goal is to calculate the capital gain over time.
Suppose that you buy n shares of stock or mutual fund for d dollars each. Later, you sell some of these shares. If the sale price exceeds the purchase price, you have made a profit—a capital gain. On the other hand, if the sale price is lower than the purchase price, you experience a loss. We will designate a loss as a negative capital gain.
Design a way to record your investment transactions chronologically and to compute the capital gain of any stock sale.
Your Tasks
Design a class, Stock, with instance variable cost. Write accessor and mutator methods, as well as a constructor that receives as argument the share cost, as a double.
Design a class, StockTransaction, as a queue. You are free to implement this either array-based or link-based. The class should have a constructor in which you declare and instantiate the queue of stocks, along with a method "buy," which will record the stocks that are added to transactions, and a method "sell," which records the selling transactions, removing the shares that were sold from the queue.
Build the transaction history and put it into a StockTransactionHistory.java file, in which you build the stock portfolio by buying a number of shares at various prices and selling some of them in the order they were purchased. This class will define, calculate, and display a variable, capitalGain, throughout the entire process.
Display the capital gain after each sale.
For a more realistic approach, you are free to research a stock market history, and take the stock value for a specific company of your interest. The history will give you real values of the shares at desired moments in time.
If you implement all the required methods properly, the driver program should generate outputs similar to the following:

Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 2 images









