Wednesday, February 9, 2011

My very simple Array-based Stack

// 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

Monday, February 7, 2011

some additional material from lecture #3

discussion of hw due tonight

void clear()
{
count = 0;
}
void pop()
{
count--;
}

discussion of next hw; due wed night
hw 1.2
instead of a max capacity of 100, it
will start at 100. if hits it, double it.
this, every time it hits the current capacity


Shape arr[] = new Shape[30];
i = 0;
arr[i] = new Rectangle(10, 10, 45, 56, "blue");
i++;

arr[i] = new Circle(40, 45, 56, "red");
i++;

for (int j = 0; j < i; j++)
arr[j].draw();

Stack<Shape> s = new Stack<Shape>();
s.push(new Rectangle(10, 10, 45, 56, "blue"));


exceptions

Sunday, February 6, 2011

A clarification regarding the first stack homework

You don't want to just use the existing stack class, which Java already has. You want to write your OWN stack class, and then demonstrate it using your driver program.

Saturday, February 5, 2011

Don't forget -- the first homework is due on Monday night!

Midnight, after class on Monday. So far, only two people have submitted their work. This is just homework 1.1, which is an array-based stack of fixed size.

Wednesday, February 2, 2011

lecture #2

http://www.cs.qc.edu/tutors.html

not yet updated

class A
{
public static void main(String args[])
{
Integer x = new Integer(6);
Integer y = 5;
int h[] = new int[1];
int j[] = new int[1];
swap(h, j);
swap(x, y);
System.out.println("" + x + " " + y);
}
// cannot do this; no pointers!
public static void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
// alas, I am copying these references by value
public static void swap(Integer a, Integer b)
{
int temp;
temp = a;
a = new Integer(b.value);
b = new Integer(temp);
}
public static void swap(const int **a, const int **b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

}

class MutableInteger
{
int value;
}

int retval;
retval = foo(x, y, &g);
if(retval == -1)
{

}
else if (retval == -2)
{


}
else if (retval < 0)
{


}
retval = bar(6, 4, k);
if(retval == -1)
{
cout << "file couldn't open";
exit(1);
}
else if (retval == -2)
{


}
else if (retval < 0)
{


}

foo(x, y, &g);
bar(6, 4, k);


try
{
foo(x, y, &g);
bar(6, 4, k);
}
catch(int i)
{
}
catch(double p)
{

}
catch(...)
{

}

Exception

class Shape
{
public:
virtual void draw() {}
}

class Triangle : public Shape
{
public:
draw() {}
}

class Square : public Shape
{
public:
draw() {}
}

foo(Shape *s)
{
s->draw();
}

main()
{
Shape *s[10];
s[0] = new Triangle();
s[1] = new Square();

}


class F
{
int y;
void foo() {}
public static void main(String args[])
{
F f = new F;
f.y;
f.foo();
}
}

Tuesday, February 1, 2011

First four lectures slides; homework

The first four PowerPoints have been added to Blackboard.

Also, homework assignment 1.1 added to Blackboard