Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
COMP 213Advanced Object-oriented Programming
Lecture 4
Implementing ADTs
Java principles of ADT implementation
• Principle 1: Fields do the work of data representation.
• Principle 2: Constructors are (often) used to instantiate fields, and to create/construct instances.
• Principle 3: Methods implement the operations of the ADT in the way specified by the user.
Java principles of ADT implementation
• Principle 1: Fields do the work of data representation.
• Principle 2: Constructors are (often) used to instantiate fields, and to create/construct instances.
• Principle 3: Methods implement the operations of the ADT in the way specified by the user.
Java principles of ADT implementation
• Principle 1: Fields do the work of data representation.
• Principle 2: Constructors are (often) used to instantiate fields, and to create/construct instances.
• Principle 3: Methods implement the operations of the ADT in the way specified by the user.
Implementing the Stack
• A stack is an ADT; it is a list which has (at least) two operations:
• push – add an element onto the top of the stack.
• pop – remove the most recently added element.
• It enforces “LIFO” (last-in-first-out) policy on additions and deletions.
• Other common operations:
• peek – return the top element without removing it.
• size – return the number of elements.
• isEmpty – return true if no elements are in it, otherwise return false.
Implementing the Stack
• A stack is an ADT; it is a list which has (at least) two operations:
• push – add an element onto the top of the stack.
• pop – remove the most recently added element.
• It enforces “LIFO” (last-in-first-out) policy on additions and deletions.
• Other common operations:
• peek – return the top element without removing it.
• size – return the number of elements.
• isEmpty – return true if no elements are in it, otherwise return false.
Implementing the Stack
• A stack is an ADT; it is a list which has (at least) two operations:
• push – add an element onto the top of the stack.
• pop – remove the most recently added element.
• It enforces “LIFO” (last-in-first-out) policy on additions and deletions.
• Other common operations:
• peek – return the top element without removing it.
• size – return the number of elements.
• isEmpty – return true if no elements are in it, otherwise return false.
Implementing a Stack of Strings
class MyStack {
public static final int MaxSize = 100;
private static int top = -1;
private static String[] store = new String[MaxSize];
...
}
Implementing a Stack of Strings
class MyStack {
public static final int MaxSize = 100;
private static int top = -1;
private static String[] store = new String[MaxSize];
...
}
Implementing a Stack of Strings
class MyStack {
public static final int MaxSize = 100;
private static int top = -1;
private static String[] store = new String[MaxSize];
...
}
Implementing a Stack of Strings
class MyStack {
public static final int MaxSize = 100;
private static int top = -1;
private static String[] store = new String[MaxSize];
...
}
Implementing a Stack of Strings
public static void push(String newItem) {
if (top < MaxSize - 1) {
top++;
store[top] = newItem;
} else {
System.out.println(“Sorry, stack full.”);
}
}
Implementing a Stack of Strings
public static String pop() {
String ans = "";
if (top >= 0) {
ans = store[top];
top--;
} else {
System.err.println("Oops, stack empty.");
}
return ans;
}
Implementing a Stack of Strings
public static int size() {
return top + 1;
}
Testing your Stack
class testingMyStack {
private static String moduleName = “COMP213";
public static void main(String[] args) {
MyStack.push("COMP101");
MyStack.push(moduleName);
String s = MyStack.pop();
System.out.println("popped:" + s);
MyStack.push("COMP109");
s = MyStack.pop();
System.out.println("popped:" + s);
s = MyStack.pop();
System.out.println("popped:" + s);
// The stack should be empty now
s = MyStack.pop();
}
}
Testing your Stack
class testingMyStack {
private static String moduleName = “COMP213";
public static void main(String[] args) {
MyStack.push("COMP101");
MyStack.push(moduleName);
String s = MyStack.pop();
System.out.println("popped:" + s);
MyStack.push("COMP109");
s = MyStack.pop();
System.out.println("popped:" + s);
s = MyStack.pop();
System.out.println("popped:" + s);
// The stack should be empty now
s = MyStack.pop();
}
}
Modifiers to be examined in the forthcoming lectures
Testing your Stack
class testingMyStack {
private static String moduleName = “COMP213";
public static void main(String[] args) {
MyStack.push("COMP101");
MyStack.push(moduleName);
String s = MyStack.pop();
System.out.println("popped:" + s);
MyStack.push("COMP109");
s = MyStack.pop();
System.out.println("popped:" + s);
s = MyStack.pop();
System.out.println("popped:" + s);
// The stack should be empty now
s = MyStack.pop();
}
}
Testing your Stack
class testingMyStack {
private static String moduleName = “COMP213";
public static void main(String[] args) {
MyStack.push("COMP101");
MyStack.push(moduleName);
String s = MyStack.pop();
System.out.println("popped:" + s);
MyStack.push("COMP109");
s = MyStack.pop();
System.out.println("popped:" + s);
s = MyStack.pop();
System.out.println("popped:" + s);
// The stack should be empty now
s = MyStack.pop();
}
}
Testing your Stack
class testingMyStack {
private static String moduleName = “COMP213";
public static void main(String[] args) {
MyStack.push("COMP101");
MyStack.push(moduleName);
String s = MyStack.pop();
System.out.println("popped:" + s);
MyStack.push("COMP109");
s = MyStack.pop();
System.out.println("popped:" + s);
s = MyStack.pop();
System.out.println("popped:" + s);
// The stack should be empty now
s = MyStack.pop();
}
}
Testing your Stack
class testingMyStack {
private static String moduleName = “COMP213";
public static void main(String[] args) {
MyStack.push("COMP101");
MyStack.push(moduleName);
String s = MyStack.pop();
System.out.println("popped:" + s);
MyStack.push("COMP109");
s = MyStack.pop();
System.out.println("popped:" + s);
s = MyStack.pop();
System.out.println("popped:" + s);
// The stack should be empty now
s = MyStack.pop();
}
}
Testing your Stack
class testingMyStack {
private static String moduleName = “COMP213";
public static void main(String[] args) {
MyStack.push("COMP101");
MyStack.push(moduleName);
String s = MyStack.pop();
System.out.println("popped:" + s);
MyStack.push("COMP109");
s = MyStack.pop();
System.out.println("popped:" + s);
s = MyStack.pop();
System.out.println("popped:" + s);
// The stack should be empty now
s = MyStack.pop();
}
}
Testing your Stack
class testingMyStack {
private static String moduleName = “COMP213";
public static void main(String[] args) {
MyStack.push("COMP101");
MyStack.push(moduleName);
String s = MyStack.pop();
System.out.println("popped:" + s);
MyStack.push("COMP109");
s = MyStack.pop();
System.out.println("popped:" + s);
s = MyStack.pop();
System.out.println("popped:" + s);
// The stack should be empty now
s = MyStack.pop();
}
}
Testing your Stack
class testingMyStack {
private static String moduleName = “COMP213";
public static void main(String[] args) {
MyStack.push("COMP101");
MyStack.push(moduleName);
String s = MyStack.pop();
System.out.println("popped:" + s);
MyStack.push("COMP109");
s = MyStack.pop();
System.out.println("popped:" + s);
s = MyStack.pop();
System.out.println("popped:" + s);
// The stack should be empty now
s = MyStack.pop();
}
}
Testing your Stack
class testingMyStack {
private static String moduleName = “COMP213";
public static void main(String[] args) {
MyStack.push("COMP101");
MyStack.push(moduleName);
String s = MyStack.pop();
System.out.println("popped:" + s);
MyStack.push("COMP109");
s = MyStack.pop();
System.out.println("popped:" + s);
s = MyStack.pop();
System.out.println("popped:" + s);
// The stack should be empty now
s = MyStack.pop();
}
}
Testing your Stack
class testingMyStack {
private static String moduleName = “COMP213";
public static void main(String[] args) {
MyStack.push("COMP101");
MyStack.push(moduleName);
String s = MyStack.pop();
System.out.println("popped:" + s);
MyStack.push("COMP109");
s = MyStack.pop();
System.out.println("popped:" + s);
s = MyStack.pop();
System.out.println("popped:" + s);
// The stack should be empty now
s = MyStack.pop();
}
}
Testing your Stack
class testingMyStack {
private static String moduleName = “COMP213";
public static void main(String[] args) {
MyStack.push("COMP101");
MyStack.push(moduleName);
String s = MyStack.pop();
System.out.println("popped:" + s);
MyStack.push("COMP109");
s = MyStack.pop();
System.out.println("popped:" + s);
s = MyStack.pop();
System.out.println("popped:" + s);
// The stack should be empty now
s = MyStack.pop();
}
}
Testing your Stack
class testingMyStack {
private static String moduleName = “COMP213";
public static void main(String[] args) {
MyStack.push("COMP101");
MyStack.push(moduleName);
String s = MyStack.pop();
System.out.println("popped:" + s);
MyStack.push("COMP109");
s = MyStack.pop();
System.out.println("popped:" + s);
s = MyStack.pop();
System.out.println("popped:" + s);
// The stack should be empty now
s = MyStack.pop();
}
}
Testing your Stack
class testingMyStack {
private static String moduleName = “COMP213";
public static void main(String[] args) {
MyStack.push("COMP101");
MyStack.push(moduleName);
String s = MyStack.pop();
System.out.println("popped:" + s);
MyStack.push("COMP109");
s = MyStack.pop();
System.out.println("popped:" + s);
s = MyStack.pop();
System.out.println("popped:" + s);
// The stack should be empty now
s = MyStack.pop();
}
}
Issues with the Implementation
• I only have one stack
• I can only store Strings in my stack
• Let’s fix that!
Implementing a Stack of “things”
class Stack {
public static final int MaxSize = 100;
private int top = -1;
private Object[] store = new Object[MaxSize];
private static int numberOfPushes = 0;
...
}
Implementing a Stack of “things”
class Stack {
public static final int MaxSize = 100;
private int top = -1;
private Object[] store = new Object[MaxSize];
private static int numberOfPushes = 0;
...
}
Implementing a Stack of “things”
class Stack {
public static final int MaxSize = 100;
private int top = -1;
private Object[] store = new Object[MaxSize];
private static int numberOfPushes = 0;
...
}
This was “static” in the previous implementation, which made it a
field of the Class, not of the instance that we may declare
Implementing a Stack of “things”
class Stack {
public static final int MaxSize = 100;
private int top = -1;
private Object[] store = new Object[MaxSize];
private static int numberOfPushes = 0;
...
}
…same here
Implementing a Stack of “things”
class Stack {
public static final int MaxSize = 100;
private int top = -1;
private Object[] store = new Object[MaxSize];
private static int totalNumberOfPushes = 0;
...
}
Implementing a Stack of “things”
public void push (Object newItem) {
if (top < MaxSize -1) {
top++;
store[top]= newItem;
totalNumberOfPushes++;
} else {
System.err.println ("Sorry, stack full.");
}
}
Implementing a Stack of “things”
public void push (Object newItem) {
if (top < MaxSize -1) {
top++;
store[top]= newItem;
totalNumberOfPushes++;
} else {
System.err.println ("Sorry, stack full.");
}
}
Implementing a Stack of “things”
public void push (Object newItem) {
if (top < MaxSize -1) {
top++;
store[top]= newItem;
totalNumberOfPushes++;
} else {
System.err.println ("Sorry, stack full.");
}
}
Implementing a Stack of “things”
public void push (Object newItem) {
if (top < MaxSize -1) {
top++;
store[top]= newItem;
totalNumberOfPushes++;
} else {
System.err.println ("Sorry, stack full.");
}
}
Implementing a Stack of “things”
public void push (Object newItem) {
if (top < MaxSize -1) {
top++;
store[top]= newItem;
totalNumberOfPushes++;
} else {
System.err.println ("Sorry, stack full.");
}
}
Implementing a Stack of “things”
public void push (Object newItem) {
if (top < MaxSize -1) {
top++;
store[top]= newItem;
totalNumberOfPushes++;
} else {
System.err.println ("Sorry, stack full.");
}
}
Implementing a Stack of “things”
public void push (Object newItem) {
if (top < MaxSize -1) {
top++;
store[top]= newItem;
totalNumberOfPushes++;
} else {
System.err.println ("Sorry, stack full.");
}
}
Implementing a Stack of “things”
public Object pop () {
Object ans = "";
if (top >= 0) {
ans = store[top];
top--;
} else {
System.err.println ("Sorry, stack empty.");
}
return ans;
}
Implementing a Stack of “things”
public Object pop () {
Object ans = "";
if (top >= 0) {
ans = store[top];
top--;
} else {
System.err.println ("Sorry, stack empty.");
}
return ans;
}
Implementing a Stack of “things”
public Object pop () {
Object ans = "";
if (top >= 0) {
ans = store[top];
top--;
} else {
System.err.println ("Sorry, stack empty.");
}
return ans;
}
Implementing a Stack of “things”
public Object pop () {
Object ans = "";
if (top >= 0) {
ans = store[top];
top--;
} else {
System.err.println ("Sorry, stack empty.");
}
return ans;
}
Implementing a Stack of “things”
public Object pop () {
Object ans = "";
if (top >= 0) {
ans = store[top];
top--;
} else {
System.err.println ("Sorry, stack empty.");
}
return ans;
}
Implementing a Stack of “things”
public Object pop () {
Object ans = "";
if (top >= 0) {
ans = store[top];
top--;
} else {
System.err.println ("Sorry, stack empty.");
}
return ans;
}
Implementing a Stack of “things”
public Object pop () {
Object ans = "";
if (top >= 0) {
ans = store[top];
top--;
} else {
System.err.println ("Sorry, stack empty.");
}
return ans;
}
Implementing a Stack of “things”
public Object pop () {
Object ans = "";
if (top >= 0) {
ans = store[top];
top--;
} else {
System.err.println ("Sorry, stack empty.");
}
return ans;
}
Implementing a Stack of “things”
public int size() {
return top+1;
}
public static int getNumberOfPushes () {
return totalNumberOfPushes;
}
public void print() {
int counter;
for (counter=top; counter>=0; counter--) {
System.out.println(store[counter]);
}
}
Implementing a Stack of “things”
public int size() {
return top+1;
}
public static int getNumberOfPushes () {
return totalNumberOfPushes;
}
public void print() {
int counter;
for (counter=top; counter>=0; counter--) {
System.out.println(store[counter]);
}
}
Implementing a Stack of “things”
public int size() {
return top+1;
}
public void print() {
int counter;
for (counter=top; counter>=0; counter--) {
System.out.println(store[counter]);
}
}
public static int getNumberOfPushes () {
return totalNumberOfPushes;
}
Implementing a Stack of “things”
public int size() {
return top+1;
}
public void print() {
int counter;
for (counter=top; counter>=0; counter--) {
System.out.println(store[counter]);
}
}
public static int getNumberOfPushes () {
return totalNumberOfPushes;
}
Implementing a Stack of “things”
public int size() {
return top+1;
}
public static int getNumberOfPushes () {
return totalNumberOfPushes;
}
public void print() {
int counter;
for (counter=top; counter>=0; counter--) {
System.out.println(store[counter]);
}
}
Implementing a Stack of “things”
public int size() {
return top+1;
}
public static int getNumberOfPushes () {
return totalNumberOfPushes;
}
public void print() {
int counter;
for (counter=top; counter>=0; counter--) {
System.out.println(store[counter]);
}
}
Implementing a Stack of “things”
public int size() {
return top+1;
}
public static int getNumberOfPushes () {
return totalNumberOfPushes;
}
public void print() {
int counter;
for (counter=top; counter>=0; counter--) {
System.out.println(store[counter]);
}
}
Implementing a Stack of “things”
public int size() {
return top+1;
}
public static int getNumberOfPushes () {
return totalNumberOfPushes;
}
public void print() {
int counter;
for (counter=top; counter>=0; counter--) {
System.out.println(store[counter]);
}
}
Creating & Testing Stacks
class TestingStack {
private static String moduleName = “COMP213";
private static String grade = “A”;
public static void main(String[] args) {
Stack moduleStack = new Stack();
Stack gradeStack = new Stack();
moduleStack.push(moduleName);
moduleStack.push(“COMP101”);
gradeStack.push(“B-”);
String s = (String) moduleStack.pop();
System.out.println("popped:" + s);
moduleStack.push("COMP109");
gradeStack.push(grade);
s = (String) gradeStack.pop();
System.out.println("popped:" + s);
moduleStack.push(s);
}
}
class TestingStack {
private static String moduleName = “COMP213";
private static String grade = “A”;
public static void main(String[] args) {
Stack moduleStack = new Stack();
Stack gradeStack = new Stack();
moduleStack.push(moduleName);
moduleStack.push(“COMP101”);
gradeStack.push(“B-”);
String s = (String) moduleStack.pop();
System.out.println("popped:" + s);
moduleStack.push("COMP109");
gradeStack.push(grade);
s = (String) gradeStack.pop();
System.out.println("popped:" + s);
moduleStack.push(s);
}
}
Creating & Testing Stacks
Creating & Testing Stacks
class TestingStack {
private static String moduleName = “COMP213";
private static String grade = “A”;
public static void main(String[] args) {
Stack moduleStack = new Stack();
Stack gradeStack = new Stack();
moduleStack.push(moduleName);
moduleStack.push(“COMP101”);
gradeStack.push(“B-”);
String s = (String) moduleStack.pop();
System.out.println("popped:" + s);
moduleStack.push("COMP109");
gradeStack.push(grade);
s = (String) gradeStack.pop();
System.out.println("popped:" + s);
moduleStack.push(s);
}
}
Creating & Testing Stacks
class TestingStack {
private static String moduleName = “COMP213";
private static String grade = “A”;
public static void main(String[] args) {
Stack moduleStack = new Stack();
Stack gradeStack = new Stack();
moduleStack.push(moduleName);
moduleStack.push(“COMP101”);
gradeStack.push(“B-”);
String s = (String) moduleStack.pop();
System.out.println("popped:" + s);
moduleStack.push("COMP109");
gradeStack.push(grade);
s = (String) gradeStack.pop();
System.out.println("popped:" + s);
moduleStack.push(s);
}
}
Creating & Testing Stacks
class TestingStack {
private static String moduleName = “COMP213";
private static String grade = “A”;
public static void main(String[] args) {
Stack moduleStack = new Stack();
Stack gradeStack = new Stack();
moduleStack.push(moduleName);
moduleStack.push(“COMP101”);
gradeStack.push(“B-”);
String s = (String) moduleStack.pop();
System.out.println("popped:" + s);
moduleStack.push("COMP109");
gradeStack.push(grade);
s = (String) gradeStack.pop();
System.out.println("popped:" + s);
moduleStack.push(s);
}
}
Creating & Testing Stacks
class TestingStack {
private static String moduleName = “COMP213";
private static String grade = “A”;
public static void main(String[] args) {
Stack moduleStack = new Stack();
Stack gradeStack = new Stack();
moduleStack.push(moduleName);
moduleStack.push(“COMP101”);
gradeStack.push(“B-”);
String s = (String) moduleStack.pop();
System.out.println("popped:" + s);
moduleStack.push("COMP109");
gradeStack.push(grade);
s = (String) gradeStack.pop();
System.out.println("popped:" + s);
moduleStack.push(s);
}
}
Creating & Testing Stacks
class TestingStack {
private static String moduleName = “COMP213";
private static String grade = “A”;
public static void main(String[] args) {
Stack moduleStack = new Stack();
Stack gradeStack = new Stack();
moduleStack.push(moduleName);
moduleStack.push(“COMP101”);
gradeStack.push(“B-”);
String s = (String) moduleStack.pop();
System.out.println("popped:" + s);
moduleStack.push("COMP109");
gradeStack.push(grade);
s = (String) gradeStack.pop();
System.out.println("popped:" + s);
moduleStack.push(s);
}
}
Creating & Testing Stacks
class TestingStack {
private static String moduleName = “COMP213";
private static String grade = “A”;
public static void main(String[] args) {
Stack moduleStack = new Stack();
Stack gradeStack = new Stack();
moduleStack.push(moduleName);
moduleStack.push(“COMP101”);
gradeStack.push(“B-”);
String s = (String) moduleStack.pop();
System.out.println("popped:" + s);
moduleStack.push("COMP109");
gradeStack.push(grade);
s = (String) gradeStack.pop();
System.out.println("popped:" + s);
moduleStack.push(s);
}
}
Creating & Testing Stacks
class TestingStack {
private static String moduleName = “COMP213";
private static String grade = “A”;
public static void main(String[] args) {
Stack moduleStack = new Stack();
Stack gradeStack = new Stack();
moduleStack.push(moduleName);
moduleStack.push(“COMP101”);
gradeStack.push(“B-”);
String s = (String) moduleStack.pop();
System.out.println("popped:" + s);
moduleStack.push("COMP109");
gradeStack.push(grade);
s = (String) gradeStack.pop();
System.out.println("popped:" + s);
moduleStack.push(s);
}
}
Creating & Testing Stacks
class TestingStack {
private static String moduleName = “COMP213";
private static String grade = “A”;
public static void main(String[] args) {
Stack moduleStack = new Stack();
Stack gradeStack = new Stack();
moduleStack.push(moduleName);
moduleStack.push(“COMP101”);
gradeStack.push(“B-”);
String s = (String) moduleStack.pop();
System.out.println("popped:" + s);
moduleStack.push("COMP109");
gradeStack.push(grade);
s = (String) gradeStack.pop();
System.out.println("popped:" + s);
moduleStack.push(s);
}
}
Creating & Testing Stacks
class TestingStack {
private static String moduleName = “COMP213";
private static String grade = “A”;
public static void main(String[] args) {
Stack moduleStack = new Stack();
Stack gradeStack = new Stack();
moduleStack.push(moduleName);
moduleStack.push(“COMP101”);
gradeStack.push(“B-”);
String s = (String) moduleStack.pop();
System.out.println("popped:" + s);
moduleStack.push("COMP109");
gradeStack.push(grade);
s = (String) gradeStack.pop();
System.out.println("popped:" + s);
moduleStack.push(s);
}
}
Creating & Testing Stacks
class TestingStack {
private static String moduleName = “COMP213";
private static String grade = “A”;
public static void main(String[] args) {
Stack moduleStack = new Stack();
Stack gradeStack = new Stack();
moduleStack.push(moduleName);
moduleStack.push(“COMP101”);
gradeStack.push(“B-”);
String s = (String) moduleStack.pop();
System.out.println("popped:" + s);
moduleStack.push("COMP109");
gradeStack.push(grade);
s = (String) gradeStack.pop();
System.out.println("popped:" + s);
moduleStack.push(s);
}
}
Creating & Testing Stacks
class TestingStack {
private static String moduleName = “COMP213";
private static String grade = “A”;
public static void main(String[] args) {
Stack moduleStack = new Stack();
Stack gradeStack = new Stack();
moduleStack.push(moduleName);
moduleStack.push(“COMP101”);
gradeStack.push(“B-”);
String s = (String) moduleStack.pop();
System.out.println("popped:" + s);
moduleStack.push("COMP109");
gradeStack.push(grade);
s = (String) gradeStack.pop();
System.out.println("popped:" + s);
moduleStack.push(s);
}
}
Creating & Testing Stacks
class TestingStack {
private static String moduleName = “COMP213";
private static String grade = “A”;
public static void main(String[] args) {
Stack moduleStack = new Stack();
Stack gradeStack = new Stack();
moduleStack.push(moduleName);
moduleStack.push(“COMP101”);
gradeStack.push(“B-”);
String s = (String) moduleStack.pop();
System.out.println("popped:" + s);
moduleStack.push("COMP109");
gradeStack.push(grade);
s = (String) gradeStack.pop();
System.out.println("popped:" + s);
moduleStack.push(s);
}
}
Creating & Testing Stacks
// Print the stacks:
System.out.println ("Here’s the module stack:");
moduleStack.print();
System.out.println ("Here’s the grade stack:");
gradeStack.print();
// Print out how many valid pushes so far.
int numOfPushes = Stack.getNumberOfPushes();
System.out.println ("Number of valid pushes: “ + numOfPushes);
Creating & Testing Stacks
// Print the stacks:
System.out.println ("Here’s the module stack:");
moduleStack.print();
System.out.println ("Here’s the grade stack:");
gradeStack.print();
// Print out how many valid pushes so far.
int numOfPushes = Stack.getNumberOfPushes();
System.out.println ("Number of valid pushes: “ + numOfPushes);
Creating & Testing Stacks
// Print the stacks:
System.out.println ("Here’s the module stack:");
moduleStack.print();
System.out.println ("Here’s the grade stack:");
gradeStack.print();
// Print out how many valid pushes so far.
int numOfPushes = Stack.getNumberOfPushes();
System.out.println ("Number of valid pushes: “ + numOfPushes);
Creating & Testing Stacks
// Print the stacks:
System.out.println ("Here’s the module stack:");
moduleStack.print();
System.out.println ("Here’s the grade stack:");
gradeStack.print();
// Print out how many valid pushes so far.
int numOfPushes = Stack.getNumberOfPushes();
System.out.println ("Number of valid pushes: “ + numOfPushes);
Creating & Testing Stacks
// Print the stacks:
System.out.println ("Here’s the module stack:");
moduleStack.print();
System.out.println ("Here’s the grade stack:");
gradeStack.print();
// Print out how many valid pushes so far.
int numOfPushes = Stack.getNumberOfPushes();
System.out.println ("Number of valid pushes: “ + numOfPushes);
Creating & Testing Stacks
// Print the stacks:
System.out.println ("Here’s the module stack:");
moduleStack.print();
System.out.println ("Here’s the grade stack:");
gradeStack.print();
// Print out how many valid pushes so far.
int numOfPushes = Stack.getNumberOfPushes();
System.out.println ("Number of valid pushes: “ + numOfPushes);
Creating & Testing Stacks
// Print the stacks:
System.out.println ("Here’s the module stack:");
moduleStack.print();
System.out.println ("Here’s the grade stack:");
gradeStack.print();
// Print out how many valid pushes so far.
int numOfPushes = Stack.getNumberOfPushes();
System.out.println ("Number of valid pushes: “ + numOfPushes);
Creating & Testing Stacks
// Print the stacks:
System.out.println ("Here’s the module stack:");
moduleStack.print();
System.out.println ("Here’s the grade stack:");
gradeStack.print();
// Print out how many valid pushes so far.
int numOfPushes = Stack.getNumberOfPushes();
System.out.println ("Number of valid pushes: “ + numOfPushes);
static vs. Instance Variables
• A variable that is declared static is unique!
• static variables are often called class variables, because there is only one created per class.
• You do not get a new one with each instance; there is only one.
• A non-static variable is called instance variable.
• When you instantiate from a class, you get a new copy/instance of each variable.
• For instance variables, there is one per instance of the class.
static vs. Instance Variables
• A variable that is declared static is unique!
• static variables are often called class variables, because there is only one created per class.
• You do not get a new one with each instance; there is only one.
• A non-static variable is called instance variable.
• When you instantiate from a class, you get a new copy/instance of each variable.
• For instance variables, there is one per instance of the class.
static vs. Instance Methods
• A method that is declared static cannot directly access any instance variables of the class or call any non-static methods
• staticmethods may access static variables directly.
• staticmethods may create new instances of the class and manipulate the instance variables & methods of those instances.
• Non-static methods are called instance methods, and may access both static and instance variables.
static vs. Instance Methods
• A method that is declared static cannot directly access any instance variables of the class or call any non-static methods
• staticmethods may access static variables directly.
• staticmethods may create new instances of the class and manipulate the instance variables & methods of those instances.
• Non-static methods are called instance methods, and may access both static and instance variables.
Homework
• Implement other common operations of the Stack ADT:
• peek – return the top element without removing it.
• isEmpty – return true if no elements are in it, otherwise return false.
• Implement a List with the following operations:
• add – add a value to the start of the list
• getValue – return the value at the start of the list
• get – return the “tail” of the list
Summary
• Implementing Abstract Data Types
• static vs. Instance fields & methods
• Next: More about modifiers – final and static.