This commit is contained in:
2025-04-29 17:51:52 +03:00
parent 9e95d7d514
commit 86ef6a2f43
21 changed files with 1272 additions and 0 deletions

View File

@ -0,0 +1,149 @@
/**
A class that implements the ADT queue by using an expandable
circular array with one unused location.
@author Frank M. Carrano
@author Timothy M. Henry
@version 4.0
*/
public final class ArrayQueue<T> implements QueueInterface<T>
{
private T[] queue; // Circular array of queue entries and one unused location
private int frontIndex;
private int backIndex;
private boolean initialized = false;
private static final int DEFAULT_CAPACITY = 50;
private static final int MAX_CAPACITY = 10000;
public ArrayQueue()
{
this(DEFAULT_CAPACITY);
} // end default constructor
public ArrayQueue(int initialCapacity)
{
checkCapacity(initialCapacity);
// The cast is safe because the new array contains null entries
@SuppressWarnings("unchecked")
T[] tempQueue = (T[]) new Object[initialCapacity + 1];
queue = tempQueue;
frontIndex = 0;
backIndex = initialCapacity;
initialized = true;
} // end constructor
public void enqueue(T newEntry)
{
checkInitialization();
ensureCapacity();
backIndex = (backIndex + 1) % queue.length;
queue[backIndex] = newEntry;
} // end enqueue
public T getFront()
{
checkInitialization();
if (isEmpty())
throw new EmptyQueueException();
else
return queue[frontIndex];
} // end getFront
public T dequeue()
{
checkInitialization();
if (isEmpty())
throw new EmptyQueueException();
else
{
T front = queue[frontIndex];
queue[frontIndex] = null;
frontIndex = (frontIndex + 1) % queue.length;
return front;
} // end if
} // end dequeue
// Doubles the size of the array queue if it is full
// Precondition: checkInitialization has been called.
private void ensureCapacity()
{
if (frontIndex == ((backIndex + 2) % queue.length)) // if array is full,
{ // double size of array
T[] oldQueue = queue;
int oldSize = oldQueue.length;
int newSize = 2 * oldSize;
checkCapacity(newSize);
// The cast is safe because the new array contains null entries
@SuppressWarnings("unchecked")
T[] tempQueue = (T[]) new Object[2 * oldSize];
queue = tempQueue;
for (int index = 0; index < oldSize - 1; index++)
{
queue[index] = oldQueue[frontIndex];
frontIndex = (frontIndex + 1) % oldSize;
} // end for
frontIndex = 0;
backIndex = oldSize - 2;
} // end if
} // end ensureCapacity
public boolean isEmpty()
{
return frontIndex == ((backIndex + 1) % queue.length);
} // end isEmpty
// Throws an exception if this object is not initialized.
private void checkInitialization()
{
if (!initialized)
{
throw new SecurityException ("ArrayQueue object is not initialized properly.");
}
}
// Throws an exception if the client requests a capacity that is too large.
private void checkCapacity(int capacity)
{
if (capacity > MAX_CAPACITY)
{
throw new IllegalStateException("Attempt to create a queue " +
"whose capacity exceeds " +
"allowed maximum.");
}
}
// Question 3, Chapter 11
public void clear()
{
checkInitialization();
if (!isEmpty())
{ // deallocates only the used portion
for (int index = frontIndex; index != backIndex; index = (index + 1) % queue.length)
{
queue[index] = null;
}
queue[backIndex] = null;
}
frontIndex = 0;
backIndex = queue.length - 1;
}
/*
// Question 4, Chapter 11
public void clear()
{
while (!isEmpty())
{
dequeue();
}
}
*/
} // end ArrayQueue

View File

@ -0,0 +1,13 @@
/** A class of runtime exceptions thrown when an attempt is made
to access or remove the front of a queue. @author Frank M. Carrano
*/
public class EmptyQueueException extends RuntimeException {
public EmptyQueueException() {
this(null);
} // end default
public EmptyQueueException(String message) {
super(message);
} // end constructor
} // end EmptyQueueException

BIN
DataLabs/lab5/Lab5.docx Normal file

Binary file not shown.

View File

@ -0,0 +1,101 @@
/**
A class that implements the ADT queue by using a chain of nodes
that has both head and tail references.
@author Frank M. Carrano
@author Timothy M. Henry
@version 4.0
*/
public final class LinkedQueue<T> implements QueueInterface<T>
{
private Node firstNode; // References node at front of queue
private Node lastNode; // References node at back of queue
public LinkedQueue()
{
firstNode = null;
lastNode = null;
} // end default constructor
public void enqueue(T newEntry)
{
Node newNode = new Node(newEntry, null);
if (isEmpty())
firstNode = newNode;
else
lastNode.setNextNode(newNode);
lastNode = newNode;
} // end enqueue
public T getFront()
{
if (isEmpty())
throw new EmptyQueueException();
else
return firstNode.getData();
} // end getFront
public T dequeue()
{
T front = getFront(); // Might throw EmptyQueueException
assert firstNode != null;
firstNode.setData(null);
firstNode = firstNode.getNextNode();
if (firstNode == null)
lastNode = null;
return front;
} // end dequeue
public boolean isEmpty()
{
return (firstNode == null) && (lastNode == null);
} // end isEmpty
public void clear()
{
firstNode = null;
lastNode = null;
} // end clear
private class Node
{
private T data; // Entry in queue
private Node next; // Link to next node
private Node(T dataPortion)
{
data = dataPortion;
next = null;
} // end constructor
private Node(T dataPortion, Node linkPortion)
{
data = dataPortion;
next = linkPortion;
} // end constructor
private T getData()
{
return data;
} // end getData
private void setData(T newData)
{
data = newData;
} // end setData
private Node getNextNode()
{
return next;
} // end getNextNode
private void setNextNode(Node nextNode)
{
next = nextNode;
} // end setNextNode
} // end Node
} // end LinkedQueue

View File

@ -0,0 +1,29 @@
/**
An interface for the ADT queue.
@author Frank M. Carrano
@author Timothy M. Henry
@version 4.0
*/
public interface QueueInterface<T>
{
/** Adds a new entry to the back of this queue.
@param newEntry An object to be added. */
public void enqueue(T newEntry);
/** Removes and returns the entry at the front of this queue.
@return The object at the front of the queue.
@throws EmptyQueueException if the queue is empty before the operation. */
public T dequeue();
/** Retrieves the entry at the front of this queue.
@return The object at the front of the queue.
@throws EmptyQueueException if the queue is empty. */
public T getFront();
/** Detects whether this queue is empty.
@return True if the queue is empty, or false otherwise. */
public boolean isEmpty();
/** Removes all entries from this queue. */
public void clear();
} // end QueueInterface

View File

@ -0,0 +1,29 @@
public class StockLedger {
private QueueInterface<StockPurchase> ledger;
public StockLedger() {
ledger = new LinkedQueue<>();
}
public void buy(int sharesBought, double pricePerShare){
while(!SharesBought) {
StockPurchase purchase = new StockPurchase(pricePerShare);
ledger.enqueue(purchase);
sharesBought--;
}
}
public double sell(int sharesSold, double pricePerShare){
double saleAmount = sharesSold*pricePerShare;
double totalCost = 0;
while (!sharesSold){
StockPurchase share = ledger.dequeue();
double shareCost = share.getCostPerShare();
totalCost += shareCost;
sharesSold--;
}
return saleAmount - totalCost;
}
}