Question

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 should always be able to add an item to the list)

     */

    boolean add (T item);

   
    /**

     * This method should add a new item into the <code>CustomList</code> at the

     * specified index (thus shuffling the other items to the right). If the index doesn't

     * yet exist, then you should throw an <code>IndexOutOfBoundsException</code>.

     * @param index the spot in the zero-based array where you'd like to insert your

     *        new item

     * @param item the item that will be inserted into the <code>CustomList</code>

     * @return <code>true</code> when the item is added

     * @throws IndexOutOfBoundsException

     */

    boolean add (int index, T item) throws IndexOutOfBoundsException;

   
    /**

     * This method should return the size of the <code>CustomList</code>

     * based on the number of actual elements stored inside of the <code>CustomList</code>

     * @return an <code>int</code> representing the number of elements stored in the <code>CustomList</code>

     */

    int getSize();

   
    /**

     * This method will return the actual element from the <code>CustomList</code> based on the

     * index that is passed in.

     * @param index represents the position in the backing <code>Object</code> array that we want to access

     * @return The element that is stored inside of the <code>CustomList</code> at the given index

     * @throws IndexOutOfBoundsException

     */

    T get(int index) throws IndexOutOfBoundsException;

   
    /**

     * This method should remove an item from the <code>CustomList</code> at the

     * specified index. This will NOT leave an empty <code>null</code> where the item

     * was removed, instead all other items to the right will be shuffled to the left.

     * @param index the index of the item to remove

     * @return the actual item that was removed from the list

     * @throws IndexOutOfBoundsException

     */

    T remove(int index) throws IndexOutOfBoundsException;

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

here is the code ---

CustomList.java

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 should always be able to add an item to the list)

*/

boolean add (T item);


/**

* This method should add a new item into the <code>CustomList</code> at the

* specified index (thus shuffling the other items to the right). If the index doesn't

* yet exist, then you should throw an <code>IndexOutOfBoundsException</code>.

* @param index the spot in the zero-based array where you'd like to insert your

* new item

* @param item the item that will be inserted into the <code>CustomList</code>

* @return <code>true</code> when the item is added

* @throws IndexOutOfBoundsException

*/

boolean add (int index, T item) throws IndexOutOfBoundsException;


/**

* This method should return the size of the <code>CustomList</code>

* based on the number of actual elements stored inside of the <code>CustomList</code>

* @return an <code>int</code> representing the number of elements stored in the <code>CustomList</code>

*/

int getSize();


/**

* This method will return the actual element from the <code>CustomList</code> based on the

* index that is passed in.

* @param index represents the position in the backing <code>Object</code> array that we want to access

* @return The element that is stored inside of the <code>CustomList</code> at the given index

* @throws IndexOutOfBoundsException

*/

T get(int index) throws IndexOutOfBoundsException;


/**

* This method should remove an item from the <code>CustomList</code> at the

* specified index. This will NOT leave an empty <code>null</code> where the item

* was removed, instead all other items to the right will be shuffled to the left.

* @param index the index of the item to remove

* @return the actual item that was removed from the list

* @throws IndexOutOfBoundsException

*/

T remove(int index) throws IndexOutOfBoundsException;

}

interfaceclass.java----

import java.util.*;
public class interfaceclass implements CustomList<String>
{
   /*
   the CustomList is the interface implements into class interfaceclass
now because the interface is public ,
then every method from the interface implemented here should be public
  
  
   */
ArrayList arr=new ArrayList(); //here i am using arraylist to perform the task

public boolean add(String item)
{
   // the add(String item) take input as string and return boolean value as true when the value add in the arraylist
   arr.add(item);
return true;  
}

public boolean add(int index, String item)
{
  
   // the add(int index,String item) take input as string and index as int and return boolean value as true when the value add in the arraylist
   arr.add(index,item);
   return true;
}
public int getSize(){
   // the getSize() return the size of the arraylist
int size =arr.size();
return size;
}

public String get(int index)
{
   // the get(int index) return the item which is stored in the index in the arraylist
   String value=(arr.get(index)).toString();
   return value;
}

public String remove(int index)
{
   // the remove(int index) return the item which is remove from the index in the arraylist
   String removeitem=(arr.remove(index)).toString();
   return removeitem;
}

public static void main(String args[])
{
   interfaceclass i=new interfaceclass();
   // creating reference of class interfaceclass
   System.out.println("the value added : "+i.add("aditya")); // add string aditya and it will return true or false , and print also
   System.out.println("the value added : "+i.add("xyz"));// add string xyz and it will return true or false , and print also
   System.out.println("the value added : "+i.add("abc"));// add string abc and it will return true or false , and print also
   System.out.println("the value added : "+i.add(2,"kori g"));// add string kori at index 2 and it will return true or false , and print also
   int sizeoflist=i.getSize();
   System.out.println("the size of list : "+sizeoflist); // it will return the size of arraylist
   String valuelist=i.get(1);
   System.out.println("the value : "+valuelist); // return the value at index 1

   String removeitem=i.remove(1); // remove item from the index 1
   System.out.println("the remove item from list : "+removeitem);


}

}

here are snapshots ---

here all done ! the method also throw exception if found runtime error ... hope it will help you !

the best approach to do this task to create package and then store the files into it..the interface provide the code reusable.

thank you !

Add a comment
Know the answer?
Add Answer to:
Given the Interface Code write a java class that implements this interface and show the working...
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
  • Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...

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

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

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • In a file named LLBag.java, write a class called LLBag that implements the Bag interface using...

    In a file named LLBag.java, write a class called LLBag that implements the Bag interface using a linked list instead of an array. You may use a linked list with or without a dummy head node. Bag interface code: /* * Bag.java * * Computer Science 112, Boston University */ /* * An interface for a Bag ADT. */ public interface Bag { /*    * adds the specified item to the Bag. Returns true on success    * and...

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

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDE Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS 2) ArrayList ADT that uses a linked list internally (call it LArrayList) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • Java Write an intersection method for the ResizableArrayBag class. The intersection of two bags is the...

    Java Write an intersection method for the ResizableArrayBag class. The intersection of two bags is the overlapping content of the bags. Intersections are explained in more detail in Chapter 1, #6. An intersecion might contain duplicates. The method should not alter either bag. The current bag and the bag sent in as a parameter should be the same when the method ends. The method header is: public BagInterface<T> intersection(ResizableArrayBag <T> anotherBag) Example: bag1 contains (1, 2, 2, 3) bag2 contains...

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDES Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

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

  • Plz help me with the code. And here are the requirement. Thanks!! You are required to...

    Plz help me with the code. And here are the requirement. Thanks!! You are required to design and implement a circular list using provided class and interface. Please filling the blank in CircularList.java. This circular list has a tail node which points to the end of the list and a number indicating how many elements in the list. And fill out the blank of the code below. public class CircularList<T> implements ListInterface<T> { protected CLNode<T> tail; // tail node that...

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