Week-03 | "Print" Stack
Task List
- Update your toString method in your MyStack class by creating astring representation thatdisplays the contents of the stack from left (bottom) to right (top) with the aid of your ArrayList class.
- Complete this code modification in your "my-api" repository and push it to GitHub remote repository.
Location in "my-api" repopackage.class : utils.MyStack
JUnit Test location in "my-api" repo
package.class : tests.junit.MyStackJUnitTest
Example console print using "toString" method:
Read from left to right
push order: "Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"
bottom[Red, Orange, Yellow, Green, Blue, Indigo, Violet]top
Example console print for LIFO operation using "pop" method :
Read from left to right (NOTE: You can test this out using the toString JUNIT Test)
bottom[Red, Orange, Yellow, Green, Blue, Indigo]top
bottom[Red, Orange, Yellow, Green, Blue]top
bottom[Red, Orange, Yellow, Green]top
Week-03 | Array Lists
Objective:The purpose of this exercise is to create an Array List data structure that mimics the behavior of the Java Standard Library Version (Java API). The outcomes/results of using the library features should be identical with your own version (My API). However, the underlying implementation should follow with the descriptions listed below.
Instructions: Create the following Array List Data Structure in your"utils" packageand use "for loops" for your repetitive tasks.
Where to find starter code in my-api
package.class :utils.ArrayList
package.class :tests.console.week03.ArrayListTest
Where to find your JUNIT test in my-api
package.class :tests.junit.ArrayListJUnitTest
Task Check List
- ONLY "for" loops should be used within the data structure class. There is anautomatic 30% deduction,if other loops are used.
- The names of identifiers MUST match the names listed in the description below. Deductions otherwise.
- Complete coding Assignment in your "my-api"GitHub Repository. You will not be graded otherwise and will receive a 0, if not uploaded there.
- Run JUNIT TEST and take a SNAPSHOT of results. Upload PDF of snapshot of yourJUnitTest results to Canvas.
Description
This Array List Data Structure operates like adynamic array, which can grow or reduce its size.
Think through the class construction so that it meets the following specifications:
data fields: The fields to declare have private accessibility.
- size: stores the number of occupied locations in internal array, and is of type int.
- data: is a reference variable for the internal array and is of typeE[ ].
constant: Use to describe defaults.
- DEFAULT_CAPACITY is aconstantthat holds a default capacity of ten (10) for the underlying array, is of type int , public accessibility and modified tostatic.
constructors: The overloaded constructors will initialize the data fields size and data.
- The default constructor calls the second constructor, generating an internal array with the specified DEFAULT_CAPACITY.
public ArrayList()
- The second constructor generates an internal array with the specified initial capacity. If the capacity is less than zero, this constructor should throw anIllegalArgumentException.
@SuppressWarnings("unchecked")
public ArrayList(int capacity)
Note: Java does not allow you to construct a generic array i.e
data = new E[capacity]; // illegal
so, you must use
data = (E[])new Object[capacity]; // valid
generic type: Allows the uses of different data types.
This is a limitation of generic types. Youmustcast an array of type Object to the generic to make it work. This new cast version will work, but it will produce a warning about using anunsafe or unchecked type.
There is no real way to get around this, because Java won’t let you construct a generic array. Therefore, you can ignore this warning and can include theannotation@SuppressWarnings("unchecked")to indicate that you don’t want to generate the warning for this method.
methods: manages the behavior of the internal array.
Together, the methods below give the illusion of a dynamic array (an array that grows or shrinks). Implement these methods with in your generic Array List class.
Method
|
Description
|
Header
|
add(item) |
appends the item specified to the end of the list and updates the number of elements one at a time. This method returns true, if the data was added successfully. |
public boolean add(E item)
|
add(index, item) |
inserts the item specified at the given index in the list. Shifts subsequent elements to the right and updates the number of elements in list one at a time. |
public void add(int index, E item)
|
checkIndex(index) |
checks if the given index is valid. Throws anIndexOutOfBoundsException, if invalid.Validation means that you cannot access indexes where elements have not been placed.This is a private helper method.Note: This method should be used in any method that requires a precondition for access to valid index.
|
private void checkIndex(int index)
|
clear()
|
clears list of all elements for garbage collection, returns size back to zero.
|
public void clear()
|
contains(item) |
searches for a specific value within the internal array and returns true, if the value is in the list. |
public boolean contains(E item)
|
ensureCapacity( targetCapacity) |
doubles the capacity of the underlying array, or if the targetCapacity is greater that double the capacity increases the array to the targetCapacity to ensure that it can hold the number of elements specified by the targetCapacity value. This will most likely have a cast to a generic type. |
@SuppressWarnings("unchecked")
public void ensureCapacity(int targetCapacity)
|
get(index) |
returns the item at the specified position in the list. This method first checks if the index requested is valid. |
public E get(int index)
|
indexOf(item) |
searches for a specific item within the linked structure and returns the first occurrence (i.e. index location) in the array if found, otherwise returns -1 to indicate that the item was NOT FOUND. |
public int indexOf(E item)
|
isEmpty() |
returns true, if the list is empty, i.e., the list contains no elements.
|
public boolean isEmpty()
|
remove(index) |
removes the item at the given index in the list. Shifts subsequent elements to the left and returns the item removed. This method first checks if the index requested is valid and reduces the number of elements by one. |
public E remove(int index)
|
remove(item) |
removes the first occurrence of the item specified from the list, if present. Shifts subsequent elements to the left and returns true, if the item is removed. Reduces the number of elements by one. |
public boolean remove(E item)
|
set(index, item) |
replaces the item at the specified position with the one specified. This method validates the index before replacing the item. |
public E set(int index, E item)
|
shiftLeft(index) |
helper method that shifts elements of internal array left to index location. Does not update the size of the list.
|
private void shiftLeft(int index)
|
shiftRight(index) |
helper method that shifts elements of internal array right from index location. Does not update the size of the list.
|
private void shiftRight(int index)
|
size()
|
returns the number of elements in the list. This is the size of the occupied locations of the array.
|
public int size()
|
toString() |
displays the contents of the list. |
public String toString()
|
JUNIT Jupiter Tests
The instructorwill provide a JUnit 5 Testfor you to test your Array List data structure.
Run it and take a SNAPSHOT of results. Upload as a PDF to Canvas.
Week-03 | "Print" Stack (0.5 hrs) " style="float: right;">