Question
I need help with this coding. I saw there are a lot of the same ones already posted but I could not find any of
I need help with this coding. I saw there are a lot of the same ones already posted but I could not find any of them to work correctly so please do not copy and paste from other posts. Thank you!
import java.io.*;
import java.util.*;
/* Winter 2017, CS211, W.P. Iverson, instructor and author
* This is a start at testing code for Chapter 15 Assignment
* Other tests can and will happen....
*/
public class Post {
public static void main(String[] args) throws FileNotFoundException {
// required:
ArrayIntStack bag15 = new ArrayIntStack();
try {
bag15.pop();
} catch (EmptyStackException e) {
System.out.println(e);
}
bag15.push(42); bag15.push(24); bag15.push(-33);
// works without the generic Iterator from Oracle
ArrayIntStack.IntStackIterator handle = bag15.iterator();
while (handle.hasNext()) System.out.println(handle.next());
System.out.println(bag15.empty());
System.out.println(bag15.push(42));
System.out.println(bag15.pop());
System.out.println(bag15.peek());
// optional:
// if you implement Iterable
//for (Integer i : bag15) System.out.println(i);
}
}
THIS CODE IS ONLY HERE TO SHOW HOW IT'S SOMEWHAT SUPPOSED TO BE SIMILAR TO THIS.
import java.util.Arrays; import java.util.Iterator; import java.util.Collection; import java.util.NoSuchElementException; // BJP code fixed (by W.P. Iverson, 2017) to import only what's needed // Class ArrayIntList can be used to store a list of integers. // // Most of this code from // http://practiceit.cs.washington.edu/problems/shared/ArrayIntList.java // // Reges and Stepp provided inner class iterator (see end of file) // and also provided (...) arguments as use in constructor public class ArrayIntList implements Iterable
// YOUR CODE GOES HERE
// post: constructs an empty list of default capacity public ArrayIntList() { this(DEFAULT_CAPACITY); } // pre : capacity >= 0 // post: constructs an empty list with the given capacity private ArrayIntList(int capacity) { if (capacity elements) { this(Math.max(DEFAULT_CAPACITY, elements.size() * 2)); for (int n : elements) { add(n); } } // copy constructor; for creating test cases more easily public ArrayIntList(ArrayIntList list) { this(Math.max(DEFAULT_CAPACITY, list.size() * 2)); addAll(list); } // pre : size() index; i--) { elementData[i] = elementData[i - 1]; } elementData[index] = value; size++; } // post: appends all values in the given list to the end of this list public void addAll(ArrayIntList other) { for (int i = 0; i elementData.length) { int newCapacity = elementData.length * 2 + 1; if (capacity > newCapacity) { newCapacity = capacity; } int[] newList = new int[newCapacity]; for (int i = 0; i
ArrayIntList other = (ArrayIntList) o; if (this.size != other.size) { return false; } for (int i = 0; i iterator() { return new ArrayIntListIterator(this); } // pre : 0 0) { result += ", "; } if (i { private ArrayIntList list; // list to iterate over private int position; // current position within the list private boolean removeOK; // whether it's okay to remove now // pre : list != null // post: constructs an iterator for the given list public ArrayIntListIterator(ArrayIntList list) { this.list = list; position = 0; removeOK = false; } // post: returns true if there are more elements left, false otherwise public boolean hasNext() { return position
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started