Question

In: Computer Science

Write a class to implement HeadTailListInterface. Instead of using an array, use a List object as...

Write a class to implement HeadTailListInterface. Instead of using an array, use a List object as your instance data variable. (List (Links to an external site.) from the Java standard library- not ListInterface!). Instantiate the List object to type ArrayList.

Inside the methods of this class, invoke methods on the List object to accomplish the task. Note: some methods might look very simple... this does not mean they are wrong!

There is one difference in how this class will work compared to the other: in this extra credit class, you do not have control over the capacity, so you should not print the capacity in display and the capacity does not have to be exactly doubled in the two add methods.

For full credit:

  • Pay close attention to what should happen in "failed" conditions as described by the HeadTailListInterface compared to List!
  • Make sure your ListHeadTailList behaves as described in HeadTailListInterface.
  • Use the methods of the List interface/ArrayList class when possible instead of re-writing the code yourself.

The class header and instance data will be:

public class ListHeadTailList<T> implements HeadTailListInterface<T>

List<T> list; // initialize to type ArrayList<T> in the ListHeadTailList constructor

------------------------------------------------------------------------------------------------------------------------------------------------

/**
* An interface for a list. Entries in a list have positions that begin with 0.
* Entries can only be removed or added to the beginning and end of the list.
* Entries can be accessed from any position.
*
* @author Jessica Masters
*/
public interface HeadTailListInterface<T> {
  
   /**
   * Adds a new entry to the beginning of the list.
   * Entries currently in the list are shifted down.
   * The list size is increased by 1.
   *
   * @param newEntry The object to be added as a new entry.
   */
   public void addFront(T newEntry);
  
   /**
   * Adds a new entry to the end of the list.
   * Entries currently in the list are unaffected.
   * The list size is increased by 1.
   *
   * @param newEntry The object to be added as a new entry.
   */
   public void addBack(T newEntry);

   /**
   * Removes an entry from the beginning of the list.
   * Entries currently in the list are shifted up.
   * The list size is decreased by 1.
   *
   * @return A reference to the removed entry or null if the list is empty.
   */
   public T removeFront();
  
   /**
   * Removes an entry from the end of the list.
   * Entries currently in the list are unaffected.
   * The list size is decreased by 1.
   *
   * @return A reference to the removed entry or null if the list is empty.
   */
   public T removeBack();

  
   /** Removes all entries from this list. */
   public void clear();

  
   /**
   * Retrieves the entry at a given position in this list.
   *
   * @param givenPosition An integer that indicates the position of the desired entry.
   * @return A reference to the indicated entry or null if the index is out of bounds.
   */
   public T getEntry(int givenPosition);
  
   /**
   * Displays the contents of the list to the console, in order.
   */
   public void display();
  
   /**
   * Determines the position in the list of a given entry.
   * If the entry appears more than once, the first index is returned.
   *
   * @param anEntry the object to search for in the list.
   * @return the first position the entry that was found or -1 if the object is not found.
   */
   public int indexOf(T anEntry);
  
   /**
   * Determines the position in the list of a given entry.
   * If the entry appears more than once, the last index is returned.
   *
   * @param anEntry the object to search for in the list.
   * @return the last position the entry that was found or -1 if the object is not found.
   */
   public int lastIndexOf(T anEntry);
  
   /**
   * Determines whether an entry is in the list.
   *
   * @param anEntry the object to search for in the list.
   * @return true if the list contains the entry, false otherwise
   */
   public boolean contains(T anEntry);


   /**
   * Gets the length of this list.
   *
   * @return The integer number of entries currently in the list.
   */
   public int size();

   /**
   * Checks whether this list is empty.
   *
   * @return True if the list is empty, or false if the list contains one or more elements.
   */
   public boolean isEmpty();
}

------------------------------------------------------------------------------------------------------------------------

********TESTING ISEMPTY AND EMPTY DISPLAY
Empty is true: true

Should display:
0 elements; capacity = 10
0 elements; capacity N/A        


********TESTING ADD TO FRONT
Should display:
1 elements; capacity = 10       [2]
1 elements; capacity N/A        [2]

Should display:
3 elements; capacity = 10       [3, 4, 2]
3 elements; capacity N/A        [3, 4, 2]

Empty is false: false


********TESTING CLEAR
Should display:
0 elements; capacity = 10
0 elements; capacity N/A        

********TESTING ADD TO BACK
Should display:
1 elements; capacity = 10       [7]
1 elements; capacity N/A        [7]

Should display:
3 elements; capacity = 10       [7, 10, 5]
3 elements; capacity N/A        [7, 10, 5]


********TESTING CONTAINS
Contains 5 true:  true
Contains 7 true:  true
Contains 4 false: false


********TESTING ADD WITH EXPANSION
Should display:
32 elements; capacity = 40      [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
32 elements; capacity N/A       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]


********TESTING INDEX OF
Index of 0  is  0: 0
Index of 31 is 31: 31
Index of -5 is -1: -1
Index of 32 is -1: -1
Index of 3  is  0: 0
Index of 5  is  6: 6


********TESTING LAST INDEX OF
Last index of 0  is  1:  1
Last index of 31 is 32: 32
Last index of -5 is -1: -1
Last index of 35 is -1: -1
Last index of 3  is  4:  4
Last index of 5  is  33: 33


********TESTING SIZE
Size is 34: 34


********TESTING GET ENTRY
Element in position 15 is 14: 14
Element in position  0 is  3: 3
Element in position 39 is null: null
Element in position -1 is null: null


********TESTING REMOVES
Remove front element 3: 3
Remove back element  5 :5
Remove front element 0: 0
Remove back element 31: 31

Should display:
30 elements; capacity = 40      [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
30 elements; capacity N/A       [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]

Remove element null: null
Remove element null: null

Remove element 1: 1
Should display:
0 elements; capacity = 40
0 elements; capacity N/A        

Remove element 1: 1
Should display:
0 elements; capacity = 40
0 elements; capacity N/A        

Remove element 1: 1
Should display:
0 elements; capacity = 40
0 elements; capacity N/A        

Remove element 1: 1
Should display:
0 elements; capacity = 40
0 elements; capacity N/A        



********TESTING MIX OF ADDS AND REMOVES
Should display:
7 elements; capacity = 40       [5, 4, 3, 2, 3, 8, 9]
7 elements; capacity N/A        [5, 4, 3, 2, 3, 8, 9]

Should display:
5 elements; capacity = 40       [4, 3, 2, 3, 8]
5 elements; capacity N/A        [4, 3, 2, 3, 8]

********TESTING WITH STRINGS
Should display:
5 elements; capacity = 5        [You, did, it!, Nice, job!]
5 elements; capacity = 5        [You, did, it!, Nice, job!]

Contains "Nice" is true: true
Contains "You"  is true: true
Contains "you"  is false: false

Index of "it!" is 2: 2
Last index of "it!" is 2: 2

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Since you have not attached the test program, I couldn’t check if the program gives the expected output. But I think everything should work. If you encounter any troubles, share your test code.

// ListHeadTailList.java

import java.util.ArrayList;

import java.util.List;

public class ListHeadTailList<T> implements HeadTailListInterface<T> {

      List<T> list;

      // constructor initializing empty list

      public ListHeadTailList() {

            list = new ArrayList<T>();

      }

      @Override

      public void addFront(T newEntry) {

            // adding to index 0

            list.add(0, newEntry);

      }

      @Override

      public void addBack(T newEntry) {

            // adding to the end

            list.add(newEntry);

      }

      @Override

      public T removeFront() {

            if (isEmpty()) {

                  // empty

                  return null;

            }

            // removing and returning element at index 0

            return list.remove(0);

      }

      @Override

      public T removeBack() {

            if (isEmpty()) {

                  return null;

            }

            // removing and returning last element

            return list.remove(list.size() - 1);

      }

      @Override

      public void clear() {

            // clearing all elements

            list.clear();

      }

      @Override

      public T getEntry(int givenPosition) {

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

                  // invalid position

                  return null;

            }

            // returning element at given position

            return list.get(givenPosition);

      }

      @Override

      public void display() {

            // simply printing the list

            System.out.println(list);

      }

      @Override

      public int indexOf(T anEntry) {

            // returning the first index of anEntry, or -1 if not found

            return list.indexOf(anEntry);

      }

      @Override

      public int lastIndexOf(T anEntry) {

            // returning the last index of anEntry, or -1 if not found

            return list.lastIndexOf(anEntry);

      }

      @Override

      public boolean contains(T anEntry) {

            //returns true if list contains anEntry, else false

            return list.contains(anEntry);

      }

      @Override

      public int size() {

            return list.size();

      }

      @Override

      public boolean isEmpty() {

            return list.isEmpty();

      }

      @Override

      public String toString() {

            //returning string representation of list

            return list.toString();

      }

}


Related Solutions

Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
Java the goal is to create a list class that uses an array to implement the...
Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you. import java.util.*; /** * Interface for an Iterable, Indexed, Unsorted List ADT....
Implement a Priority Queue (PQ) using an UNSORTED LIST. Use an array size of 10 elements....
Implement a Priority Queue (PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element, add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill in...
Implement a stack in C++ using an array, not an array list. Make your stack size...
Implement a stack in C++ using an array, not an array list. Make your stack size 5 when you test it, but do not hardcode this! You should be able to change the size for testing purposes with the change of one variable. DO NOT use Stack class defined in C++ Implement the following methods in your stack class. stack() creates an empty stacks, stack s is new and empty. push(item) adds a new item to the stack s, stacks...
Implement a program as an object using a class (abstract data type) in C++ that does...
Implement a program as an object using a class (abstract data type) in C++ that does the following: 1) reads the firstName, lastName and 3 test scores of at least five students. 2) calculate student test score totals, average, letter grade for each student. 3) Display the results in a table format showing firstName, lastName, test1, test2, test3, total, average, letterGrade, of all the students. 3 files .h, .cpp, main.cpp create an object that can hold records. must get records...
           Homework: Polynomial Using Array Description: Implement a polynomial class (1) Name your class...
           Homework: Polynomial Using Array Description: Implement a polynomial class (1) Name your class Polynomial (2) Use array of doubles to store the coefficients so that the coefficient for x^k is stored in the location [k] of the array. (3) define the following methods: a. public Polynomial()    POSTCONDITION: Creates a polynomial represents 0 b. public Polynomial(double a0)    POSTCONDITION: Creates a polynomial has a single x^0 term with coefficient a0 c. public Polynomial(Polynomial p)    POSTCONDITION: Creates...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT