'Creating a program that outputs a poem's name and the associated lines

I'm having some issues with an assignment for my java class. As the title suggests I have been assigned a task that requires the use of inheritance in order to create a program that will output 4 different poem names along with their number of lines. An example output for this would be.

Poem: The Raven Lines: 84 | Poem: True Wit Lines: 2 | Poem: There was an Old Man with a Beard Lines: 5 | Poem: The Wren Lines: 3

I have been given 5 different files in order to achieve this output. Couplet.java, DemoPoem.java, Haiku.java, Limerick.java, and lastly Poem.java. The only file that has code at the current time is DemoPoem.java, which is provided below

import java.util.*;
public class DemoPoems
{
   public static void main(String[] args)
   {
      Poem poem1 = new Poem("The Raven");
      Couplet poem2 = new Couplet("True Wit");
      Limerick poem3 = new Limerick("There was an Old Man with a Beard");
      Haiku poem4 = new Haiku("The Wren");
      display(poem1);
      display(poem2);
      display(poem3);
      display(poem4);
   }

   public static void display(Poem p)
   {
      System.out.println("Poem: " + p.getTitle() +
         "   Lines: " + p.getLines());
   }
}  

The instructions given provide a bit of an insight into what is wanted from the program. "Create a class named Poem that contains the following fields: title - the name of the poem | lines - the number of lines in the poem. Include a constructor that requires values for both fields. Also include get methods to retrieve field values. Create three subclasses: Couplet, Limerick, and Haiku. These subclasses only require a title and lines field which is to be set to a constant value. The values of which will be the respective number of lines found in the form of poem, couplet has 2, limerick has 5, and haiku had 3.

The things that I have tried so far are by creating the 4 other classes that haven't been included and then extending Couplet.java, Haiku.java, and Limerick.java to Poem.java. I also put String title and int lines into Poem.java, the problem that I am having is trying to fill out the rest of the files in order to create the previously described program. So any help would be greatly appreciated. Thank you for your time.



Solution 1:[1]

In case, that you do not have the full text of the poems:

Step 1: Every class needs a toString(), which does nothing more then System.out.println("name="+ this.name + "\t" + "lines=" + this.lines)

Step 2: In your main(), you need to create your poems and then do poem1.toString(); poem2.toString(); poem3.toString();.

In case, you have the full poem text, do this additionally:

You need to count the linebreaks, e.g. \n, in your poemTextString and add 1 to it. And this is the amount of lines of your poem!

You are welcome. :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Weber