Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List
__________________________________________
public interface IntegerListInterface{
public boolean isEmpty();
//Determines whether a list is empty.
//Precondition: None.
//Postcondition: Returns true if the list is empty,
//otherwise returns false.
//Throws: None.
public int size();
// Determines the length of a list.
// Precondition: None.
// Postcondition: Returns the number of items in this
IntegerList.
//Throws: None.
public void removeAll();
// Deletes all the items form the list.
// Precondition: None.
// Postcondition: The list is Empty.
//Throws: None.
public void add(int index, int newItem) throws
ListIndexOutOfBoundsException;
//Adds an item to the list at positon index.
//Precondition: index indicated the position at which the item
should be inserted in the list.
//Postcondition: is insertion is succesful, item is at positon
index in the list, and other items are renumbered
accordingly.
//Throws: ListIndexOutOfBoundsException if index < 1 or index
> size()+1
//Throws: ListException if item cannot be placed on the list.
public int get(int index) throws
ListIndexOutOfBoundsException;
//Retrieves a list item by position.
//Precondition: index is the number of the item to be
retrieved
//Postcondition: If 1 <= index <= size(), the item at
position index in the list is returned.
//Throws: ListIndexOutOfBoundsException if index < 1 or index
> size().
public void remove(int index) throws
ListIndexOutOfBoundsException;
//Deleted an item from the list at a given position.
//Precondition index indicated where the deletion should
occur.
//Postcondition: if 1 <= index <= size(), the item at
position index in the list i s deleted, and other items are
renumbered accordingly.
//Throws: ListIndexOutOfBoundsException if index < 1 or index
> size().
} //end IntegerListInterface
_____________________________________________________________________
//Arraybased implementation of the ADT list.
public class ListArrayBased implements ListInterface {
private static final int MAX_LIST = 50;
private Object items[]; // an array of list items
private int numItems; // number of items in list
public ListArrayBased() {
items = new Object[MAX_LIST];
numItems = 0;
} // end default constructor
public boolean isEmpty() {
return (numItems == 0);
} // end isEmpty
public int size() {
return numItems;
} // end size
public void removeAll() {
// Creates a new array; marks old array for
// garbage collection.
items = new Object[MAX_LIST];
numItems = 0;
} // end removeAll
public void add(int index, Object item)
throws ListIndexOutOfBoundsException {
if (numItems >= MAX_LIST) {
throw new ListException("ListException on add");
} // end if
if (index >= 0 && index <= numItems) {
// make room for new element by shifting all items at
// positions >= index toward the end of the
// list (no shift if index == numItems)
for (int pos = numItems-1; pos >= index; pos--) {
items[pos+1] = items[pos];
} // end for
// insert new item
items[index] = item;
numItems++;
}
else { // index out of range
throw new ListIndexOutOfBoundsException(
"ListIndexOutOfBoundsException on add");
} // end if
} //end add
public Object get(int index)
throws ListIndexOutOfBoundsException {
if (index >= 0 && index < numItems) {
return items[index];
}
else { // index out of range
throw new ListIndexOutOfBoundsException(
"ListIndexOutOfBoundsException on get");
} // end if
} // end get
public void remove(int index)
throws ListIndexOutOfBoundsException {
if (index >= 0 && index < numItems) {
// delete item by shifting all items at
// positions > index toward the beginning of the list
// (no shift if index == size)
for (int pos = index+1; pos < size(); pos++) {
items[pos-1] = items[pos];
} // end for
numItems--;
}
else { // index out of range
throw new ListIndexOutOfBoundsException(
"ListIndexOutOfBoundsException on remove");
} // end if
} // end remove
private int translate(int position){
return position - 1;
} //end translate
} //end List ArrayBased
______________________________________
public class ListException extends RuntimeException
{
public ListException(String s)
{
super(s);
} // end constructor
} // end ListException
____________________________________
//hello
public class ListIndexOutOfBoundsException extends
IndexOutOfBoundsException
{
public ListIndexOutOfBoundsException(String s)
{
super(s);
} // end constructor
} // end ListIndexOutOfBoundsException
_______________________________________
//interface ListInterface for ADT List.
public interface ListInterface {
public boolean isEmpty();
public int size();
public void add(int index, Object item)
throws ListIndexOutOfBoundsException,
ListException;
public Object get(int index)
throws ListIndexOutOfBoundsException;
public void remove(int index)
throws ListIndexOutOfBoundsException;
public void removeAll();
}// end ListInterface
Attaching driver code which means Main method code
Driver.java
public class Driver {
public static void main(String[] args) {
// TODO Auto-generated method
stub
//Initialising an ADT list
object
ListArrayBased list = new
ListArrayBased();
//First Lets Start by calling the
isEmpty() method
System.out.println("Checking the
emptiness of list using isEmpty() method");
System.out.println("Is list empty :
" + list.isEmpty());
System.out.println();
System.out.println("Now adding one
element to list using add() method");
//Now add one element to the
list
try {
list.add(0,
1);
} catch
(ListIndexOutOfBoundsException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
System.out.println();
System.out.println("Checking the
emptiness of list using isEmpty() method");
//Again Lets Start by calling the
isEmpty() method
System.out.println("Is list empty :
" + list.isEmpty());
System.out.println();
System.out.println("Now retriving
data from list using get() method");
//Now lets get the element at the
index given
try {
System.out.println("Element at zeroth index is : " +
list.get(0));
} catch
(ListIndexOutOfBoundsException e1) {
// TODO
Auto-generated catch block
e1.printStackTrace();
}
System.out.println();
System.out.println("Now remove the
element at particular index using remove() method");
//Now remove one element from the
List based on index
try {
list.remove(0);
} catch
(ListIndexOutOfBoundsException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
System.out.println();
//Again Lets Start by calling the
isEmpty() method
System.out.println("Checking the
emptiness of list using isEmpty() method");
System.out.println("Is list empty :
" + list.isEmpty());
System.out.println();
System.out.println("Adding two
elements to list");
//Now add two elements to the
list
try {
list.add(0, 1);
list.add(1,2);
} catch (ListIndexOutOfBoundsException e)
{
// TODO Auto-generated catch
block
e.printStackTrace();
}
System.out.println();
System.out.println("Applying removeAll() method
to remove the two elements present in the list");
//Now remove all
elements using removeAll()
list.removeAll();
System.out.println();
//Again Lets Start by calling the isEmpty()
method
System.out.println("Checking the emptiness of
list using isEmpty() method");
System.out.println("Is list empty : " +
list.isEmpty());
System.out.println();
System.out.println("Now you have to some error. Because we are
trying to add element to list at invalid index.");
//Now try to add one element at
invalid index
try {
list.add(-1, 2);
} catch (ListIndexOutOfBoundsException e)
{
// TODO Auto-generated catch
block
e.printStackTrace();
}
}
}
Output is
Checking the emptiness of list using isEmpty() method
Is list empty : true
Now adding one element to list using add() method
Checking the emptiness of list using isEmpty() method
Is list empty : false
Now retriving data from list using get() method
Element at zeroth index is : 1
Now remove the element at particular index using remove() method
Checking the emptiness of list using isEmpty() method
Is list empty : true
Adding two elements to list
Applying removeAll() method to remove the two elements present in the list
Checking the emptiness of list using isEmpty() method
Is list empty : true
Now you have to some error. Because we are trying to add element
to list at invalid index.
july19.ListIndexOutOfBoundsException: ListIndexOutOfBoundsException
Occured on add
at
july19.ListArrayBased.add(ListArrayBased.java:46)
at july19.Driver.main(Driver.java:95)
Screenshot of class Diiagram of ADT List
If you left with any doubts, feel free to ask.
Complete an Array-Based implementation of the ADT List including a main method and show that the...
Chapter 4 describes the ADT Sorted List using an array implementation with a maximum of 25 items. The pseudocode for the ADT Sorted List Operations are provided on page 210. Use this information to create an ADT for handling a collection of Person objects, where each object will contain a Social Insurance Number (validate this), a first name, a last name, a gender and a data of birth. This implementation should prevent duplicate entries – that is, the Social Insurance...
Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the first item containing a given value from a doubly linked list. The header of the method is as follows: public boolean removeValue(T aValue) where T is the general type of the objects in the list and the methods returns true if such an item is found and deleted. Include testing of the method in a main method of the DoublyLList class. ------------------------------------------------------------------------------------- /** A...
I need help with the Implementation of an Ordered List (Template Files) public interface OrderedStructure { public abstract int size(); public abstract boolean add( Comparable obj ) throws IllegalArgumentException; public abstract Object get( int pos ) throws IndexOutOfBoundsException; public abstract void remove( int pos ) throws IndexOutOfBoundsException; public abstract void merge( OrderedList other ); } import java.util.NoSuchElementException; public class OrderedList implements OrderedStructure { // Implementation of the doubly linked nodes (nested-class) private static class Node { private Comparable value; private...
Write a Java program to work with a generic list ADT using a fixed size array, not ArrayList. Create the interface ListInterface with the following methods a) add(newEntry): Adds a new entry to the end of the list. b) add(newPosition, newEntry): Adds a new entry to the list at a given position. c) remove(givenPosition): Removes the entry at a given position from the list. d) clear( ): Removes all entries from the list . e) replace(givenPosition, newEntry): Replaces the entry...
Can you help with the merge method? This method should create and return an ArrayBagcontaining one occurrence of any item that is found in either the called object or the parameter other. For full credit, the resulting bag should not include any duplicates. Give the new ArrayBag a maximum size that is the sum of the two bag’s maximum sizes. import java.util.*; /** * An implementation of a bag data structure using an array. */ public class ArrayBag { /**...
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...
Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...
I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think about how to implement KWArrayList class. Please implement the following constructor and methods: public KWArrayList() public boolean add(E anEntry) public E get(int index) { public E set(int index, E newValue) public E remove(int index) private void reallocate() public int size() public int indexOf(Object item) Study the code for ArrayList implementation (enclosed in the folder) and work on the following exercise Provide a constructor...
Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> { /** * Append an item to the end of the list * * @param item – item to be appended */ public void append(E item); /** * Insert an item at a specified index position * * @param item – item to be...
Class SortedList { Public: SortedType(); int getLength(); //returns size of the list void putItem(Itemtype item); //inserts an item Itemtype getNextItem(); //returns the next item pointed by the index void deleteItem(Itemtype item) //deletes a specified item Private: int length; //length of the list Nodetype* listData //head of the list Nodetype* currentPos; //current pointer to the list } Class Itemtype { Public: Itemtype(); int getValue();// returns the value Private: int value; } struct Nodetype { Itemtype info; Nodetype* next; } Add a...