advanced object-oriented programming lecture 4cgi.csc.liv.ac.uk › ~akridel › comp213-fall2016...
TRANSCRIPT
![Page 1: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/1.jpg)
COMP 213Advanced Object-oriented Programming
Lecture 4
Implementing ADTs
![Page 2: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/2.jpg)
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.
![Page 3: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/3.jpg)
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.
![Page 4: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/4.jpg)
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.
![Page 5: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/5.jpg)
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.
![Page 6: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/6.jpg)
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.
![Page 7: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/7.jpg)
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.
![Page 8: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/8.jpg)
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];
...
}
![Page 9: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/9.jpg)
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];
...
}
![Page 10: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/10.jpg)
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];
...
}
![Page 11: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/11.jpg)
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];
...
}
![Page 12: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/12.jpg)
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.”);
}
}
![Page 13: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/13.jpg)
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;
}
![Page 14: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/14.jpg)
Implementing a Stack of Strings
public static int size() {
return top + 1;
}
![Page 15: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/15.jpg)
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();
}
}
![Page 16: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/16.jpg)
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
![Page 17: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/17.jpg)
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();
}
}
![Page 18: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/18.jpg)
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();
}
}
![Page 19: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/19.jpg)
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();
}
}
![Page 20: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/20.jpg)
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();
}
}
![Page 21: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/21.jpg)
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();
}
}
![Page 22: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/22.jpg)
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();
}
}
![Page 23: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/23.jpg)
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();
}
}
![Page 24: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/24.jpg)
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();
}
}
![Page 25: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/25.jpg)
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();
}
}
![Page 26: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/26.jpg)
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();
}
}
![Page 27: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/27.jpg)
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();
}
}
![Page 28: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/28.jpg)
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();
}
}
![Page 29: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/29.jpg)
Issues with the Implementation
• I only have one stack
• I can only store Strings in my stack
• Let’s fix that!
![Page 30: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/30.jpg)
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;
...
}
![Page 31: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/31.jpg)
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;
...
}
![Page 32: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/32.jpg)
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
![Page 33: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/33.jpg)
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
![Page 34: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/34.jpg)
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;
...
}
![Page 35: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/35.jpg)
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.");
}
}
![Page 36: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/36.jpg)
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.");
}
}
![Page 37: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/37.jpg)
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.");
}
}
![Page 38: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/38.jpg)
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.");
}
}
![Page 39: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/39.jpg)
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.");
}
}
![Page 40: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/40.jpg)
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.");
}
}
![Page 41: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/41.jpg)
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.");
}
}
![Page 42: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/42.jpg)
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;
}
![Page 43: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/43.jpg)
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;
}
![Page 44: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/44.jpg)
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;
}
![Page 45: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/45.jpg)
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;
}
![Page 46: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/46.jpg)
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;
}
![Page 47: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/47.jpg)
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;
}
![Page 48: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/48.jpg)
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;
}
![Page 49: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/49.jpg)
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;
}
![Page 50: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/50.jpg)
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]);
}
}
![Page 51: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/51.jpg)
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]);
}
}
![Page 52: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/52.jpg)
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;
}
![Page 53: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/53.jpg)
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;
}
![Page 54: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/54.jpg)
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]);
}
}
![Page 55: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/55.jpg)
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]);
}
}
![Page 56: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/56.jpg)
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]);
}
}
![Page 57: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/57.jpg)
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]);
}
}
![Page 58: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/58.jpg)
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);
}
}
![Page 59: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/59.jpg)
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
![Page 60: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/60.jpg)
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);
}
}
![Page 61: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/61.jpg)
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);
}
}
![Page 62: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/62.jpg)
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);
}
}
![Page 63: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/63.jpg)
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);
}
}
![Page 64: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/64.jpg)
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);
}
}
![Page 65: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/65.jpg)
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);
}
}
![Page 66: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/66.jpg)
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);
}
}
![Page 67: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/67.jpg)
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);
}
}
![Page 68: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/68.jpg)
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);
}
}
![Page 69: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/69.jpg)
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);
}
}
![Page 70: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/70.jpg)
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);
}
}
![Page 71: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/71.jpg)
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);
}
}
![Page 72: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/72.jpg)
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);
![Page 73: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/73.jpg)
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);
![Page 74: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/74.jpg)
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);
![Page 75: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/75.jpg)
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);
![Page 76: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/76.jpg)
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);
![Page 77: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/77.jpg)
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);
![Page 78: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/78.jpg)
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);
![Page 79: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/79.jpg)
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);
![Page 80: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/80.jpg)
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.
![Page 81: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/81.jpg)
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.
![Page 82: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/82.jpg)
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.
![Page 83: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/83.jpg)
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.
![Page 84: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/84.jpg)
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
![Page 85: Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java](https://reader034.vdocuments.us/reader034/viewer/2022042406/5f20f2e84c644a7b175bf40c/html5/thumbnails/85.jpg)
Summary
• Implementing Abstract Data Types
• static vs. Instance fields & methods
• Next: More about modifiers – final and static.