Question
Please help me with this project! THANK YOU. import java.util.Arrays; import java.util.Iterator; import java.util.Collection; import java.util.NoSuchElementException; // Class ArrayIntList can be used to store a
Please help me with this project! THANK YOU.
import java.util.Arrays;
import java.util.Iterator;
import java.util.Collection;
import java.util.NoSuchElementException;
// 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
public class ArrayIntList implements Iterable
private int[] elementData; // list of integers
private int size = 0; // current number of elements in the list
public static final int DEFAULT_CAPACITY = 10;
// 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
throw new IllegalArgumentException("Capacity must be at least
0: " + capacity);
}
elementData = new int[capacity];
}
// for creating test cases more easily, Reges provided ... arg
public ArrayIntList(int... elements) {
this(Math.max(DEFAULT_CAPACITY, elements.length * 2));
for (int n : elements) {
add(n);
}
}
// for creating test cases more easily (a dupe of the above
constructor)
public static ArrayIntList withValues(int... elements) {
ArrayIntList list = new ArrayIntList(Math.max(DEFAULT_CAPACITY,
elements.length * 2));
for (int n : elements) {
list.add(n);
}
return list;
}
// for creating test cases more easily
public ArrayIntList(int element, boolean notCapacity) {
this();
add(element);
}
// for creating test cases more easily
public ArrayIntList(Collection
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()
// post: appends the given value to the end of the list
public void add(int value) {
add(size, value);
}
// pre: size()
// post: inserts the given value at the given index, shifting
subsequent
// values right
public void add(int index, int value) {
checkIndex(index, 0, size);
ensureCapacity(size + 1);
for (int i = size; i > 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
add(other.elementData[i]);
}
}
// post: list is empty
public void clear() {
size = 0;
}
// post: returns true if the given value is contained in the list,
false otherwise
public boolean contains(int value) {
return indexOf(value) != -1;
}
// post: ensures that the underlying array has the given capacity; if
not,
// the size is doubled (or more if given capacity is even larger)
public void ensureCapacity(int capacity) {
if (capacity > elementData.length) {
int newCapacity = elementData.length * 2 + 1;
if (capacity > newCapacity) {
newCapacity = capacity;
}
int[] newList = new int[newCapacity];
for (int i = 0; i
newList[i] = elementData[i];
}
elementData = newList;
}
}
// returns true if o is an ArrayIntList with the same size and
elements as this one
public boolean equals(Object o) {
if (!(o instanceof ArrayIntList)) {
return false;
}
ArrayIntList other = (ArrayIntList) o;
if (this.size != other.size) {
return false;
}
for (int i = 0; i
if (elementData[i] != other.elementData[i]) {
return false;
}
}
return true;
}
// pre : 0
// post: returns the integer at the given index in the list
public int get(int index) {
checkIndex(index);
return elementData[index];
}
// post: returns capacity of this list's underlying array
public int getCapacity() {
return elementData.length;
}
// post : returns the position of the first occurence of the given
// value (-1 if not found)
public int indexOf(int value) {
for (int i = 0; i
if (elementData[i] == value) {
return i;
}
}
return -1;
}
// post: returns true if list is empty, false otherwise
public boolean isEmpty() {
return size == 0;
}
// post: returns an iterator for this list
public Iterator
return new ArrayIntListIterator(this);
}
// pre : 0
// post: removes value at the given index, shifting subsequent values
left
public void remove(int index) {
checkIndex(index);
for (int i = index; i
elementData[i] = elementData[i + 1];
}
size--;
}
// post: removes all occurrences of the values in the given list from
this list
public void removeAll(ArrayIntList other) {
int newSize = 0;
for (int i = 0; i
if (!other.contains(elementData[i])) {
elementData[newSize] = elementData[i];
newSize++;
}
size = newSize;
}
}
// pre : 0
// post: replaces the integer at the given index with the given value
public void set(int index, int value) {
checkIndex(index);
elementData[index] = value;
}
// post: returns the current number of elements in the list
public int size() {
return size;
}
// post: returns an array version of the contents of this list
public int[] toArray() {
return Arrays.copyOf(elementData, size);
}
// post: creates a comma-separated, bracketed version of the list
public String toString() {
String result = "[";
for (int i = 0; i
if (i > 0) {
result += ", ";
}
if (i
result += elementData[i];
} else {
// student's code is bogus; OOB
result += "OOB!";
}
}
result += "]";
return result;
}
// helpers to make sure indexes do not fall out of bounds
private void checkIndex(int index) {
checkIndex(index, 0, size - 1);
}
private void checkIndex(int index, int min, int max) {
if (!(min
throw new ArrayIndexOutOfBoundsException("Illegal index " +
index +
"; must be between " + min + " and " + max + " "
+ "list : " + toString() + " (size=" + size + ") "
+ "array: " + Arrays.toString(elementData) + "
(capacity=" + elementData.length + ")");
}
}
// Stuart Reges
// 4/4/05
//
// The ArrayIntListIterator class provides a set of utilities for
iterating
// over an ArrayIntList and possibly removing values from the list.
public static class ArrayIntListIterator implements Iterator
{
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
}
// pre : hasNext()
// post: returns the next element in the iteration
public Integer next() {
if (!hasNext())
throw new NoSuchElementException();
int result = list.get(position);
position++;
removeOK = true;
return result;
}
// pre : next() has been called without a call on remove (i.e., at
most one
// call per call on next)
// post: removes the last element returned by the iterator
public void remove() {
if (!removeOK)
throw new IllegalStateException();
list.remove(position - 1);
position--;
removeOK = false;
}
}
}
import java.io.*;
import java.util.*;
/* 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);
}
}
Chapter 14: Programming Project. A. Create ArraylntStack.java (one that's REALLY O(constant), totally!!!) B. Create the Javadoc web page (separate Assignment #15b) A. Create your own Stack Class, call it ArraylntStack.java and write the standard four public Stack methods as listed below (this is the easy part of the assignment). Use at least one private helper method, maybe to ensure capacity of the array. All methods in your ArraylntStack must have O(constant) run-time. Your ArraylntStack extends no other Classes from the Java API. You must provide a default constructor 1. boolean empty; Tests if this stack is empty (watch spelling here, as I will) 2. int peek); Looks at the object at the top of this stack without removing it from the stack. 3. int popl; Removes the object at the top of this stack and returns that object as the value of this function 4. int push(int item); Pushes an item onto the top of this stack, return what was pushed Additional methods, code, etc... will also need to be provided, so the client can use an iterator: 5. public class IntStacklterator { II similar to what the BJP authors show, but without a remove) implemented 6. public IntStacklterator iterator) method so the client can browse through the data with.next0 etc... Start the iterator at the top of the stack!!! Suggestion: Look at ArraylntList.javaas a reference to start his Assignment, Notice how ArrayintList.javais fundamentally the ArraylntList4 from the Chapter 15 downloads, plus an internal Iterator as used by Practice-IT, plus many other constructors (..) and operators that you do not need to do for this Assignment. Understand how ArrayintList.javaworks, and this Assignment will start to look easy Iterate through your array from top (end) to bottom (index 0), so the top of the Stack is returned from the first.next0 This is backwards from the textbook ArraylntListlterator example, but what I require for your Stack, as then you need to really understand what an iterator is doing, and Stacks are kind of backwards when compared to Lists, the first thing you ee is the top. It only makes sense, so push and pop work on the top (end) of the array. You must throw an "appropriate" Exception (e.g. EmptyStackException) for illegal peek, pop, etc... operations. Regarding size, let's say this remains O(constant) while the Stack size is less than 20 elements, beyond that the push(int) would need to throw a Stack Overflow Upload your ArraylntStack.java (one file) Here's some test code that outlines what might work when done above Post,javaStep 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