Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Client class with a main method that tests the data structures as follows: For the ArrayStack, LinkedStack, ArrayQueue, LinkedQueue, and ArrayList: Perform a

Write a Client class with a main method that tests the data structures as follows:

  • For the ArrayStack, LinkedStack, ArrayQueue, LinkedQueue, and ArrayList:
    • Perform a timing test for each of these data structures.
    • Each timing test should measure in nanoseconds how long it takes to add and remove N Integers from the structure.
    • N should vary from 10 to 1,000,000,000 increasing N by a factor of 10 for each test.
    • Depending on your system you may run out of memory before you reach the maximum value of N.
      • If you run out of memory, and your program crashes just decrease the maximum value of N by a factor of 10 and try a new run.
      • You should see that your program throws an OutOfMemoryError
      • Generally, you should not try to catch an OutOfMemoryError because your memory space might be corrupted and there is no guarantee that you can recover from the error.
    • Test results must be displayed in a nicely formatted ASCII table similar to the examples provided.
    • In the ASCII table:
      • Values in each cell are padded by 2 blank spaces
      • Each column is just wide enough to display the widest entry in that column including the cell padding. Your program must dynamically adjust the width of each column based on the values that it needs to print.
      • Numeric values must be printed using the comma thousand separator, i.e.
        • you must print a large number like 12,345
        • and not 12345
      • It is strongly suggested that you create a method that generates and prints the ASCII table. You could pass this method a 2-dimensional array of values that are to be printed.
    • You should have two final runs in your Word document.
    • For the first run, set the max value of N to 1,000,000 so that the times are fairly small.
    • For the second run, set the max value of N to 1,000,000,000
    • If you run out of memory reduce the max value of N by a factor of 10 and try a new run.
    • For this assignment your ASCII tables do NOT need to have column labels.

ArrayList Code:

public class ArrayList implements List { // instance variables public static final int CAPACITY = 16; // default array capacity private E[] data; // generic array used for storage private int size = 0; // current number of elements // constructors public ArrayList() { this(CAPACITY); } // constructs list with default capacity public ArrayList(int capacity) { // constructs list with given capacity data = (E[]) new Object[capacity]; // safe cast; compiler may give warning } // public methods /** Returns the number of elements in the array list. */ public int size() { return size; } /** Returns whether the array list is empty. */ public boolean isEmpty() { return size == 0; } /** Returns (but does not remove) the element at index i. */ public E get(int i) throws IndexOutOfBoundsException { checkIndex(i, size); return data[i]; } /** Replaces the element at index i with e, and returns the replaced element. */ public E set(int i, E e) throws IndexOutOfBoundsException { checkIndex(i, size); E temp = data[i]; data[i] = e; return temp; } /** Inserts element e to be at index i, shifting all subsequent elements later. */ public void add(int i, E e) throws IndexOutOfBoundsException, IllegalStateException { checkIndex(i, size + 1); if (size == data.length) // not enough capacity throw new IllegalStateException("Array is full"); for (int k = size-1; k >= i; k--) // start by shifting rightmost data[k+1] = data[k]; data[i] = e; // ready to place the new element size++; } /** Removes/returns the element at index i, shifting subsequent elements earlier. */ public E remove(int i) throws IndexOutOfBoundsException { checkIndex(i, size); E temp = data[i]; for (int k=i; k < size-1; k++) // shift elements to fill hole data[k] = data[k + 1]; data[size-1] = null; // help garbage collection size--; return temp; } // utility method /** Checks whether the given index is in the range [0, n-1]. */ protected void checkIndex(int i, int n) throws IndexOutOfBoundsException { if (i < 0 || i>= n) throw new IndexOutOfBoundsException("Illegal index: " + i); } /** Resizes internal array to have given capacity >= size. */ protected void resize(int capacity) { E[] temp = (E[]) new Object[capacity]; // safe cast; compiler may give warning for (int k=0; k < size; k++) temp[k] = data[k]; data = temp; // start using the new array } }

ArrayStack Code:

public class ArrayStack implements Stack{ public static final int CAPACITY = 1000; // default array capacity private E[] data; // generic array used for storage private int t = -1; // index of the top element in stack public ArrayStack() { this(CAPACITY); } // constructs stack with default capacity public ArrayStack(int capacity) { // constructs stack with given capacity data = (E[]) new Object[capacity]; // safe cast; compiler may give warning } public int size() { return (t + 1); } public boolean isEmpty() { return (t == -1); } public void push(E e) throws IllegalStateException { if (size() == data.length) throw new IllegalStateException("Stack is full"); data[++t] = e; // increment t before storing new item } public E top() { if (isEmpty()) return null; return data[t]; } public E pop() { if (isEmpty()) return null; E answer = data[t]; data[t] = null; // dereference to help garbage collection t--; return answer; } public boolean equals( Object o){ if( ! ( o instanceof ArrayStack ) ) return false; ArrayStack a = ( ArrayStack ) o; return data == a.data; }

}

LinkedStack Code:

public class LinkedStack implements Stack{ private SinglyLinkedList list = new SinglyLinkedList<>(); //an empty list public LinkedStack() {} // new stack relies on the initially empty list public int size() { return list.size(); } public boolean isEmpty() { return list.isEmpty(); } public void push(E element) { list.addFirst(element); } public E top() { return list.first(); } public E pop() { return list.removeFirst(); } }

ArrayQueue Code:

public class ArrayQueue implements Queue { // instance variables private E[] data; // generic array used for storage private int f = 0; // index of the front element private int sz = 0; // current number of elements // constructors public ArrayQueue() {this(CAPACITY);} // constructs queue with default capacity public ArrayQueue(int capacity) { // constructs queue with given capcity data = (E[]) new Object[capacity]; // safe cast; compiler may give warning } // methods /** Returns the number of elements in the queue. */ public int size() { return sz; } /** Tests whether the queue is empty. */ public boolean isEmpty() { return (sz == 0); } /** Inserts an element at the rear of the queue. */ public void enqueue(E e) throws IllegalStateException { if (sz == data.length) throw new IllegalStateException("Queue is full"); int avail = (f + sz) % data.length; // use modular arithmetic data[avail] = e; sz++; } /** Returns, but does not remove, the first element of the queue (null if empty). */ public E first() { if (isEmpty()) return null; return data[f]; } /** Removes and returns the first element of the queue (null if empty). */ public E dequeue() { if (isEmpty()) return null; E answer = data[f]; data[f] = null; // dereference to help garbage collection f = (f + 1) % data.length; sz--; return answer; } /** Tests whether data is equal or not */ public boolean equals( Object o){ if( ! ( o instanceof ArrayQueue ) ) return false; ArrayQueue a = ( ArrayQueue ) o; return data == a.data; }

}

LinkedQueue Code:

public class LinkedQueue implements Queue { private SinglyLinkedList list = new SinglyLinkedList<>(); // an empty list public LinkedQueue() {} // new queue relies on the initially empty list public int size() { return list.size(); } public boolean isEmpty() { return list.isEmpty(); } public void enqueue(E element) { list.addLast(element); } public E first() { return list.first(); } public E dequeue() { return list.removeFirst(); } }

There were other questions like this posted but I couldn't get the LinkedStack and LinkedQueue for loops to run

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Postgresql 16 Administration Cookbook Solve Real World Database Administration Challenges With 180+ Practical Recipes And Best Practices

Authors: Gianni Ciolli ,Boriss Mejias ,Jimmy Angelakos ,Vibhor Kumar ,Simon Riggs

1st Edition

1835460585, 978-1835460580

Students also viewed these Databases questions

Question

3. What are the current trends in computer hardware platforms?

Answered: 1 week ago