Question

JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...

JAVA:

Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java.
Need to complete: ReversePoem.java.

This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm.

1.   You are given a list of phrases each ending with a pound sign: ‘#’.
2.   Create a single String object from this list.
3.   Then, split the String of phrases into an array of phrases using the String split method.
4.   Display the poem by walking through the array and displaying each phrase one per line.
5.   And, at the same time, place each phrase on a queue using a MyQueue object using only the enqueue method.
6.   After all the phrases have been placed on the queue, transfer the phrases from the MyQueue object to a MyStack object using the dequeue and push methods.
7.   Then, use the pop method to remove them from the stack and, at the same time, display the phrases, one per line.
8.   The result is another poem with the phrases reversed.

ReversePoem.java:

public class ReversePoem
{
   // This programs has you display a pessimistic poem from a list of phrases
   // and then reverse the phrases to find another more optimistic poem.

   public static void main(String[] args) throws Exception
   {
       // Create a single String object from the 16 Strings below

       String set1 = "I am part of a lost generation#and I refuse to believe that#";
       String set2 = "I can change the world#I realize this may be a shock but#";
       String set3 = "'Happiness comes from within'#is a lie, and#";
       String set4 = "'Money will make me happy'#So in 30 years I will tell my children#";
       String set5 = "they are not the most important thing in my life#";
       String set6 = "My employer will know that#I have my priorities straight because#";
       String set7 = "work#is more important than#family#I tell you this#";
       String set8 = "Once upon a time#Families stayed together#";
       String set9 = "but this will not be true in my era#";
       String set10 = "This is a quick fix society#Experts tell me#";
       String set11 = "30 years from now, I will be celebrating the 10th anniversary of my divorce#";
       String set12 = "I do not concede that#I will live in a country of my own making#";
       String set13 = "In the future#Environmental destruction will be the norm#";
       String set14 = "No longer can it be said that#My peers and I care about this earth#";
       String set15 = "It will be evident that#My generation is apathetic and lethargic#";
       String set16 = "It is foolish to presume that#There is hope#";

       /* You are given a list of phrases in Strings; the phrases
           are separated by pound signs: '#':
           1. Create a single String object from this list.
           2. Then, split the String of phrases into an array of
              phrases using the String split method.
           3. Display a poem by walking through the array and
              displaying each phrase one per line.
           4. And, at the same time, place each phrase on a
              MyQueue object using only the enqueue method.
           5. After all the phrases have been placed on the queue,
              transfer the phrases from the MyQueue object to a
              MyStack object using the dequeue and push methods.
           6. Then, use the pop method to remove them from the
              stack and, at the same time,display the phrases,
              one per line.
      
       The result is another poem with the phrases reversed.*/

   }
}

MyList.java:

public interface MyList> extends java.lang.Iterable
{
    // Add a new element at the end of this list
    public void add (E e);

    // Add a new element at specified index in this list
    public void add (int index, E e);

    // Return true if this list contains the element
    public boolean contains (E e);

    // Return the element at the specified index
    public E get (int index);

    // Return the index of the first matching element
    // Return -1 if no match.
    public int indexOf (E e);

    // Return the index of the last matching element
    // Return -1 if no match.
    public int lastIndexOf (E e);

    // Return true if this list contains no elements
    public boolean isEmpty();

    // Clear the list
    public void clear();

    // Remove the first occurrence of the element
    // Shift any subsequent elements to the left.
    // Return true if the element is removed.
    public boolean remove (E e);

    // Remove the element at the specified position
    // Shift any subsequent elements to the left.
    // Return the element that was removed from the list.
    public E remove (int index);

    // Replace the element at the specified position
    // with the specified element and returns the new set.
    public Object set (int index, E e);

    // Return the number of elements in this list
    public int size();
}

MyAbstractList.java:

public abstract class MyAbstractList> implements MyList
{
    protected int size = 0; // The size of the list

    protected MyAbstractList()
    {
    } // Create a default list

    // Create a list from an array of objects
    protected MyAbstractList (E[] objects)
    {
        for (int i = 0; i < objects.length; i++)
        {
            add (objects[i]);
        }
    }

    // Add a new element at the end of this list
    @Override
    public void add (E e)
    {
        add (size, e);
    }

    // Return true if this list contains no elements
    @Override
    public boolean isEmpty()
    {
        return size == 0;
    }

    // Return the number of elements in this list
    @Override
    public int size()
    {
        return size;
    }

    // Remove the first occurrence of the element e
    // Shift any subsequent elements to the left.
    // Return true if the element is removed.
    @Override
    public boolean remove (E e)
    {
        if (indexOf (e) >= 0)
        {
            remove (indexOf(e));
            return true;
        }
        else
        {
            return false;
        }
    }
}

MyArrayList.java:

public class MyArrayList> extends MyAbstractList
{
   public static final int INITIAL_CAPACITY = 16;
   private E[] data = null;
   public int capacity;

   // Create a default list of 16 elements
   public MyArrayList()
   {
       data = (E[]) new Comparable[INITIAL_CAPACITY]; // array
       capacity = INITIAL_CAPACITY;
   }

   // Create a list of capacity elements
   public MyArrayList (int capacity)
   {
       data = (E[]) new Comparable[capacity]; // array
   }

   // Create a list from an array of objects
   public MyArrayList (E[] objects)
   {
       for (int i = 0; i < objects.length; i++)
       {
           add (objects[i]); // Warning: don’t use super(objects)!
       }
   }

   // Add a new element at the specified index
   @Override
   public void add (int index, E e)
   {
       // Doubles array, if not big enough
       ensureCapacity();

       // Move the elements to the right after the specified index
       for (int i = size - 1; i >= index; i--)
       {
           data[i + 1] = data[i];
       }

       // Insert new element to data[index] and increase size
       data[index] = e;  
       size++;       
   }

   // Create a new larger array, double the current size + 1
   private void ensureCapacity()
   {
       if (size >= data.length)
       {
           E[] newData = (E[]) (new Comparable[size * 2 + 1]);
           System.arraycopy (data, 0, newData, 0, size);
           data = newData;
       }
   }

   // Return true if this list contains the element
   @Override
   public boolean contains (E e)
   {
       for (int i = 0; i < size; i++)
       {
           if (e.equals(data[i]))
           {
               return true;
           }
       }
       return false;
   }

   // Return the element at the specified index
   @Override
   public E get (int index)
   {
       checkIndex (index);
       return data[index];
   }

   // Throws IndexOutOfBoundsException, if needed
   private void checkIndex (int index)
   {
       if (index < 0 || index >= size)
       {
           throw new IndexOutOfBoundsException
           ("Index: " + index + ", Size: " + size);
       }
   }

   // Return the index of the first matching element
   // Return -1 if no match.
   @Override
   public int indexOf(E e)
   {
       for (int i = 0; i < size; i++)
       {
           if (e.equals (data[i]))
           {
               return i;
           }
       }
       return -1;
   }

   // Return the index of the last matching element
   // Return -1 if no match.
   @Override
   public int lastIndexOf (E e)
   {
       for (int i = size - 1; i >= 0; i--)
       {
           if (e.equals (data[i]))
           {
               return i;
           }
       }
       return -1;
   }

   // Remove the element at the specified position
   // Shift any subsequent elements to the left.
   // Return the element that was removed from the list.
   @Override
   public E remove(int index)
   {
       checkIndex (index);
       E old = data[index];

       // Shift data to the left
       for (int j = index; j < size - 1; j++)
       {
           data[j] = data[j + 1];
       }
       data[size - 1] = null;       // This element is now null
       size--;                          // Decrement size
       return old;
   }

   // Replace the element at the specified position
   // with the specified element.
   @Override
   public E set (int index, E e)
   {
       checkIndex (index);
       E old = data[index];
       data[index] = e;
       return old;
   }

   // Return a string representation of the array
   @Override
   public String toString()
   {
       StringBuilder result = new StringBuilder("[");
       for (int i = 0; i < size; i++)
       {
           result.append (data[i]);
           if (i < size - 1)
           {
               result.append (", ");
           }
       }
       return result.toString() + "]";
   }

   // Trims the capacity of the array to the current size
   // If size == capacity, no need to trim
   public void trimToSize()
   {
       if (size != data.length)
       {
           E[] newData = (E[]) (new Comparable[size]);
           System.arraycopy (data, 0, newData, 0, size);
           data = newData;
       }
   }

   // Clear the array: create a new empty one
   @Override
   public void clear()
   {
       data = (E[]) new Comparable[INITIAL_CAPACITY];
       size = 0;
   }

   // Override iterator() defined in Iterable
   @Override
   public java.util.Iterator iterator()
   {
       return new ArrayListIterator();
   }

   // Private iterator class for myArrayList class
   private class ArrayListIterator implements java.util.Iterator
   {
       private int current = 0; // Current index

       // Return true when there are more elements past current
       @Override
       public boolean hasNext()
       {
           return(current < size);
       }

       // Return the element at current
       @Override
       public E next()
       {
           return data[current++];
       }

       // Remove the element at current
       @Override
       public void remove()
       {
           MyArrayList.this.remove(current);
       }
   }
}

MyLinkedLink.java:

public class MyLinkedList> extends MyAbstractList
implements Comparable>            
{
   private Node head, tail;       // head and tail pointers

   // Create a default list

   public MyLinkedList()
   {
   }

   // Need this for implementing Comparable
   public int compareTo (MyLinkedList e)
   {
       return(size() - e.size());
   }

   // Create a list from an array of objects

   public MyLinkedList (E[] objects)
   {
       super(objects);       // Passes the array up to abstract parent
   }

   // Return the head element in the list

   public E getFirst()
   {
       if (size == 0)
       {
           return null;
       }
       else
       {
           return head.element;
       }
   }

   // Return the last element in the list

   public E getLast()
   {
       if (size == 0)
       {
           return null;
       }

       else
       {
           return tail.element;
       }
   }

   // Add an element to the beginning of the list

   public void addFirst (E e)
   {
       // Create a new node for element e

       Node newNode = new Node(e);

       newNode.next = head;   // link the new node with the head
       head = newNode;           // head points to the new node
       size++;                   // Increase list size
       if (tail == null)          // new node is only node
       {
           tail = head;
       }
   }

   // Add an element to the end of the list

   public void addLast (E e)
   {
       // Create a new node for element e

       Node newNode = new Node(e);
       if (tail == null)
       {
           head = tail = newNode; // new node is only node
       }
       else
       {
           tail.next = newNode; // Link the new with the last node
           tail = tail.next;   // tail now points to the last node
       }
       size++;       // Increase size
   }

   // Remove the head node and
   // return the object from the removed node

   public E removeFirst()
   {
       if (size == 0)
       {
           return null;
       }
       else
       {
           Node temp = head;
           head = head.next;
           size--;
           if (head == null)
           {
               tail = null;
           }
           return temp.element;
       }
   }

   // Remove the last node and return the object from it

   public E removeLast()
   {
       if (size == 0)
       {
           return null;
       }

       else if (size == 1)
       {
           Node temp = head;
           head = tail = null;
           size = 0;
           return temp.element;
       }
       else
       {
           Node current = head;
           for (int i = 0; i < size - 2; i++)
           {
               current = current.next;
           }
           Node temp = tail;
           tail = current;
           tail.next = null;
           size--;
           return temp.element;
       }
   }

   // Remove the element at the specified position
   // Return the element that was removed from the list.

   @Override
   public E remove (int index)
   {
       if (index < 0 || index >= size)
       {
           return null;
       }

       else if (index == 0)
       {
           return removeFirst();
       }

       else if (index == size - 1)
       {
           return removeLast();
       }

       else
       {
           Node previous = head;
           for (int i = 1; i < index; i++)
           {
               previous = previous.next;
           }

           Node current = previous.next;
           previous.next = current.next;
           size--;
           return current.element;
       }
   }

   // Return a String representation of the linked list elements

   @Override
   public String toString()
   {
       StringBuilder result = new StringBuilder("[");
       Node current = head;
       for (int i = 0; i < size; i++)
       {
           result.append (current.element);
           current = current.next;
           if (current != null)
           {
               result.append (", "); // Separate elements with a comma
           }
           else
           {
               result.append ("]");       // Insert the ] in the string
           }
       }
       return result.toString();
   }

   // Clear the list

   @Override
   public void clear()
   {
       size = 0;
       head = tail = null;
   }

   @Override

   // Add a new element at specified index in this list

   public void add(int index, E e)
   {
       if (index == 0)
       {
           addFirst (e);
       }
       else if (index == size)
       {
           addLast (e);
       }
       else
       {
           checkIndex (index);

           // Create a new node for element e
           Node newNode = new Node(e);
           Node previous = head;
           for (int i = 1; i < index; i++)
           {
               previous = previous.next;
           }

           newNode.next = previous.next;
           previous.next = newNode;
           size++;       // Increase size
       }
   }

   // Return true if this list contains the element e

   @Override
   public boolean contains(E e)
   {
       Node current = head;
        for (int i = 0; i < size; i++)
        {
            if (current.element == e)
            {
                return true;
            }                
            current = current.next;
        }
        return false;
   }

   // Return the element at the specified index

   @Override
   public E get (int index)
   {
       if (index < 0 || index >= size)
           {
                return null;
           }
           else
           {
                Node current = head;
                for (int i = 0; i == index; i++)
                {
                    current = current.next;
                }
                return current.element;
           }
   }

   // Return the index of the first matching element
   // Return -1 if no match

   @Override
   public int indexOf (E e)
   {
       Node current = head;
            for (int i = 0; i < size; i++)
            {
                if (current.element.equals(e))
                {
                    return i;
                }
                current = current.next;
            }
            return -1;

   }

   // Return the index of the last matching element
   // Return -1 if no match.

   @Override
   public int lastIndexOf (E e)
   {
       int lastIndex = -1;
       Node current = head;
       for (int i = 0; i < size; i++)
       {
           if (e.equals (current.element))
           {
               lastIndex = i;
           }
           current = current.next;
       }
       return lastIndex;
   }

   // Replace the element at the specified position
   // with the specified element

   @Override
   public E set (int index, E e)
   {
       Node current = head;
       for (int i = 0; i < index; i++)
       {
           current = current.next;
       }
       E old = current.element;
       current.element = e;
       return old;
   }

   // Override iterator() defined in Iterable

   @Override
   public java.util.Iterator iterator()
   {
       return new LinkedListIterator();
   }

   // Throws IndexOutOfBoundsException, if needed

   private void checkIndex (int index)
   {
       if (index < 0 || index >= size)
       {
           throw new IndexOutOfBoundsException
           ("Index: " + index + ", Size: " + size);
       }
   }

   // The Node class

   private static class Node
   {
       E element;
       Node next;

       public Node (E element)
       {
           this.element = element;
       }
   }

   // Private iterator class for myArrayList class

   private class LinkedListIterator implements java.util.Iterator
   {
       private Node current = head;       // Current index

       @Override
       public boolean hasNext()
       {
           return(current != null);
       }

       @Override
       public E next()
       {
           E e = current.element;
           current = current.next;
           return e;
       }

       @Override
       public void remove()
       {
           MyLinkedList.this.remove (current.element);
       }
   }
}

MyStack.java:

public class MyStack> implements Comparable>
{
   private MyArrayList list = new MyArrayList();

   // Use this to find top of stack and to compare Stacks
   public int getSize()
   {
       return list.size();
   }

   // Look at top of stack, without removing it
   public E peek()
   {
       return list.get (getSize() - 1);
   }

   // Place a new element on the stack
   public void push (E e)
   {
       list.add (e);
   }

   // Remove the top element from the stack
   public E pop()
   {
       E e = list.get (getSize() - 1);
       list.remove (getSize() - 1);
       return e;
   }

   public boolean isEmpty()
   {
       return list.isEmpty();
   }

   public int compareTo (MyStack e)
   {
       return(getSize() - e.getSize());
   }

   public MyArrayList getList()
   {
       return list;
   }

   @Override
   public String toString()
   {
       return "Stack: " + list.toString();
   }
}

MyQueue.java:

public class MyQueue>
{
   private MyLinkedList list = new MyLinkedList();

   public void enqueue(E e)
   {
       list.addLast(e);
   }

   public E dequeue()
   {
       return list.removeFirst();
   }

   public int getSize()
   {
       return list.size();
   }

   @Override
   public String toString()
   {
       return "Queue: " + list.toString();
   }
}

THANK YOU!!

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
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