Using queues, write two implementations to compute the Capital Gain in a sale of
ID: 3604078 • Letter: U
Question
Using queues, write two implementations to compute the Capital Gain in a sale of stock. Suppose that you buy n shares of a stock for d dollars each. Later when you sell some of these shares, if the sales price exceeds the purchase price, you have made a profit which is called a capital gain. If the sale price is lower than the purchase price, you have a loss which is called a negative capital gain.
Investors buy shares in a particular company over a period of time. For example, last year you bought 20 shares of Amazon at $45 per share. Last month, you bought 20 additional shares at $75 per share and today, you sold 30 shares at $65 per share. What is your capital gain? Well, which of your 40 shares did you actually sell? Unfortunately, you cannot pick and choose. When computing capital gains, you must assume that you sell shares in the order in which you purchased them. Stock sales are a first-in-first-out application. In our example, you sold 20 shares that you bought at $45 each and 10 of the shares that you bought at $75 each. Your cost for the $30 shares is $1650. You sold them for $1950, a capital gain of $300. You will design a way to record your investment transactions chronologically and to compute the capital gain of any stock.
Requirements for iteration 1: (25 points)
1. For the first iteration use a standard queue implementation from chapter 4. Assume that all transactions are for stocks of a single company and there is no commission charge for the transactions. Write a class called StockPurchase that records the cost of a single share of stock. (see below)
2. Write a class called StockLedger that enables us to record stock purchases in chronological order. (see below)
3. At the time of sale, the StockLedger class computes the capital gain and updates the record of stocks owned. It contains a queue that stores each share individually. (20 shares equal 20 enqueues into the queue.)
4. Write an application program that allows the user to buy and sell stocks for a company. It has to use the StockLedger and StockPurchase classes. For every sale compute the capital gain and display the result. If the capital gain is positive it is a profit, if the capital gain is negative it is a loss.
5. The following statements demonstrate how we could use StockLedger in our application program to record the transactions given in the problem set:
StockLedger myStocks = new StockLedger();
myStocks.buy(20, 45);
myStocks.buy(20, 75);
double capGain = myStocks.sell(30, 65);
6. StockLedger records instances of StockPurchase which represents the shares we own in a queue. The queue orders the shares so we can sell them in the order in which we purchased them. The method buy enqueues each share that is bought. The method sell removes from the queue as many shares as are sold. As it does this, it computes the total capital gain from the sale and returns it.
Explanation / Answer
// class StockLedger definition
class StockLedger
{
// Initial capacity of stockLedger
private int capacity = 20;
// To store number of shares
int shares[];
// To store each share price
double price[];
// Initializes front to zero
int front = 0;
// Initializes rear to -1
int rear = -1;
// Initializes currentSize to zero
int currentSize = 0;
// Default constructor
public StockLedger()
{
// Allocates memory to array shares
shares = new int[this.capacity];
// Allocates memory to array price
price = new double[this.capacity];
}// End of constructor
// Method adds element at the rear of the queue.
public void addShare(int currentShare, double currentPrice)
{
// Calls the method to checks whether queue is full
if (isQueueFull())
{
// If full displays message
System.out.println("Queue is full, increase capacity...");
// Calls the method to increase the capacity
increaseCapacity();
}// End of if
// Increase the rear by one
rear++;
// Checks if rear is greater then or equals to share length and current size is not equals to shares length
if(rear >= shares.length && currentSize != shares.length)
{
// Sets the rear to zero
rear = 0;
}// End of if
// Assigns currentShare value in the array shares in the rear index position
shares[rear] = currentShare;
// Assigns currentPrice value in the array price in the rear index position
price[rear] = currentPrice;
currentSize++;
System.out.println("Adding: Share: " + shares[rear] + " price: " + price[rear]);
}// End of method
// Method removes an element from the front of the queue
public void removeShare()
{
// Calls the method to checks whether queue is full
if (isQueueEmpty())
{
System.out.println("Underflow ! Unable to remove element from Queue");
} // End of if
// Otherwise
else
{
// Increase the front by one
front++;
// Checks if front is greater than one less than the shares length
if(front > shares.length-1)
{
// Displays the removed share and price value
System.out.println("removed: Share: " + shares[front-1] + " Price: " + price[front-1]);
// Sets the front to zero
front = 0;
} // End of if
// Otherwise
else
{
// Displays the removed share and price value
System.out.println("removed: Share: " + shares[front-1] + " Price: " + price[front-1]);
}// End of inner else
// Decreases the current size by one
currentSize--;
}// End of outer else
}// End of method
// Method checks whether the queue is full or not
public boolean isQueueFull()
{
// Initializes the false to status
boolean status = false;
// Checks if the current size is equals to shares length
if (currentSize == shares.length)
{
// Assigns true to status
status = true;
}// End of if
// Returns status
return status;
}// End of method
// Method checks whether the queue is empty or not
public boolean isQueueEmpty()
{
// Initializes the false to status
boolean status = false;
// Checks if the current size is equals to zero
if (currentSize == 0)
{
// Assigns true to status
status = true;
}// End of if
// Returns status
return status;
}// End of method
private void increaseCapacity()
{
// Create new array with increasing the size by capacity i.e., 20
int newCapacity = this.shares.length + capacity;
// Allocates memory to newShare with new capacity
int[] newShare = new int[newCapacity];
// Allocates memory to newPrice with new capacity
double[] newPrice = new double[newCapacity];
// Copy elements to new array, copy from rear to front
// Creates a temporary front and store the front value
int tmpFront = front;
// Initializes the current index to -1
int index = -1;
// Loops till current size is equals to index value plus one
while(true)
{
// Allocates each old share value to the new share
newShare[++index] = this.shares[tmpFront];
// Allocates each old price value to the new price
newPrice[++index] = this.price[tmpFront];
// Increase the temporary front by one
tmpFront++;
// Checks if the temporary front value is equal to current shares length
if(tmpFront == this.shares.length)
{
// Sets the temporary front value to zero
tmpFront = 0;
}// End of if
// current size is equals to index value plus one then stop
if(currentSize == index+1)
break;
}// End of while
// Make new array as queue
this.shares = newShare;
this.price = newPrice;
// Displays the new capacity
System.out.println("New array capacity: " + this.shares.length);
// Reset front & rear values
this.front = 0;
this.rear = index;
}// End of method
// Method to by a share
public void buy(int sh, double pri)
{
// Calls the method to add the share to queue
addShare(sh, pri);
}// End of method
// Method to sell a share
public double sell(int sh, double pri)
{
// Initializes profit or loss variable to zero
double profitLoss = 0;
// Calculates sell share with price to calculate sell amount
double sellAmt = sh * pri;
// Initializes purchase amount to zero
double purAmt = 0;
// To store remaining share
int rem = 0;
// Loops till sell shares is zero
do
{
// Extracts current shares
int currentShare = shares[front];
// Extracts current price
double currentPrice = price[front];
// Checks if the current share is greater than the sold share
if(currentShare > sh)
{
// Calculates the purchase amount
purAmt += sh * currentPrice;
// Calls the method to remove the shares and price from queue
removeShare();
// Calculates the remaining purchased share
rem = currentShare - sh;
// Calculates remaining sold share
sh -= rem;
// Calls the method to add the unsold remaining share and price back to the queue
addShare(rem, currentPrice);
}// End of if condition
// Otherwise current share is less than the sold share
else
{
// Calculates the purchase amount
purAmt += currentShare * currentPrice;
// Calculates remaining sold share
sh -= currentShare;
// Calls the method to remove the shares and price from queue
removeShare();
}// End of else
}while(sh != 0);
// Calculates profit or loss
profitLoss = sellAmt - purAmt;
return profitLoss;
}// End of method
}// End of class
// Driver class StockLedgerDemo definition
public class StockLedgerDemo
{
// main method definition
public static void main(String[] args)
{
// StockLedger class object created
StockLedger queue = new StockLedger();
// Calls the method to buy share
queue.buy(20, 45);
// Calls the method to buy share
queue.buy(20, 75);
// Calls the method to sell share
double capGain = queue.sell(30, 65);
// Checks the capital gain if it is greater than zero then profit otherwise loss
if(capGain > 0)
System.out.println("Profit = " + capGain);
else
System.out.println("Loss = " + Math.abs(capGain));
}// End of main method
}// End of class
Sample Run:
Adding: Share: 20 price: 45.0
Adding: Share: 20 price: 75.0
removed: Share: 20 Price: 45.0
removed: Share: 20 Price: 75.0
Adding: Share: 10 price: 75.0
Profit = 300.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.