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!!
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.
JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...
Hi! Can someone can convert this java code to c++. ASAP thanks I will thumbs up Here's the code: package lists; import bookdata.*; public class DoublyLinkedList { int size; //Variable que define el tamano de la lista. Node head, tail; //Nodos que definen el Head y Tail en la lista. //Constructor public DoublyLinkedList(){ this.head = null; this.tail = null; this.size = 0; } //Insert a new book in alphabetic order. public void insert(Book nb){ //Creamos uno nuevo nodo con el...
Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...
Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> { private static class Node<T> { public Node<T> prev, next;...
write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...
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 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...
Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...
I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...
PYTHON -------------------------------------------------------- class LinkedList: def __init__(self): self.__head = None self.__tail = None self.__size = 0 # Return the head element in the list def getFirst(self): if self.__size == 0: return None else: return self.__head.element # Return the last element in the list def getLast(self): if self.__size == 0: return None else: return self.__tail.element # Add an element to the beginning of the list def addFirst(self, e): newNode = Node(e) # Create a new node newNode.next = self.__head # link...
Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...