Question

package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic *...

package model;

import java.util.Iterator;

/**

* This class implements interface PriorityList<E> to represent a generic

* collection to store elements where indexes represent priorities and the

* priorities can change in several ways.

*

* This collection class uses an Object[] data structure to store elements.

*

* @param <E> The type of all elements stored in this collection

*/

// You will have an error until you have have all methods

// specified in interface PriorityList included inside this class.

public class ArrayPriorityList<E> implements PriorityList<E>,Iterable<E>{

// You must use these instance variables and the constructor

// below that takes an initalCapacity argument in the unit test.

private Object[] data;

private int n;

// Create an empty list with zero elements

public ArrayPriorityList(int initialCapacity) {

data = new Object[initialCapacity];

n = 0;

}

/**

* Return the number of elements currently in this PriorityList

*

* @return The number of elements in this PriorityList

*/

public int size() {

return n;

}

/**

* Return true if there are zero elements in this PriorityList

*

* @return true if size() == 0 or false if size() > 0

*/

public boolean isEmpty() {

if (size() == 0) {

return true;

} else {

return false;

}

}

/**

* If possible, insert the element at the given index. If index is out of the

* valid range of 0..size(), throw new IllegalArgumentException(); When size is

* 3, the only possible values for index are 0, 1, 2, AND 3 because you can add

* an element as the new last.

*

* @param index The index of the element to move.

* @param el The element to insert

* @throws IllegalArgumentException

*/

public void insertElementAt(int index, E el) throws IllegalArgumentException {

if (index < 0 || index > size()) {

throw new IllegalArgumentException();

} else if (size() == data.length) {

addmorecapacity();

}

for (int i = size(); i > index; i--) {

data[i] = data[i - 1];

}

n++;

data[index] = el;

}

/**

* If possible, return a reference to the element at the given index. If index

* is out of the valid range of 0..size()-1, throw new

* IllegalArgumentException(); When size is 3, the only possible values for

* index are 0, 1, and 2.

*

* @param index The index of the element to move.

* @return A reference to the element at index index.

* @throws IllegalArgumentException

*/

@SuppressWarnings("unchecked")

public E getElementAt(int index) throws IllegalArgumentException {

if (index < 0 || index >= size()) {

throw new IllegalArgumentException();

}

return (E) data[index];

}

/**

* If possible, remove the element at the given index. If index is out of the

* valid range of 0..size()-1, throw new IllegalArgumentException(); When size

* is 3, the only possible values for index are 0, 1, and 2.

*

* @param index The index of the element to move.

* @throws IllegalArgumentException

*/

public void removeElementAt(int index) throws IllegalArgumentException {

if (index < 0 || index >= size()) {

throw new IllegalArgumentException();

}

data[index] = null;

for (int i = index; i < n - 1; i++)

data[i] = data[i + 1];

data[n - 1] = null;

n--;

}

/**

* If possible, swap the element located at index with the element at index + 1.

* An attempt to lower the priority of the element at index size()-1 has no

* effect. If index is out of the valid range of 0..size()-1, throw new

* IllegalArgumentException(); When size is 3, the only possible values for

* index are 0, 1, and 2.

*

* @param index The index of the element to move

* @throws IllegalArgumentException

*/

public void lowerPriorityOf(int index) throws IllegalArgumentException {

int size=size()-1;

if (index < 0 || index > size) {

throw new IllegalArgumentException();

}

if (index >= 0 && index < size) {

Object temp = new Object();

temp = data[index + 1];

data[index + 1] = data[index];

data[index] = temp;

}

}

/**

* If possible, swap the element located at index with the element at index-1.

* An attempt to raise the priority at index 0 has no effect. If index is out of

* the valid range of 0..size()-1, throw new IllegalArgumentException(); When

* size is 3, the only possible values for index are 0, 1, and 2.

*

* @param index The index of the element to move

* @throws IllegalArgumentException

*/

public void raisePriorityOf(int index) throws IllegalArgumentException {

int size=size()-1;

if (index < 0 || index > size) {

throw new IllegalArgumentException();

}

if (index >= 0 && index < size) {

Object temp = new Object();

temp = data[index - 1];

data[index - 1] = data[index];

data[index] = temp;

}

}

/**

* Return a copy of all elements as an array of Objects that is the size of this

* PriorityList and in the same order. If there are no elements in this list,

* return new Object[0]; A change to the return value must not affect this

* PriorityList object.

*

* @return An array of Objects where capacity == size()

*/

public Object[] toArray() {

Object result[] = new Object[size()];

int size=size();

for (int i = 0; i < size; i++) {

Object temp = data[i];

result[i] = temp;

i++;

}

return result;

}

/**

* If possible, move the element at the given index to the end of this list. An

* attempt to move the last element to the last has no effect. If the index is

* out of the valid range 0..size()-1 throw new IllegalArgumentException(); When

* size is 3, the only possible values for index are 0, 1, and 2.

*

* @param index The index of the element to move.

* @throws IllegalArgumentException

*/

public void moveToLast(int index) throws IllegalArgumentException {

int size=size()-1;

if (index < 0 || index > size) {

throw new IllegalArgumentException();

}

if (index < size) {

Object temp = data[index];

for (int i = index; i < size; i++)

data[i] = data[i + 1];

data[size] = temp;

}

}

/**

* If possible, move the element at the given index to the front of this list An

* attempt to move the top element to the top has no effect. If the index is out

* of the valid range of 0..size()-1, throw new IllegalArgumentException(); When

* size is 3, the only possible values for index are 0, 1, and 2.

*

* @param index The index of the element to move.

*

* @throws IllegalArgumentException

*/

public void moveToTop(int index) throws IllegalArgumentException {

int size=size()-1;

if (index < 0 || index > size)

throw new IllegalArgumentException();

if (index > 0) {

Object temp = data[index];

for (int i = index; i > 0; i--)

data[i] = data[i - 1];

data[0] = temp;

}

}

private void addmorecapacity() {

Object temp[] = new Object[n + 5];

for (int i = 0; i < n; i++)

temp[i] = data[i];

data = temp;

}

@Override

public Iterator<E> iterator() {

return new Arrayiterator();

}

private class Arrayiterator<E> implements Iterator<E>{

@Override

public boolean hasNext() {

// TODO Auto-generated method stub

return false;

}

@Override

public E next() {

// TODO Auto-generated method stub

return null;

}

}

}

I need to add the iterator to this ArrayPrioritylist and i don't really know how to do it.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package model;

import java.util.Iterator;

/**

* This class implements interface PriorityList<E> to represent a generic

* collection to store elements where indexes represent priorities and the

* priorities can change in several ways.

*

* This collection class uses an Object[] data structure to store elements.

*

* @param <E> The type of all elements stored in this collection

*/

// You will have an error until you have have all methods

// specified in interface PriorityList included inside this class.

public class ArrayPriorityList<E> implements PriorityList<E>, Iterable<E>{

       // You must use these instance variables and the constructor

       // below that takes an initalCapacity argument in the unit test.

       private Object[] data;

       private int n;

      

       // Create an empty list with zero elements

       public ArrayPriorityList(int initialCapacity) {

       data = new Object[initialCapacity];

       n = 0;

       }

      

       /**

       * Return the number of elements currently in this PriorityList

       *

       * @return The number of elements in this PriorityList

       */

       public int size() {

       return n;

       }

       /**

       * Return true if there are zero elements in this PriorityList

       *

       * @return true if size() == 0 or false if size() > 0

       */

       public boolean isEmpty() {

       if (size() == 0) {

       return true;

       } else {

       return false;

       }

       }

      

       /**

       * If possible, insert the element at the given index. If index is out of the

       * valid range of 0..size(), throw new IllegalArgumentException(); When size is

       * 3, the only possible values for index are 0, 1, 2, AND 3 because you can add

       * an element as the new last.

       *

       * @param index The index of the element to move.

       * @param el The element to insert

       * @throws IllegalArgumentException

       */

       public void insertElementAt(int index, E el) throws IllegalArgumentException {

       if (index < 0 || index > size()) {

       throw new IllegalArgumentException();

       } else if (size() == data.length) {

       addmorecapacity();

       }

       for (int i = size(); i > index; i--) {

       data[i] = data[i - 1];

       }

       n++;

       data[index] = el;

       }

      

       /**

       * If possible, return a reference to the element at the given index. If index

       * is out of the valid range of 0..size()-1, throw new

       * IllegalArgumentException(); When size is 3, the only possible values for

       * index are 0, 1, and 2.

       *

       * @param index The index of the element to move.

       * @return A reference to the element at index index.

       * @throws IllegalArgumentException

       */

       @SuppressWarnings("unchecked")

       public E getElementAt(int index) throws IllegalArgumentException {

       if (index < 0 || index >= size()) {

       throw new IllegalArgumentException();

       }

       return (E) data[index];

       }

      

       /**

       * If possible, remove the element at the given index. If index is out of the

       * valid range of 0..size()-1, throw new IllegalArgumentException(); When size

       * is 3, the only possible values for index are 0, 1, and 2.

       *

       * @param index The index of the element to move.

       * @throws IllegalArgumentException

       */

       public void removeElementAt(int index) throws IllegalArgumentException {

       if (index < 0 || index >= size()) {

       throw new IllegalArgumentException();

       }

       data[index] = null;

       for (int i = index; i < n - 1; i++)

       data[i] = data[i + 1];

       data[n - 1] = null;

       n--;

       }

      

       /**

       * If possible, swap the element located at index with the element at index + 1.

       * An attempt to lower the priority of the element at index size()-1 has no

       * effect. If index is out of the valid range of 0..size()-1, throw new

       * IllegalArgumentException(); When size is 3, the only possible values for

       * index are 0, 1, and 2.

       *

       * @param index The index of the element to move

       * @throws IllegalArgumentException

       */

       public void lowerPriorityOf(int index) throws IllegalArgumentException {

       int size=size()-1;

       if (index < 0 || index > size) {

       throw new IllegalArgumentException();

       }

       if (index >= 0 && index < size) {

       Object temp = new Object();

       temp = data[index + 1];

       data[index + 1] = data[index];

       data[index] = temp;

       }

       }

      

       /**

       * If possible, swap the element located at index with the element at index-1.

       * An attempt to raise the priority at index 0 has no effect. If index is out of

       * the valid range of 0..size()-1, throw new IllegalArgumentException(); When

       * size is 3, the only possible values for index are 0, 1, and 2.

       *

       * @param index The index of the element to move

       * @throws IllegalArgumentException

       */

       public void raisePriorityOf(int index) throws IllegalArgumentException {

       int size=size()-1;

       if (index < 0 || index > size) {

       throw new IllegalArgumentException();

       }

       if (index >= 0 && index < size) {

       Object temp = new Object();

       temp = data[index - 1];

       data[index - 1] = data[index];

       data[index] = temp;

       }

       }

      

       /**

       * Return a copy of all elements as an array of Objects that is the size of this

       * PriorityList and in the same order. If there are no elements in this list,

       * return new Object[0]; A change to the return value must not affect this

       * PriorityList object.

       *

       * @return An array of Objects where capacity == size()

       */

       public Object[] toArray() {

       Object result[] = new Object[size()];

       int size=size();

       for (int i = 0; i < size; i++) {

       Object temp = data[i];

       result[i] = temp;

       i++;

       }

       return result;

       }

      

       /**

       * If possible, move the element at the given index to the end of this list. An

       * attempt to move the last element to the last has no effect. If the index is

       * out of the valid range 0..size()-1 throw new IllegalArgumentException(); When

       * size is 3, the only possible values for index are 0, 1, and 2.

       *

       * @param index The index of the element to move.

       * @throws IllegalArgumentException

       */

       public void moveToLast(int index) throws IllegalArgumentException {

       int size=size()-1;

       if (index < 0 || index > size) {

       throw new IllegalArgumentException();

       }

       if (index < size) {

       Object temp = data[index];

       for (int i = index; i < size; i++)

       data[i] = data[i + 1];

       data[size] = temp;

       }

       }

      

       /**

       * If possible, move the element at the given index to the front of this list An

       * attempt to move the top element to the top has no effect. If the index is out

       * of the valid range of 0..size()-1, throw new IllegalArgumentException(); When

       * size is 3, the only possible values for index are 0, 1, and 2.

       *

       * @param index The index of the element to move.

       *

       * @throws IllegalArgumentException

       */

       public void moveToTop(int index) throws IllegalArgumentException {

       int size=size()-1;

       if (index < 0 || index > size)

       throw new IllegalArgumentException();

       if (index > 0) {

       Object temp = data[index];

       for (int i = index; i > 0; i--)

       data[i] = data[i - 1];

       data[0] = temp;

       }

       }

      

       private void addmorecapacity() {

             Object temp[] = new Object[n + 5];

             for (int i = 0; i < n; i++)

             temp[i] = data[i];

             data = temp;

             }

      

       @Override

       public Iterator<E> iterator() {

             return new Arrayiterator();

       }

      

       private class Arrayiterator<E> implements Iterator<E>{

            

             private int curr; // current element of the priority list

            

             // initialize iterator to start element index 0

             public Arrayiterator()

             {

                    curr = 0;

             }

            

             // method to check if the list has next element

             @Override

             public boolean hasNext() {

                    return(curr < size());

             }

             // method to return the next element in the list

             @Override

             public E next() {

                    E elem = (E) getElementAt(curr);

                    curr++;

                    return elem;

             }

             }

}

Add a comment
Know the answer?
Add Answer to:
package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic *...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement the missing methods in java! Thanks! import java.util.Iterator; import java.util.NoSuchElementException; public class HashTableOpenAddressing<K, V> implements...

    Implement the missing methods in java! Thanks! import java.util.Iterator; import java.util.NoSuchElementException; public class HashTableOpenAddressing<K, V> implements DictionaryInterface<K, V> { private int numEntries; private static final int DEFAULT_CAPACITY = 5; private static final int MAX_CAPACITY = 10000; private TableEntry<K, V>[] table; private double loadFactor; private static final double DEFAULT_LOAD_FACTOR = 0.75; public HashTableOpenAddressing() { this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR); } public HashTableOpenAddressing(int initialCapacity, double loadFactorIn) { numEntries = 0; if (loadFactorIn <= 0 || initialCapacity <= 0) { throw new IllegalArgumentException("Initial capacity and load...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)...

    Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)    {        this.size = size;    }    /** Reference to list head. */    private Node<E> head = null;    /** The number of items in the list */    private int size = 0;       /** Add an item to the front of the list.    @param item The item to be added    */    public void addFirst(E...

  • Methods enforced by the set interface: import java.util.Iterator; public interface Set<E> { /** Removes all of...

    Methods enforced by the set interface: import java.util.Iterator; public interface Set<E> { /** Removes all of the elements from this set */ void clear(); /** Returns true if this set contains no elements @return true if this set contains no elements */ boolean isEmpty(); /** Returns the number of elements in this set (its cardinality) @return the number of elements in this set */ int size(); /** Returns an iterator over the elements in this set @return an iterator over...

  • ANNOTATE BRIEFLY LINE BY LINE THE FOLLOWING CODE: package shop.data; import junit.framework.Assert; import junit.framework.TestCase; public class...

    ANNOTATE BRIEFLY LINE BY LINE THE FOLLOWING CODE: package shop.data; import junit.framework.Assert; import junit.framework.TestCase; public class DataTEST extends TestCase { public DataTEST(String name) { super(name); } public void testConstructorAndAttributes() { String title1 = "XX"; String director1 = "XY"; String title2 = " XX "; String director2 = " XY "; int year = 2002; Video v1 = Data.newVideo(title1, year, director1); Assert.assertSame(title1, v1.title()); Assert.assertEquals(year, v1.year()); Assert.assertSame(director1, v1.director()); Video v2 = Data.newVideo(title2, year, director2); Assert.assertEquals(title1, v2.title()); Assert.assertEquals(director1, v2.director()); } public void testConstructorExceptionYear()...

  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> {    // Data...

    Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> {    // Data fields    /** The default initial capacity */    private static final int INITIAL_CAPACITY = 10;       /** The underlying data array */    private E[] theData;       /** The current size */    private int size = 0;       /** The current capacity */    private int capacity = 0;       @SuppressWarnings("unchecked")    public KWArrayList() {        capacity...

  • Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /**...

    Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /** Removes all of the elements from this set */ void clear(); /** Returns true if this set contains no elements @return true if this set contains no elements */ boolean isEmpty(); /** Returns the number of elements in this set (its cardinality) @return the number of elements in this set */ int size(); /** Returns an iterator over the elements in this set @return...

  • Array with Iterator. Java style Implement an array data structure as a class JstyArray<E> to support...

    Array with Iterator. Java style Implement an array data structure as a class JstyArray<E> to support the Iterable interface such that the following code works: JstyArray<Integer> data; data = new JstyArray<Integer>(10); for (int i = 0; i < 10; ++i) { data.set(i, new Integer(i) ); } int sum = 0; for ( int v : data ) { if (v == null) continue; // empty cell sum += v; } The iterator provided by this class follows the behaviour of...

  • Given the Interface Code write a java class that implements this interface and show the working...

    Given the Interface Code write a java class that implements this interface and show the working functionality in the main method: public interface CustomList<T> { /** * This method should add a new item into the <code>CustomList</code> and should * return <code>true</code> if it was successfully able to insert an item. * @param item the item to be added to the <code>CustomList</code> * @return <code>true</code> if item was successfully added, <code>false</code> if the item was not successfully added (note: it...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT