473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Stacks and ArrayList

mia023
89
Hello everyone.



Assume that we have previously defined the following class StockPurchase that has the following as instance variables
a) The name of the stock (a string)
b) The number of shares of a stock (an int)
c) The purchase price (can be a decimal)



Part-2) Write a class called ManageMultipleStocks and its corresponding driver. Your program should allow the user to enter information about purchases of various stocks: their names, the amount of shares, and the prices. The user can then enter a query about the cost of a given stock according to the LIFO or FIFO accounting methods for a certain number of shares.
The ManageMultipleStocks could have the following methods:
void addStockPurchase(StockPurchase sp);
double getLIFOCost(String stockName, int numShares);
double getFIFOCost(String stockName, int numShares);

Hint: ManageMultipleStocks will have a collection of stacks (a Vector or ArrayList of ObjectStack) and a collection of queues (a Vector or ArrayList of ObjectQueue). You might also have ManageMultipleStocks contain a collection of ManageSingleStock objects.







My question is just an explaination of the problem and why do we need to use ArrayList here and in addStockPurchase(StockPurchase sp) do we have to do ArrayList list=new ArrayList();
list.add(sp);


I don't need the code I just need an explaination for the problem.
May 20 '08 #1
11 5890
mia023
89
Hello everyone.



Assume that we have previously defined the following class StockPurchase that has the following as instance variables
a) The name of the stock (a string)
b) The number of shares of a stock (an int)
c) The purchase price (can be a decimal)



Part-2) Write a class called ManageMultipleStocks and its corresponding driver. Your program should allow the user to enter information about purchases of various stocks: their names, the amount of shares, and the prices. The user can then enter a query about the cost of a given stock according to the LIFO or FIFO accounting methods for a certain number of shares.
The ManageMultipleStocks could have the following methods:
void addStockPurchase(StockPurchase sp);
double getLIFOCost(String stockName, int numShares);
double getFIFOCost(String stockName, int numShares);

Hint: ManageMultipleStocks will have a collection of stacks (a Vector or ArrayList of ObjectStack) and a collection of queues (a Vector or ArrayList of ObjectQueue). You might also have ManageMultipleStocks contain a collection of ManageSingleStock objects.







My question is just an explaination of the problem and why do we need to use ArrayList here and in addStockPurchase(StockPurchase sp) do we have to do ArrayList list=new ArrayList();
list.add(sp);


I don't need the code I just need an explaination for the problem.
Any suggestions or help is also helpful!!
May 20 '08 #2
Laharl
849 Expert 512MB
You need those two lines first to initialize the ArrayList so it can be used and second to add the object to the list. That said, I question why the first line is in the method rather than the constructor, where it belongs.
May 20 '08 #3
r035198x
13,262 8TB
A few points to look up and read about are :
ArrayList vs array
ArrayList vs Vector

Google for those. They should be a good starting point.
May 20 '08 #4
JosAH
11,448 Expert 8TB
You need those two lines first to initialize the ArrayList so it can be used and second to add the object to the list. That said, I question why the first line is in the method rather than the constructor, where it belongs.
I'm afraid that when this thread really gets started the OP simply tries to add
those few magical lines here and there in the code and then come back here.
I hope I'm wrong ...

kind regards,

Jos
May 20 '08 #5
mia023
89
I'm afraid that when this thread really gets started the OP simply tries to add
those few magical lines here and there in the code and then come back here.
I hope I'm wrong ...

kind regards,

Jos
I didn't understand what should be done
May 20 '08 #6
mia023
89
I didn't understand what should be done
Expand|Select|Wrap|Line Numbers
  1. ]import java.util.*;
  2. public class ManageSingleStock{
  3.     ObjectStack stack;
  4.  
  5.     public ManageSingleStock(){    //constructor
  6.         stack=new ObjectStack();
  7.     }
  8.  
  9.     public void addStockPurchase(StockPurchase sp){    //add
  10.         stack.push(sp);
  11.     }
  12.  
  13.     public double getLIFOCost(int num_shares){    //stack
  14.         double cost = 0;
  15.         int num_needed    = num_shares;
  16.  
  17.         while(num_needed > 0){    //loop until remaining shares needed not equals to zero
  18.  
  19.             StockPurchase sp = (StockPurchase)stack.peek();
  20.  
  21.             if(num_needed >= sp.numOfshares){    //case where we need shares more than the available shares in peek()
  22.                 cost += sp.numOfshares*sp.purchase_price;
  23.                 num_needed -= sp.numOfshares;
  24.                 stack.pop();
  25.             }
  26.             else{    //case where we need shares less than the available shares in peek()
  27.                 cost += num_needed*sp.purchase_price;
  28.                 sp.numOfshares -= num_needed;
  29.                 num_needed = 0;
  30.             }
  31.         }
  32.  
  33.         cost = cost/num_shares;    //calculate the average cost
  34.  
  35.         return cost;
  36.     }
  37.  
  38.     public double getFIFOCost(int num_shares){    //queue
  39.         double cost = 0;
  40.         int num_needed    = num_shares;
  41.  
  42.         ObjectStack stack1 = new ObjectStack();
  43.  
  44.         //stack the stack => queue
  45.         while(!stack.isEmpty())
  46.             stack1.push(stack.pop());
  47.  
  48.         //same like before
  49.         while(num_needed > 0){
  50.  
  51.             StockPurchase sp = (StockPurchase)stack1.peek();
  52.  
  53.             if(num_needed >= sp.numOfshares){
  54.                 cost += sp.numOfshares*sp.purchase_price;
  55.                 num_needed -= sp.numOfshares;
  56.                 stack.pop();
  57.             }
  58.             else{
  59.                 cost += num_needed*sp.purchase_price;
  60.                 sp.numOfshares -= num_needed;
  61.                 num_needed = 0;
  62.             }
  63.         }
  64.  
  65.         cost = cost/num_shares;
  66.  
  67.         //stack the queue => stack
  68.         while(!stack1.isEmpty())
  69.             stack.push(stack1.pop());
  70.  
  71.         return cost;
  72.     }
  73.  
  74.     public String toString(){
  75.         stack.trimToSize();
  76.         return stack.toString();
  77.     }
  78.  
  79.     public static void main(String[]args){
  80.         ManageSingleStock st=new ManageSingleStock();
  81.         Scanner in=new Scanner(System.in);
  82.         st.addStockPurchase( new StockPurchase("A", 100, 500));
  83.         st.addStockPurchase( new StockPurchase("B", 3, 1000));    
  84.         st.addStockPurchase( new StockPurchase("C", 4, 250));
  85.         System.out.print("Do you want to getFIFOCOST(Press 1) or getLIFOCOST(Press 2) ?");    
  86.         int num=in.nextInt();
  87.         System.out.println();
  88.         if(num==1){
  89.             double x =st.getFIFOCost(5);
  90.             System.out.println("FIFO:\t" +x );
  91.             System.out.println(st);
  92.         }    
  93.         if(num==2){
  94.             double y=st.getLIFOCost(5);
  95.              System.out.println("LIFO:\t"+y);
  96.             System.out.println(st);
  97.         }
  98.     }
  99. }
This is my code but one a single stock
May 20 '08 #7
JosAH
11,448 Expert 8TB
So what is your ObjectStack class like?

kind regards,

Jos
May 20 '08 #8
mia023
89
So what is your ObjectStack class like?

kind regards,

Jos
import java.util.EmptyStackException;

/************************************************** ****************************
* An <CODE>ObjectStack</CODE> is a stack of references to objects.
*
* <dl><dt><b>Limitations:</b>
* <dd>
* (1) The capacity of one of these stacks can change after it's created, but
* the maximum capacity is limited by the amount of free memory on the
* machine. The constructor, <CODE>ensureCapacity</CODE>, <CODE>push</CODE>,
* and <CODE>trimToSize</CODE> will result in an
* <CODE>OutOfMemoryError</CODE> when free memory is exhausted.
* <dd>
* (2) A stack's capacity cannot exceed the maximum integer 2,147,483,647
* (<CODE>Integer.MAX_VALUE</CODE>). Any attempt to create a larger capacity
* results in a failure due to an arithmetic overflow.
* </dl>
*
* <dt><b>Java Source Code for this class:</b><dd>
* <A HREF="../edu/colorado/collections/ObjectStack.java">
* http://www.cs.colorado.edu/~main/edu/colorado/collections/ObjectStack.java
* </A>
*
* @author Michael Main
* <A HREF="mailto:main@colorado.edu"> (main@colorado.edu) </A>
*
* @version
* Jun 12, 1998
*
* @see ObjectLinkedStack
* @see BooleanStack
* @see ByteStack
* @see CharStack
* @see DoubleStack
* @see FloatStack
* @see IntStack
* @see LongStack
* @see ShortStack
************************************************** ****************************/
public class ObjectStack implements Cloneable
{
// Invariant of the ObjectStack class:
// 1. The number of items in the stack is in the instance variable manyItems.
// 2. For an empty stack, we do not care what is stored in any of data; for a
// non-empty stack, the items in the stack are stored in a partially-filled array called
// data, with the bottom of the stack at data[0], the next item at data[1], and so on
// to the top of the stack at data[manyItems-1].
private Object[ ] data;
private int manyItems;


/**
* Initialize an empty stack with an initial capacity of 10. Note that the
* <CODE>push</CODE> method works efficiently (without needing more
* memory) until this capacity is reached.
* @param - none
* <dt><b>Postcondition:</b><dd>
* This stack is empty and has an initial capacity of 10.
* @exception OutOfMemoryError
* Indicates insufficient memory for:
* <CODE>new Object[10]</CODE>.
**/
public ObjectStack( )
{
final int INITIAL_CAPACITY = 10;
manyItems = 0;
data = new Object[INITIAL_CAPACITY];
}


/**
* Initialize an empty stack with a specified initial capacity. Note that the
* <CODE>push</CODE> method works efficiently (without needing more
* memory) until this capacity is reached.
* @param <CODE>initialCapacity</CODE>
* the initial capacity of this stack
* <dt><b>Precondition:</b><dd>
* <CODE>initialCapacity</CODE> is non-negative.
* <dt><b>Postcondition:</b><dd>
* This stack is empty and has the given initial capacity.
* @exception IllegalArgumentException
* Indicates that initialCapacity is negative.
* @exception OutOfMemoryError
* Indicates insufficient memory for:
* <CODE>new Object[initialCapacity]</CODE>.
**/
public ObjectStack(int initialCapacity)
{
if (initialCapacity < 0)
throw new IllegalArgumentException
("initialCapacity too small " + initialCapacity);
manyItems = 0;
data = new Object[initialCapacity];
}


/**
* Generate a copy of this stack.
* @param - none
* @return
* The return value is a copy of this stack. Subsequent changes to the
* copy will not affect the original, nor vice versa. Note that the return
* value must be type cast to an <CODE>ObjectStack</CODE> before it can be used.
* @exception OutOfMemoryError
* Indicates insufficient memory for creating the clone.
**/
public Object clone( )
{ // Clone an ObjectStack.
ObjectStack answer;

try
{
answer = (ObjectStack) super.clone( );
}
catch (CloneNotSupportedException e)
{
// This exception should not occur. But if it does, it would probably indicate a
// programming error that made super.clone unavailable. The most comon error
// The most common error would be forgetting the "Implements Cloneable"
// clause at the start of this class.
throw new RuntimeException
("This class does not implement Cloneable");
}

answer.data = (Object [ ]) data.clone( );

return answer;
}


/**
* Change the current capacity of this stack.
* @param <CODE>minimumCapacity</CODE>
* the new capacity for this stack
* <dt><b>Postcondition:</b><dd>
* This stack's capacity has been changed to at least <CODE>minimumCapacity</CODE>.
* If the capacity was already at or greater than <CODE>minimumCapacity</CODE>,
* then the capacity is left unchanged.
* @exception OutOfMemoryError
* Indicates insufficient memory for: <CODE>new Object[minimumCapacity]</CODE>.
**/
public void ensureCapacity(int minimumCapacity)
{
Object biggerArray[ ];

if (data.length < minimumCapacity)
{
biggerArray = new Object[minimumCapacity];
System.arraycopy(data, 0, biggerArray, 0, manyItems);
data = biggerArray;
}
}


/**
* Accessor method to get the current capacity of this stack.
* The <CODE>push</CODE> method works efficiently (without needing
* more memory) until this capacity is reached.
* @param - none
* @return
* the current capacity of this stack
**/
public int getCapacity( )
{
return data.length;
}


/**
* Determine whether this stack is empty.
* @param - none
* @return
* <CODE>true</CODE> if this stack is empty;
* <CODE>false</CODE> otherwise.
**/
public boolean isEmpty( )
{
return (manyItems == 0);
}


/**
* Get the top item of this stack, without removing the item.
* @param - none
* <dt><b>Precondition:</b><dd>
* This stack is not empty.
* @return
* the top item of the stack
* @exception EmptyStackException
* Indicates that this stack is empty.
**/
public Object peek( )
{
if (manyItems == 0)
// EmptyStackException is from java.util and its constructor has no argument.
throw new EmptyStackException( );
return data[manyItems-1];
}


/**
* Get the top item, removing it from this stack.
* @param - none
* <dt><b>Precondition:</b><dd>
* This stack is not empty.
* <dt><b>Postcondition:</b><dd>
* The return value is the top item of this stack, and the item has
* been removed.
* @exception EmptyStackException
* Indicates that this stack is empty.
**/
public Object pop( )
{
if (manyItems == 0)
// EmptyStackException is from java.util and its constructor has no argument.
throw new EmptyStackException( );
return data[--manyItems];
}


/**
* Push a new item onto this stack. If the addition
* would take this stack beyond its current capacity, then the capacity is
* increased before adding the new item. The new item may be the null
* reference.
* @param <CODE>item</CODE>
* the item to be pushed onto this stack
* <dt><b>Postcondition:</b><dd>
* The item has been pushed onto this stack.
* @exception OutOfMemoryError
* Indicates insufficient memory for increasing the stack's capacity.
* <dt><b>Note:</b><dd>
* An attempt to increase the capacity beyond
* <CODE>Integer.MAX_VALUE</CODE> will cause the stack to fail with an
* arithmetic overflow.
**/
public void push(Object item)
{
if (manyItems == data.length)
{
// Double the capacity and add 1; this works even if manyItems is 0. However, in
// case that manyItems*2 + 1 is beyond Integer.MAX_VALUE, there will be an
// arithmetic overflow and the bag will fail.
ensureCapacity(manyItems*2 + 1);
}
data[manyItems] = item;
manyItems++;
}


/**
* Accessor method to determine the number of items in this stack.
* @param - none
* @return
* the number of items in this stack
**/
public int size( )
{
return manyItems;
}


/**
* Reduce the current capacity of this stack to its actual size (i.e., the
* number of items it contains).
* @param - none
* <dt><b>Postcondition:</b><dd>
* This stack's capacity has been changed to its current size.
* @exception OutOfMemoryError
* Indicates insufficient memory for altering the capacity.
**/
public void trimToSize( )
{
Object trimmedArray[ ];

if (data.length != manyItems)
{
trimmedArray = new Object[manyItems];
System.arraycopy(data, 0, trimmedArray, 0, manyItems);
data = trimmedArray;
}
}
public String toString(){
String s = "";
for(int i=0;i<data.length;i++){
s+=data[i]+" , ";
}
return "[ " + s + "]";
}

}




























import java.util.NoSuchElementException;

/************************************************** ****************************
* An <CODE>ObjectQueue</CODE> is a queue of references to objects.
*
* <dl><dt><b>Limitations:</b>
* <dd>
* (1) The capacity of one of these queues can change after it's created, but
* the maximum capacity is limited by the amount of free memory on the
* machine. The constructor, <CODE>add</CODE>, <CODE>clone</CODE>,
* and <CODE>union</CODE> will result in an
* <CODE>OutOfMemoryError</CODE> when free memory is exhausted.
* <dd>
* (2) A queue's capacity cannot exceed the maximum integer 2,147,483,647
* (<CODE>Integer.MAX_VALUE</CODE>). Any attempt to create a larger capacity
* results in a failure due to an arithmetic overflow.
* </dl>
*
* <dt><b>Java Source Code for this class:</b><dd>
* <A HREF="../edu/colorado/collections/ObjectQueue.java">
* http://www.cs.colorado.edu/~main/edu/colorado/collections/ObjectQueue.java
* </A>
*
* @author Michael Main
* <A HREF="mailto:main@colorado.edu"> (main@colorado.edu) </A>
*
* @version
* Jun 12, 1998
*
* @see ObjectLinkedQueue
* @see BooleanQueue
* @see ByteQueue
* @see CharQueue
* @see DoubleQueue
* @see FloatQueue
* @see IntQueue
* @see LongQueue
* @see ShortQueue
************************************************** ****************************/
public class ObjectQueue implements Cloneable
{
// Invariant of the ObjectQueue class:
// 1. The number of items in the queue is in the instance variable manyItems.
// 2. For a non-empty queue, the items are stored in a circular array
// beginning at data[front] and continuing through data[rear].
// 3. For an empty queue, manyItems is zero and data is a reference to an
// array, but we don't care about front and rear.
private Object[ ] data;
private int manyItems;
private int front;
private int rear;


/**
* Initialize an empty queue with an initial capacity of 10. Note that the
* <CODE>insert</CODE> method works efficiently (without needing more
* memory) until this capacity is reached.
* @param - none
* <dt><b>Postcondition:</b><dd>
* This queue is empty and has an initial capacity of 10.
* @exception OutOfMemoryError
* Indicates insufficient memory for:
* <CODE>new Object[10]</CODE>.
**/
public ObjectQueue( )
{
final int INITIAL_CAPACITY = 10;
manyItems = 0;
data = new Object[INITIAL_CAPACITY];
// We don't care about front and rear for an empty queue.
}


/**
* Initialize an empty queue with a specified initial capacity. Note that the
* <CODE>insert</CODE> method works efficiently (without needing more
* memory) until this capacity is reached.
* @param <CODE>initialCapacity</CODE>
* the initial capacity of this queue
* <dt><b>Precondition:</b><dd>
* <CODE>initialCapacity</CODE> is non-negative.
* <dt><b>Postcondition:</b><dd>
* This queue is empty and has the given initial capacity.
* @exception IllegalArgumentException
* Indicates that initialCapacity is negative.
* @exception OutOfMemoryError
* Indicates insufficient memory for:
* <CODE>new Object[initialCapacity]</CODE>.
**/
public ObjectQueue(int initialCapacity)
{
if (initialCapacity < 0)
throw new IllegalArgumentException
("initialCapacity is negative: " + initialCapacity);
manyItems = 0;
data = new Object[initialCapacity];
// We don't care about front and rear for an empty queue.
}


/**
* Generate a copy of this queue.
* @param - none
* @return
* The return value is a copy of this queue. Subsequent changes to the
* copy will not affect the original, nor vice versa. Note that the return
* value must be type cast to an <CODE>ObjectQueue</CODE> before it can be used.
* @exception OutOfMemoryError
* Indicates insufficient memory for creating the clone.
**/
public Object clone( )
{ // Clone an ObjectQueue.
ObjectQueue answer;

try
{
answer = (ObjectQueue) super.clone( );
}
catch (CloneNotSupportedException e)
{
// This exception should not occur. But if it does, it would probably indicate a
// programming error that made super.clone unavailable. The most comon error
// The most common error would be forgetting the "Implements Cloneable"
// clause at the start of this class.
throw new RuntimeException
("This class does not implement Cloneable");
}

answer.data = (Object [ ]) data.clone( );

return answer;
}


/**
* Change the current capacity of this queue.
* @param <CODE>minimumCapacity</CODE>
* the new capacity for this queue
* <dt><b>Postcondition:</b><dd>
* This queue's capacity has been changed to at least <CODE>minimumCapacity</CODE>.
* If the capacity was already at or greater than <CODE>minimumCapacity</CODE>,
* then the capacity is left unchanged.
* @exception OutOfMemoryError
* Indicates insufficient memory for: <CODE>new Object[minimumCapacity]</CODE>.
**/
public void ensureCapacity(int minimumCapacity)
{
Object biggerArray[ ];
int n1, n2;

if (data.length >= minimumCapacity)
// No change needed.
return;
else if (manyItems == 0)
// Just increase the size of the array because the queue is empty.
data = new Object[minimumCapacity];
else if (front <= rear)
{ // Create larger array and copy data[front]...data[rear] into it.
biggerArray = new Object[minimumCapacity];
System.arraycopy(data, front, biggerArray, front, manyItems);
data = biggerArray;
}
else
{ // Create a bigger array, but be careful about copying items into it. The queue items
// occur in two segments. The first segment goes from data[front] to the end of the
// array, and the second segment goes from data[0] to data[rear]. The variables n1
// and n2 will be set to the number of items in these two segments. We will copy
// these segments to biggerArray[0...manyItems-1].
biggerArray = new Object[minimumCapacity];
n1 = data.length - front;
n2 = rear + 1;
System.arraycopy(data, front, biggerArray, 0, n1);
System.arraycopy(data, 0, biggerArray, n1, n2);
front = 0;
rear = manyItems-1;
data = biggerArray;
}
}


/**
* Accessor method to get the current capacity of this queue.
* The <CODE>insert</CODE> method works efficiently (without needing
* more memory) until this capacity is reached.
* @param - none
* @return
* the current capacity of this queue
**/
public int getCapacity( )
{
return data.length;
}


/**
* Get the front item, removing it from this queue.
* @param - none
* <dt><b>Precondition:</b><dd>
* This queue is not empty.
* <dt><b>Postcondition:</b><dd>
* The return value is the front item of this queue, and the item has
* been removed.
* @exception NoSuchElementException
* Indicates that this queue is empty.
**/
public Object getFront( )
{
Object answer;

if (manyItems == 0)
throw new NoSuchElementException("Queue underflow");
answer = data[front];
front = nextIndex(front);
manyItems--;
return answer;
}


/**
* Insert a new item in this queue. If the addition
* would take this queue beyond its current capacity, then the capacity is
* increased before adding the new item. The new item may be the null
* reference.
* @param <CODE>item</CODE>
* the item to be pushed onto this queue
* <dt><b>Postcondition:</b><dd>
* The item has been pushed onto this queue.
* @exception OutOfMemoryError
* Indicates insufficient memory for increasing the queue's capacity.
* <dt><b>Note:</b><dd>
* An attempt to increase the capacity beyond
* <CODE>Integer.MAX_VALUE</CODE> will cause the queue to fail with an
* arithmetic overflow.
**/
public void insert(Object item)
{
if (manyItems == data.length)
{
// Double the capacity and add 1; this works even if manyItems is 0. However, in
// case that manyItems*2 + 1 is beyond Integer.MAX_VALUE, there will be an
// arithmetic overflow and the bag will fail.
ensureCapacity(manyItems*2 + 1);
}

if (manyItems == 0)
{
front = 0;
rear = 0;
}
else
rear = nextIndex(rear);

data[rear] = item;
manyItems++;
}


/**
* Determine whether this queue is empty.
* @param - none
* @return
* <CODE>true</CODE> if this queue is empty;
* <CODE>false</CODE> otherwise.
**/
public boolean isEmpty( )
{
return (manyItems == 0);
}


private int nextIndex(int i)
// Precondition: 0 <= i and i < data.length
// Postcondition: If i+1 is data.length,
// then the return value is zero; otherwise
// the return value is i+1.
{
if (++i == data.length)
return 0;
else
return i;
}


/**
* Accessor method to determine the number of items in this queue.
* @param - none
* @return
* the number of items in this queue
**/
public int size( )
{
return manyItems;
}


/**
* Reduce the current capacity of this queue to its actual size (i.e., the
* number of items it contains).
* @param - none
* <dt><b>Postcondition:</b><dd>
* This queue's capacity has been changed to its current size.
* @exception OutOfMemoryError
* Indicates insufficient memory for altering the capacity.
**/
public void trimToSize( )
{
Object trimmedArray[ ];
int n1, n2;

if (data.length == manyItems)
// No change needed.
return;
else if (manyItems == 0)
// Just change the size of the array to 0 because the queue is empty.
data = new Object[0];
else if (front <= rear)
{ // Create trimmed array and copy data[front]...data[rear] into it.
trimmedArray = new Object[manyItems];
System.arraycopy(data, front, trimmedArray, front, manyItems);
data = trimmedArray;
}
else
{ // Create a trimmed array, but be careful about copying items into it. The queue items
// occur in two segments. The first segment goes from data[front] to the end of the
// array, and the second segment goes from data[0] to data[rear]. The variables n1
// and n2 will be set to the number of items in these two segments. We will copy
// these segments to trimmedArray[0...manyItems-1].
trimmedArray = new Object[manyItems];
n1 = data.length - front;
n2 = rear + 1;
System.arraycopy(data, front, trimmedArray, 0, n1);
System.arraycopy(data, 0, trimmedArray, n1, n2);
front = 0;
rear = manyItems-1;
data = trimmedArray;
}
}
public String toString(){
String s = "";
for(int i=0;i<data.length;i++){
s+=data[i]+" , ";
}
return "[ " + s + "]";
}
}



Note that I didn't write these classes I downloaded them from the enternet
May 20 '08 #9
Laharl
849 Expert 512MB
There are a few issues here:
1) Don't bold code, use the provided CODE tags instead.
2) This is clearly a homework assignment. If that code isn't part of your class, you're cheating. I hope my read is wrong...
May 20 '08 #10
mia023
89
There are a few issues here:
1) Don't bold code, use the provided CODE tags instead.
2) This is clearly a homework assignment. If that code isn't part of your class, you're cheating. I hope my read is wrong...
It is an assignment type but this code(ObjectStack-ObjectQueue) was said to be downloaded from the enternet. I told you at first that I don't need the answer I just need to understand why do we need to use ArrayList and why do we need the name of the stock.
May 21 '08 #11
Laharl
849 Expert 512MB
Ok. Sorry I misjudged you, our policies are quite strict on that subject, as are those of most professors. You don't necessarily need an ArrayList, you could use an array or a Vector or a LinkedList or anything else that can store a bunch of objects. As to why you need the identifier of the StockPurchase object, it's so you can add it to the collection you're using to store them.
May 21 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Vanessa T. | last post by:
Hello All! Is there a place where I can learn stacks, (push and pop) etc. Thanks,
6
by: Sathyaish | last post by:
I've searched Google and found a few, but I am not so satisfied. Any good reading on "stacks and heaps" about how and when memory is allocated from the heap?
17
by: Frank Rizzo | last post by:
Hello, doing some profiling work and trying to resolve the memory usage balooning through the roof. Just want to make sure that my reasoning is correct. My question is as follows: Let's say I...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
10
by: Rich Kucera | last post by:
Holding all versions at 5.0.4, Multiple stacks with multiple-version configurations inevitable Will have to wait to see what the impact of problems such as http://bugs.php.net/bug.php?id=33643 ...
2
by: Daniel | last post by:
Hi, I have a question regarding the memory managment in stl stacks. I want to use stacks to store a very large amount of numbers (some millions), thus I'm interested in how the stack behaves...
0
by: raghuveer | last post by:
i want to implement multiple stacks using arrays..I am able to create ,insert and print them but not poping an element form any of the stack..This is what i have #include<stdio.h>...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
8
by: cerise | last post by:
I can't figure out how to make and handle multiple stacks and use them so I could create four linked list stacks representing each suit of cards, one stack each for diamonds, hearts, spades, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.