Write a Client class with a main method that tests the data structures as follows:
public class ArrayStack<E> implements Stack<E>
{
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) { //contsructs stack with given
capacity
data = (E[]) new Object[capacity]; //safe cast; compiler may give
warning
}
@Override
public int size() {return(t + 1);}
@Override
public boolean isEmpty() { return(t == -1); }
@Override
public void push(E e) throws IllegalStateException {
if(size() == data.length) throw new IllegalStateException("Stack is
full");
data[++t] = e; //increments t before storing new item
}
public E top() {
if(isEmpty()) return null;
return data[t];
}
@Override
public E pop() {
if(isEmpty()) return null;
E answer = data[t];
data[t] = null; //dereference to help garbage collection
t--;
return answer;
}
}
public class LinkedStack<E> implements Stack<E> {
private SinglyLinkedList<E> list = new
SinglyLinkedList<>(); // an empty list
public LinkedStack() {} // new stack relies on the initially empty
list
@Override
public int size() { return list.size(); }
@Override
public boolean isEmpty() { return list.isEmpty(); }
@Override
public void push(E element) { list.addFirst(element); }
public E top() { return list.first(); }
@Override
public E pop() { return list.removeFirst(); }
}
public class ArrayQueue<E> implements Queue<E>
{
// instance variables
public static final int CAPACITY = 1000; //default array
capacity
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
capacity
data = (E[ ]) new Object[capacity]; // safe cast; compiler may give
warning
}
// methods
/** Returns the number of elements in the queue. */
@Override
public int size()
{ return sz; }
/** Tests whether the queue is empty. */
@Override
public boolean isEmpty()
{ return (sz == 0);}
/** Inserts an element at the rear of the queue. */
@Override
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). */
@Override
public E first() {
if (isEmpty()) return null;
return data[f];
}
/** Removes and returns the first element of the queue (null if
empty). */
@Override
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;
}
}
public class LinkedQueue<E> implements Queue<E>
{
private SinglyLinkedList<E> list = new
SinglyLinkedList<>(); // an empty list
public LinkedQueue() {} // new queue relies on the initially empty
list
@Override
public int size() { return list.size(); }
@Override
public boolean isEmpty() { return list.isEmpty(); }
@Override
public void enqueue(E element) { list.addLast(element); }
@Override
public E first() { return list.first(); }
@Override
public E dequeue() { return list.removeFirst(); }
}
To solve the given problem, the overall approach is to first add N integers to the concerned structure and then delete those N elements after the addition has been completed. To do this, run a for loop varying the value of N from 10 to 100,000,000 while checking for exception(outofmemoryerror) using try and catch block every time the structure is created of capacity N. When an exception is thrown, the loop breaks and goes to the next loop for the next structure. The time elapsed is calculated using System.nanoTime() which gives the current value of time and the elapsed time is calculated by tend - tstart.
In the ending, a method is created which makes a 2d array corresponding to the value of N and time. There are 8 of these arrays. Two(add and delete) for each structure.
To create the ASCII table format(which is not shown in the question by the way), first, find out the maximum value of time and N and then convert them to string and store the corresponding length of the strings. Then run the for loop for the size of the array print the value of N and time with padding where the padding also includes the difference between the maximum string length and the current string length along with padding of size 2 on both the sides. In the below-given segment of code, I have only done this for the value of N but one can do this for the value of time also.
Remark: The code given below is giving an overall idea of what is to be done. It might not be accurate but is provided just for the sake of understanding. Also, the above given code in the question contains some error and needs to be corrected.
public class test
{
public static void main(String[] args) throws Exception
{
ArrayList<Float> asarray = new ArrayList<>();
ArrayList<Float> lsarray = new ArrayList<>();
ArrayList<Float> aqarray = new ArrayList<>();
ArrayList<Float> lqarray = new ArrayList<>();
ArrayList<Float> dasarray = new ArrayList<>();
ArrayList<Float> dlsarray = new ArrayList<>();
ArrayList<Float> daqarray = new ArrayList<>();
ArrayList<Float> dlqarray = new ArrayList<>();
int n = 0;
for(int i = 10; i<=100000000; i = i*10)
{
try
{
ArrayStack<Integer> as = new ArrayStack<>(i);
float tstart = (float)System.nanoTime();
for(int k = 0; k < i; k++)
{
as.push(1);
}
float tend = (float)System.nanoTime();
float time = 0;
if(i==10)
time = tend-tstart;
else
time = asarray.get(asarray.size()-1) + tend - tstart;
asarray.add(time);
n = i;
float dtstart = (float)System.nanoTime();
for(int k = 0; k < n; k++)
{
as.pop();
}
float dtend = (float)System.nanoTime();
float dtime =0;
if(i==10)
dtime = dtend-dtstart;
else
dtime = dasarray.get(dasarray.size()-1) + dtend - dtstart;
dasarray.add(dtime);
}
catch(OutOfMemoryError E)
{
break;
}
}
n = 0;
for(int i = 10; i<=100000000; i = i*10)
{
try
{
LinkedStack<Integer> ls = new
LinkedStack<>(i);
float tstart = (float)System.nanoTime();
for(int k = 0; k < i; k++)
{
ls.push(1);
}
float tend =(float)System.nanoTime();
float time =0;
if(i==10)
time = tend-tstart;
else
time = lsarray.get(lsarray.size()-1) + tend - tstart;
lsarray.add(time);
n = i;
float dtstart = (float)System.nanoTime();
for(int k = 0; k < n; k++)
{
ls.pop();
}
float dtend = (float)System.nanoTime();
float dtime =0;
if(i==10)
dtime = dtend-dtstart;
else
dtime = dlsarray.get(dlsarray.size()-1) + dtend - dtstart;
dlsarray.add(dtime);
}
catch(OutOfMemoryError E)
{
break;
}
}
n = 0;
for(int i = 10; i<=100000000; i = i*10)
{
try
{
ArrayQueue<Integer> aq = new ArrayQueue<>(i);
float tstart = (float)System.nanoTime();
for(int k = 0; k < i; k++)
{
aq.enqueue(1);
}
float tend =(float)System.nanoTime();
float time =0;
if(i==10)
time = tend-tstart;
else
time = aqarray.get(aqarray.size()-1) + tend - tstart;
aqarray.add(time);
n = i;
float dtstart =(float)System.nanoTime();
for(int k = 0; k < n; k++)
{
aq.dequeue();
}
float dtend = (float)System.nanoTime();
float dtime =0;
if(i==10)
dtime = dtend-dtstart;
else
dtime = daqarray.get(daqarray.size()-1) + dtend - dtstart;
daqarray.add(dtime);
}
catch(OutOfMemoryError E)
{
break;
}
}
n = 0;
for(int i = 10; i<=100000000; i = i*10)
{
try
{
LinkedQueue<Integer> lq = new
LinkedQueue<>(i);
float tstart = (float)System.nanoTime();
for(int k = 0; k < i; k++)
{
lq.enqueue(1);
}
float tend =(float)System.nanoTime();
float time =0;
if(i==10)
time = tend-tstart;
else
time = lqarray.get(lqarray.size()-1) + tend - tstart;
lqarray.add(time);
n = i;
float dtstart =(float)System.nanoTime();
for(int k = 0; k < n; k++)
{
lq.dequeue();
}
float dtend = (float)System.nanoTime();
float dtime =0;
if(i==10)
dtime = dtend-dtstart;
else
dtime = dlqarray.get(dlqarray.size()-1) + dtend - dtstart;
dlqarray.add(dtime);
}
catch(OutOfMemoryError E)
{
break;
}
}
float[][] as = to2d(asarray);
float[][] das = to2d(dasarray);
float[][] ls = to2d(lsarray);
float[][] dls = to2d(dlsarray);
float[][] aq = to2d(aqarray);
float[][] daq = to2d(daqarray);
float[][] lq = to2d(lqarray);
float[][] dlq = to2d(dlqarray);
totable(asarray);
totable(dasarray);
totable(lsarray);
totable(dlsarray);
totable(aqarray);
totable(daqarray);
totable(lqarray);
totable(dlqarray);
}
public float[][] to2d(ArrayList<Float> asarray)
{
float[][] asarr = new float[asarray.size()][2];
float m=10;
for(int i=0;i<asarray.size();i++)
{
asarr[i][0]=m;
m=m*10;
asarr[i][1]=asarray.get(i);
}
return asarr;
}
public void totable(float[][] arr)
{
int maxlen = 9
System.out.print("| N |"+"| time ");
for(int i=0;i<arr.length(0);i++)
{
int curlen = (String.valueOf(arr[i][0])).length();
String space = "";
for(int k = 0; k < maxlen - curlen; k++)
{
space += " "
}
System.out.print("| " + arr[i][0] + space + " |");
System.out.print("| " + arr[i][1] + " |");
System.out.println();
}
}
}
Write a Client class with a main method that tests the data structures as follows: For...
Write a method with signature "concatenate(LinkedQueue<E> Q2)" for the LinkedQueue<E> class that takes all elements of Q2 and appends them to the end of the original queue. The operation should run in O(1) time and should result in Q2 being an empty queue. Write the necessary code to test the method.You may just modify the SinglyLinkedList class to add necessary support. LinkedQueue Class public class LinkedQueue<E> implements Queue<E> { /** The primary storage for elements of the queue */ private...
Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizeIS is public int sizeIs() a.) Write the code for sizeIs for the ArrayStack class b.) Write the code for sizeIs for the LinkedStack class (do not add any instance variables to the class; each time sizeIs is called you must "walk" through the stack...
JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array storing elements in the list • topOfStack: an int value representing the location of the stack top in the array • INITIAL_CAPACITY: the default capacity of the stack public class ArrayBasedStack <E> { private E[] data; private int topOfStack; private static final int INITIAL_CAPACITY = 5; } Add a constructor that will initialize the stack with a user-defined initial capacity. The top of the...
Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...
Currently, I'm getting this as my output: "Exception in thread "main" java.lang.NullPointerException at InfixExpression.execute(InfixExpression.java:98) at InfixExpression.Evaluate(InfixExpression.java:65) at InfixExpression.setWholeExpr(InfixExpression.java:24) at InfixExpression.<init>(InfixExpression.java:17) at Main.testHW1(Main.java:17) at Main.main(Main.java:6)" I need to get this as my output: "Testing InfixExpression: When passing null, the String and double = Infix String: , result: 0.0 When passing a valid String, the String and double = Infix String: ( 234.5 * ( 5.6 + 7.0 ) ) / 100.2, result: 29.488023952095805 ..." I...
create a class named IntegerQueue given a singlylinkedqueue of integers, write the following methods: max(SinglyLinkedQueue<Integer> s) to return the max element in the queu. min(SinglyLinkedQueue<Integer> s) to return the min element in the queue. sum(SinglyLInkedQueue<Integer> s) to return the sum of elements in the queu. median(SinglyLinkedQueue<Integer> s) to return the median of elements in the queue. split(SinglyLinkedQueue<Integer> s) to separate the SinglyLinkedQueue into two ArrayQueues based on whether the element values are even or odd. package Stack_and_Queue; import java.util.Iterator; import...
Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable { // ---------------- nested Node class...
Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of the peek, isEmpty, size, and toString methods. See Base_A06Q1.java for a starting place and a description of these methods. Here is the base given: /** * Write a description of the program here. * * @author Lewis et al., (your name) * @version (program version) */ import java.util.Iterator; public class Base_A06Q1 { /** * Program entry point for stack testing. * @param args Argument...
Java/Queues ** Task: Write a JUnit test that shows a failure in some part of the ADT -----ArrayQueue.java------- public class ArrayQueue { private static final int INITIAL_CAPACITY = 2; // to permit easier testing private Object[] contents; private int front, rear; /** * Create an empty queue with an initial capacity. */ public ArrayQueue() { contents = new Object[INITIAL_CAPACITY]; } /** * Add an element to...
There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...