Title: ITI 1221. Introduction to Computing II Lab-5
1ITI 1221. Introduction to Computing IILab-5
- Dewan Tanvir Ahmed
- University of Ottawa
2Part I Interface
- Objectives
- Understanding and implementing an interface
- Creating an interface
- Introduction and application of generic data
structures
3Interface - Comparable
- Implements the interface
- java.lang.Comparable in Combination class
- Make all the necessary changes to the class
Combination
public interface Comparable // Compares
this object with the specified object for order.
// Returns a negative integer, zero, or a p
ositive integer // as this object is less t
han, equal to, or greater than the
// specified object. public int com
pareTo (Object o)
4Combination (cont..)
public class Combination implements Comparable
private int first private int second
private int third public Combination(int
first, int second, int third)
this.first first this.second
second this.third third
public String toString() return
first "" second "" third
5Combination (cont..)
public int compareTo(Object obj) //
pre-condition (obj ! null) (obj instanceof
Combination) Combination other
(Combination) obj int result
if (first lt other.first) result -1
else if (first gt other.first) result 1
else if (second lt other.second)
result -1 else if (second gt
other.second) result 1 else if
(third lt other.third) result -1
else if (third gt other.third) result 1
else result 0 return result
6Interface - OrderedStructure
- Create an interface, called OrderedStructure,
that declares the following methods. - int size()
- boolean add(Comparable obj)
- Object get(int pos)
- void remove(int pos)
Is it essential?
interface OrderedStructure public abstract
int size() public abstract boolean
add(Comparable obj) public abstract Object
get(int pos) public abstract void
remove(int pos)
7Class OrderedArray
- Provide an implementation for each method
declared in the interface OrderedStructure - Instance variables
- An OrderedArray makes use of an array to store
elements. - It must also keep track of the number of elements
that are currently stored in the array.
8Class OrderedArray
- public class OrderedArray implements
OrderedStructure - private Comparable elems
- private int numElems
- public OrderedArray(int size)
- elems new Comparablesize
- numElems 0
-
- public int size() return numElems
- public Object get(int pos)
- // pre-condition 0 lt pos lt numElements
- return elemspos
-
- public void remove(int pos)
- // pre-condition 0 lt pos lt numElements
- for (int ipos ilt(numElems-1) i)
9Class OrderedArray (cont..)
public boolean add(Comparable elem) if
( elem null numElems elems.length )
return false boolean inserted
false for ( int i0 iltnumElems !
inserted i) if (
elemsi.compareTo( elem ) gt 0 )
for ( int jnumElems jgti j-- )
elems j elems j-1
elems i elem
inserted true numElems
if ( ! inserted )
elems numElems elem return
true
- add() method adds an element in increasing order
- returns true if the element can be successfully
added - false if the data structure is full or the
parameter argument is null
10Lets Run a Program
- What do you need?
- Comparable.java
- OrderedStructure.java
- OrderedArray.java
- Test.java
11Part 2 Stack-based Algorithm
- Understanding of the stack-based algorithms
- Program
- Validating expressions containing parentheses
- round,
- curly
- square parentheses
12First Algorithm
public class Balanced public static boolean
algo1( String s ) int curly 0
int square 0 int round 0
for ( int i0 ilts.length() i )
char c s.charAt( i ) switch ( c )
case '' curly break
case '' curly-- break case
'' square break case ''
square-- break case '(' round
break case ')' round--
return curly 0 square 0
round 0 public static void
main( String args ) for ( int i0
iltargs.length i )
System.out.println( "algo1( \"" args i "\"
) -gt " algo1( args i ) )
Whats happen if you miss to write break?
13Exploit Stack
- Observation
- A well-balanced expression is an expression such
that for each type of parentheses (round, curly
and square), the number of opening and closing
parentheses are same. - Parentheses rules.
- Implement a stack-based algorithm to validate an
expression.
14Second Algorithm
- public interface Stack
- // Return true if this Stack is empty and
false otherwise. - public abstract boolean isEmpty ()
- // Return The top element of this stack
without removing it. - public abstract Object peek ()
- // Removes and returns the element at the top
of this stack. - public abstract Object pop ()
- // Puts an element onto the top of this
stack. - public abstract void push (Object element)
-
15Second Algorithm (cont..)
- public class ArrayStack implements Stack
- private Object elems // used to store the
elements of this ArrayStack - private int top // designates the
first free cell - public ArrayStack( int capacity )
- elems new Object capacity
- top 0
-
- public boolean isEmpty()
- return top 0
-
- public Object peek()
- // pre-conditions ! isEmpty()
- return elems top-1
-
- . . .
16Second Algorithm (cont..)
- public class ArrayStack implements Stack
-
- public Object pop()
- // pre-conditions ! isEmpty()
- // decrements top, then access the value
- Object saved elems --top
- elems top null // scrub the memory!
- return saved
-
- public void push( Object element )
- // pre-condition the stack is not full
- // stores the element at position top,
then increments top - elems top element
-
-
17Second Algorithm (cont..)
- public static boolean algo2( String s )
- Stack brackets new ArrayStack( 100 )
- for ( int i0 ilts.length() i )
- char current s.charAt( i )
- if ( current '(' current ''
current '' ) - brackets.push( new Character(
current ) ) -
- else if ( current ')' current
'' current '' ) - if (brackets.isEmpty())
- return false
- char top ( (Character)
brackets.pop() ).charValue() - if ( (current ')' top !
'(') - (current '' top !
'') - (current '' top !
'') ) - return false
-
-
- return brackets.isEmpty ()
18Clarification - Assignment 2
- Modifying the interface Stack adding a method
clear() - public void clear()
- Purpose
- Clearing Stack, and it will be empty after this
call
public interface Stack public abstract boo
lean isEmpty() public abstract Object peek(
) public abstract Object pop()
public abstract void push( Object element )
19Clarification - Assignment 2 (cont..)
Uses fixed sized array and implements the
interface Stack
Class ArrayStack Implementing the method
clear() in the
Not Allowed??
20Clarification - Assignment 2 (cont..)
- Calculator Using a stack
- Implement following operations in Calculator
class - Add
- sub,
- Exch
- Dup
- pstack
Example 3 pstack dup pstack add pstack
-top- INTEGER 3 -bottom-
-top- INTEGER 3 INTEGER 3 -bottom-
-top- INTEGER 6 -bottom-
3
6
3
Add
21Clarification - Assignment 2 (cont..)
- Class DynamicArrayStack
- Modify the implementation of the method push() so
that the size of the array is increased when the
array is full. - Allocate a new array which is bigger than the
existing one - Copy all the elements from the old array into the
new one - Set the instance variable to point at the new
array - Proceed adding the element
- The initial size of the stack is determined by
the parameter of the constructor of the - class DynamicArrayStack( int capacity )
- Every time the array will be full, the method
push() will create an array that has 1.5 times
its current capacity
22Clarification - Assignment 2 (cont..)
- Creating a LinkedStack
- Variable size (does not have a fixed capacity)
- Always uses an amount of memory that is
proportional to the number of elements stored in
the stack (does not waste any memory) - Efficient (elements are never copied from one
array to another) - LinkedStack must implement the instance method
- booleean equals( Object o )
- Returns true other if designates a Stack (any
implementation of the interface Stack) that
contains the same number of elements as this
stack, and each element of the other stack must
be equals to the corresponding element in this
stack - Returns false otherwise
- Both stacks must not be changed by the method
equals
23The End