Question

Complete an Array-Based implementation of the ADT List including a main method and show that the...

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

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

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.

Add a comment
Know the answer?
Add Answer to:
Complete an Array-Based implementation of the ADT List including a main method and show that the...
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
  • /* * Purpose: Test an implementation of an array-based list ADT. * To demonstrate the use...

    /* * Purpose: Test an implementation of an array-based list ADT. * To demonstrate the use of "ListArrayBased" the project driver * class will populate the list, then invoke various operations * of "ListArrayBased". */ import java.util.Scanner; public class ListDriver {    /*    * main    *    * An array based list is populated with data that is stored    * in a string array. User input is retrieved from that std in.    * A text based...

  • Chapter 4 describes the ADT Sorted List using an array implementation with a maximum of 25...

    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...

  • Please write in Java Recall that the ADT list class methods are;  void List() ...

    Please write in Java Recall that the ADT list class methods are;  void List()  bool isEmpty()  int size()  void add(int item, int pos)//inserts item at specified position (first postion is 1)  void remove(int index)//removes item from specified position  void removeAll()  int indexOf(int item)//returns the index of item  int itemAt(int index)//returns the item in position specified by index Implementation of LIST ADT operations and details are hidden. Only the methods listed above are...

  • Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the...

    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 Ordered...

    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...

  • Currently working on a Java Assignment. I have written most codes for swap, reverse and insert....

    Currently working on a Java Assignment. I have written most codes for swap, reverse and insert. Just need a. itemCount receives a value and returns a count of the number of times this item is found in the list. c. sublist receives two indexes and returns an ArrayList of node values from the first index to the second index, provided the indexes are valid. d. select receives a variable number of indexes, and returns an ArrayList of node values corresponding...

  • USE JAVA: Given the Interface Code and the Interface Implementation Code; Write Junit Tests to test...

    USE JAVA: Given the Interface Code and the Interface Implementation Code; Write Junit Tests to test all fuctionality. ------------- Interface code: public interface CustomList { /** * This method should add a new item into the CustomList and should * return true if it was successfully able to insert an item. * @param item the item to be added to the CustomList * @return true if item was successfully added, false if the item was not successfully added (note: it...

  • Write a Java program to work with a generic list ADT using a fixed size array,...

    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...

    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)...

    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...

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