Question

In: Computer Science

Create a linked list of Poem objects using the Poem.java file provided. You may create Poem...

Create a linked list of Poem objects using the Poem.java file provided. You may create Poem objects by hard coding (minimum 4), reading from a file, or getting user input.

Print the list using ListIterator.

Here is Poem.Java:

public class Poem
{

   private String title;
   private String poet;

   /**
   * No arg constructor
   */
   public Poem()
   {
       title = "not set";
       poet = "not set";
   }

   /**
   * @param title
   * @param poet
   */
   public Poem(String title, String poet)
   {
       super();
       setTitle(title);
       setPoet(poet);
   }

   /**
   * @return the title
   */
   public String getTitle()
   {
       return title;
   }

   /**
   * @param title the title to set
   */
   public void setTitle(String title)
   {
       if (title.length() > 0)
       {
           this.title = title;
       } else
       {
           this.title = "not set";
       }
   }

   /**
   * @return the poet
   */
   public String getPoet()
   {
       return poet;
   }

   /**
   * @param poet the poet to set
   */
   public void setPoet(String poet)
   {
       if (poet.length() > 0)
       {
           this.poet = poet;

       } else
       {
           this.poet = "not set";
       }
   }

   @Override
   public String toString()
   {
       return "Poem [title=" + title + ", poet=" + poet + "]";
   }

}

Solutions

Expert Solution

//The code and output both have been provided in case of any doubts please do comment..

import java.util.LinkedList;
import java.util.ListIterator;

public class Poems {

    public static void main(String[] args) {
        //Initializing a Linked List of Poem Object
        LinkedList<Poem> poems = new LinkedList<Poem>();

        //Adding the various Poems to poems Linked List
        poems.add(new Poem("The Good Life", "Tracy K. Smith"));
        poems.add(new Poem("How Bright It Is", "Brian Turner"));
        poems.add(new Poem("Numbers", "Mary Cornish"));
        poems.add(new Poem("The Road Not Taken", "Robert Frost"));
        poems.add(new Poem("Daffodils", "William Wordsworth"));
        poems.add(new Poem("Sonnet 18", "William Shakespeare"));
        poems.add(new Poem("A Psalm Of Life", "Henry Wadsworth Longfellow"));

        //Declaration and initialization of list iterator to iterate over poems
        ListIterator<Poem> iterator = poems.listIterator();
        System.out.println("=========================");
        //It iterates until next is not null
        while (iterator.hasNext()){
            //Store current poem index in current Poem and iterate to next poem
            Poem currentPoem = iterator.next();
            System.out.println("The current poem is \"" + currentPoem.getTitle() + "\" and its author is \"" + currentPoem.getPoet() + "\"");
        }
        System.out.println("=========================");

    }


}


Related Solutions

Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file...
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file is also given. The given testfile listmain.cpp is given for demonstration of unsorted list functionality. The functions header file is also given. Complete the functions of the header file linked_list.h below. ========================================================= // listmain.cpp #include "Linked_List.h" int main(int argc, char **argv) {      float           f;      Linked_List *theList;      cout << "Simple List Demonstration\n";      cout << "(List implemented as an Array - Do...
1.List the four objects provided by the WSH to interact with the computer system using a...
1.List the four objects provided by the WSH to interact with the computer system using a scripting language: 2.What does the following instruction do? at 22:00 /every:monday dfrgui.exe 3. What does the following command line statement do? schtasks /create /? 4.What does the following command line statement do? schtasks /create /sc daily /tn t1 /tr chkdsk.exe /st 23:45:00 5.What does the following instruction do? schtasks /delete /tn task3 6. What does the following command prompt instruction do? move .\*.txt D:\files...
First, create a project that creates an ordered linked list class (use the code provided, make...
First, create a project that creates an ordered linked list class (use the code provided, make sure to update the insert function so that it maintains an ordered list). To this class, add a function that prints the list in REVERSE order. Making use of recursion is the easiest and best way to do this, but certainly not the only way. Submit a .zip of your entire project.
In python I want to create a function that takes in a linked list. Using recursion...
In python I want to create a function that takes in a linked list. Using recursion only, I want to check if the linked list is sorted. How do i do this?
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
Using Linked List, create a Java program that does the following without using LinkedList from the...
Using Linked List, create a Java program that does the following without using LinkedList from the Java Library. and please include methods for each function. Create a menu that contains the following options : 1. Add new node at the end of LL. ( as a METHOD ) 2. Add new node at the beginning of LL. ( as a METHOD ) 3. Delete a node from the end of LL. ( as a METHOD ) 4. Delete a node...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT