Wednesday, February 9, 2011

lecture #4

// array-based stack
class JArrayStack
{
int capacity = 100;
int a[] = new int [100];
int i = 0;

public void push(int x) { a[i++] = x; }
public void pop() { i++; }
public int top() { return a[i-1]; }
public boolean isFull() { return i == capacity; }
public boolean isEmpty() { return i == 0; }
public int size() { return i; }
}

int [] temp = new int [capacity * 2];
copy from a into temp using for loop or memcopy
a = temp
capacity *= 2

debugger
you shouldn't use venus if you have a bug in your program
printf debugging

*setting breakpoints
*stepping thru code
*using the watch window

hw 1.3:
Templatize it! Use generics.
also, modify your driver to insert varying sizes, 0, 100, 200, 300, 400, 500 all the way to 20000. Plot the results in Excel.
Tell me, is it O(1)? O(n)? O(n^2)?

10 comments:

  1. professor i just wanted to ask you something about templates in java. i read power point slides about generics but it was not that much helpful. Since i have stackinterface into seperate class and MyStack.java seperate class and Main.java into seperate class in which do i add to? All of them? i tried that and didnt work, should i change all int's into Objects? and if i have public int push(int a) {
    return whatever; } would it look like this? public T push (Object a) {
    } ?

    ReplyDelete
  2. modify your MyStack.java
    e.x:

    class JArrayStack
    {
    }

    ReplyDelete
  3. i tried that but then it gave me errors in StackInterface.java class

    ReplyDelete
  4. well, at this stage, you don't need interfaces yet, so you could just abandon it for this portion of the assignment. however, you might want to try putting pointy brackets (less than and greater than signs) into your interface as well.

    push usually returns void, but it should take a T as a parameter. Thus:

    public void push (T a) {

    or if you want to return what was pushed in, for some reason, it would be:

    public T push (T a) {

    ReplyDelete
  5. so basically wherever i have int in methods should be changed to T. how about when i do T myArray = new T[100] this does not work

    ReplyDelete
  6. that is the one thing that Java does not support, an array of generics. for this, you can allocate an array of Objects, and then cast it to a T array.

    i think something like this:
    T [] myArray = (T []) new Object[100];

    don't forget the brackets when declaring your array.

    ReplyDelete
  7. Professor, I'm a little confused as to what you mean by plotting the results in Excel. Do you mean the times we got by using the stopwatch()?

    ReplyDelete
  8. yes. number of items inserted against time.

    ReplyDelete
  9. how do we get the times in milli seconds or in seconds. we need start time and end time right and then subtract start time from endtime and thats the time it took for the numbers to be inserted. i use for loop that inserts 20,000 random numbers into the stack by 100s. i mean i have 2 loops and first loop increments by 100 as second loop goes from 1 to 100. i just dont know what method (if any in java) to use to get the time

    ReplyDelete
  10. System.currentTimeMillis()

    but it looks like this might take too little time. i instead inserted up to 2 million elements, in increments of 100 thousand.

    ReplyDelete