Starting from:
$30

$24

First Part Stack

Learning objectives
    • Recognize the situation where a stack would be necessary for the conception of an algorithm
    • Describe the implementation of a stack using an array
    • Implement a stack using a dynamic array
    • Implement a predefined interface type
    • Apply the concept of generic types for the implementation of classes and interfaces
Stack
Introduction
Stacks are part of the common data structure types used by programmers. There are however many ways to implement them. To do so, you must understand their behaviors. First, stacks follow the principle of last-in first-out (LIFO). In other words, the elements are stacked one on top of the other, and you can only access the element on top of the stack. To access an element below the top element, you can only access this element by removing every element on top of it, i.e. entered after.
You are constantly surrounded by stacks in the real world. Determine if these are indeed an example of a stack (true) or not (false).
Question 1.1 :
Tennis balls in their container
Answer
Question 1.2 :
The waiting list for students to be admitted in engineering.
Answer
Question 1.3 :
A stack of dishes.
Answer
Question 1.4 :
Evaluation of postfix expressions
Answer
Question 1.5 :
The chips in a Pringles can
Answer
Question 1.6 :
A toll booth
Answer
Question 1.7 :
The back button in your web browser
Answer
Application
This part of the lab will focus on two of the three implementations of the interface Stack. You will be evaluated on these two implementations in the midterm evaluation.
1.1 Modify the interface Stack: add a method clear()
Modify the interface Stack bellow to add a method abstract public void clear().
public interface Stack<E> {
    boolean isEmpty();
    E peek();
    E pop();
    void push(E element);
}
File:
    • Stack.java
1.2 Implement the method clear() of the class ArrayStack
The class ArrayStack uses an array of fixed size and implements the interface Stack. Since the interface Stack has been modified to have the method clear(), the implementation of the class ArrayStack is faulty. (Try to compile it now without changing anything, what error do you see?).
Since the class ArrayStack implements the interface Stack, it needs to have an implementation for all methods of the interface. Therefore, you will need to write a method void clear(). This method will remove all the elements from this stack (ArrayStack). The stack will be empty after this call. Use the class L6Q1 to test the implementation of the method clear().
Files:
    • ArrayStack.java
    • L6Q1.java
1.3 Create a new class DynamicArrayStack
Modify the class ArrayStack using the technique known as dynamic array. This new class DynamicArrayStack has a constant DEFAULT_INC, with the value 25. The constructor takes an argument capacity which is the initial size of the array. However, the minimum size of the array is never less that DEFAULT_INC (25). It has a getter, getCapacity(), that returns an integer, which is the length of the array (physical size of the stack). When the array becomes full, a new array is created with DEFAULT_INC more cells than the previous array, and filled with the elements from the previous array. Similarly, when the stack has DEFAULT_INC elements less than the length of the array (physical size of the stack), it needs to be automatically reduced in size. Make the necessary changes, particularly in pop, push and clear, and in the constructor (recall, the minimal size of an array is DEFAULT_INC).
File:
    • DynamicArrayStack.java
 

More products