Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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: (50 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);

  1. 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.

Requirements for iteration 2: (50 points)

  1. Redesign and rewrite the first iteration using a deque instead of a standard queue.
    1. A typical stock transaction involves multiple shares and the two methods buy and sell reflect this in their parameters. However, when using a standard queue the implementation of myStocks.buy(30, 45) adds each of the 30 shares to a queue. The advantage of this approach is that sell can remove as many or as few shares as necessary. The disadvantage is that the queue storage can become quite large.
  2. Encapsulate the purchase of 30 shares into one object and add it to the queue. If we sell 20 of those shares, we would remove the object from the queue and compute the capital gain. We would have 10 shares that must remain in the queue.
  3. The 10 shares are the oldest shares and we cannot simply add them to the back of the queue; they must remain at the front. The standard queue has no operation that allows you to add an object to its front. A Deque does have this ability.
  4. Revise the class StockPurchase so that it represents the purchase of n shares of stock at d dollars per share. The revised class has the data fields share and cost, a constructor to initially set both data fields, and the accessor methods getNumberOfShares and getCostPerShare.
  5. Revise the class StockLedger with the ledger now an instance of a deque instead of a queue. YOU NEED TO WRITE THE DEQUE IMPLEMENTATION BASED ON THE INTERFACE AND DLLNODE CLASS GIVEN.
  6. The method buy creates an instance of StockPurchase and places it at the back of the deque.
  7. The method sell is now more involved. It must remove a StockPurchase object from the front of the deque and decide whether that object represents more shares than the number sold. If it does, the method creates a new instance of StockPurchase to represent the shares that remain in the portfolio. It then adds that instance to the front of the deque, since these shares would be sold next. The rest of the method performs the same tasks as in the previous iteration.
  8. Write an application program that allows the user to buy and sell stocks for a company. It has to use the StockPurchase and StockLedger 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. You can reuse the previous application and it should display the same results.

//------------------------------------------------------------------------

----

// DLLNode.java by Dale/Joyce/Weems

Chapter 4

//

// Implements nodes holding info of class for a doubly linked list.

//------------------------------------------------------------------------

----

package support;

public class DLLNode

{

private T info;

private DLLNode forward, back;

public DLLNode(T info)

{

this.info = info; forward = null; back = null;

}

public void setInfo(T info){this.info = info;}

public T getInfo(){return info;}

public void setForward(DLLNode forward){this.forward = forward;}

public void setBack(DLLNode back){this.back = back;}

public DLLNode getForward(){return forward;}

public DLLNode getBack(){return back;}

}

//------------------------------------------------------------------------

----

// DequeInterface.java by Dale/Joyce/Weems

Chapter 4

//

// Interface for a class that implements a deque of T.

// A deque is a linear structure allowing insertion/removal at both ends.

//------------------------------------------------------------------------

----

//package ch04.queues;

public interface DequeInterface

{

void enqueueFront(T element) throws QueueOverflowException;

// Throws QueueOverflowException if this queue is full;

// otherwise, adds element to the front of this queue.

void enqueueRear(T element) throws QueueOverflowException;

// Throws QueueOverflowException if this queue is full;

// otherwise, adds element to the rear of this queue.

T dequeueFront() throws QueueUnderflowException;

// Throws QueueUnderflowException if this queue is empty;

// otherwise, removes front element from this queue and returns it.

T dequeueRear() throws QueueUnderflowException;

// Throws QueueUnderflowException if this queue is empty;

// otherwise, removes rear element from this queue and returns it.

boolean isFull();

// Returns true if this queue is full; otherwise, returns false.

boolean isEmpty();

// Returns true if this queue is empty; otherwise, returns false.

int size();

// Returns the number of elements in this queue.

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management With Website Development Applications

Authors: Greg Riccardi

1st Edition

0201743876, 978-0201743876

More Books

Students also viewed these Databases questions

Question

Relevant costs vs. Irrelevant costs, what is the difference?

Answered: 1 week ago