85
COMP 213 Advanced Object-oriented Programming Lecture 4 Implementing ADTs

Advanced Object-oriented Programming Lecture 4cgi.csc.liv.ac.uk › ~akridel › COMP213-Fall2016 › Lecture...Advanced Object-oriented Programming Lecture 4 Implementing ADTs Java

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Summary

• Implementing Abstract Data Types

• static vs. Instance fields & methods

• Next: More about modifiers – final and static.