Question

Create a class named Poem that contains the following fields: title - the name of the...

Create a class named Poem that contains the following fields:

  • title - the name of the poem (of type String)
  • lines - the number of lines in the poem (of type int)

Include a constructor that requires values for both fields. Also include get methods to retrieve field values. Create three subclasses: Couplet, Limerick, and Haiku. The constructor for each subclass requires only a title; the lines field is set using a constant value. A couplet has two lines, a limerick has five lines, and a haiku has three lines.

Couplet.java

public class Couplet
{
// Define the Couplet class here
}

DemoPoems.java

import java.util.*;
public class DemoPoems
{
public static void main(String[] args)
{
Poem poem1 = new Poem("The Raven", 84);
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());
}
}


Haiku.java

public class Haiku
{
// Define the Haiku class here
}

Limerick.java

public class Limerick
{
// Define the Limerick class here
}

Poem.java

public class Poem
{
// Define the Poem class here
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class Poem {
    /*
    title - the name of the poem (of type String)
lines - the number of lines in the poem (of type int)
    * */
    private String title;
    private int lines;
    /*a constructor that requires values for both fields.*/
    public Poem(String title,int lines)
    {
        this.title= title;
        this.lines= lines;
    }
    /*get methods to retrieve field values.*/

    public String getTitle() {
        return title;
    }

    public int getLines() {
        return lines;
    }
}

==========================================

public class Couplet extends Poem{
    /*A couplet has two lines*/
    private static final int LINES=2;
    public Couplet(String title)
    {
        super(title,LINES);
    }
}

========================================

public class Limerick extends Poem{
    /*a limerick has five lines*/
    private static final int LINES=5;
    public Limerick(String title)
    {
        super(title,LINES);
    }
}

=====================================

public class Haiku extends Poem {
    /* a haiku has three lines.*/
    private static final int LINES=3;
    public Haiku(String title)
    {
        super(title,LINES);
    }
}

========================================

import java.util.*;
public class DemoPoems
{
    public static void main(String[] args)
    {
        Poem poem1 = new Poem("The Raven", 84);
        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());
    }
}

===============================================

//output

//if you need any help regarding this solution........... please leave a comment ............ thanks

Add a comment
Know the answer?
Add Answer to:
Create a class named Poem that contains the following fields: title - the name of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a...

    Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races(of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. DemoHorses.java public class DemoHorses { public static void main(String args[])...

  • (To be written in Java code) Create a class named CollegeCourse that includes the following data...

    (To be written in Java code) Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the department (for example, ENG) id (int) - the course number (for example, 101) credits (double) - the credits (for example, 3) price (double) - the fee for the course (for example, $360). All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display()...

  • Create an abstract Student class for Parker University. The class contains fields for student ID number,...

    Create an abstract Student class for Parker University. The class contains fields for student ID number, last name, and annual tuition. Include a constructor that requires parameters for the ID number and name. Include get and set methods for each field; the setTuition() method is abstract. Create three Student subclasses named UndergraduateStudent, GraduateStudent, and StudentAtLarge, each with a unique setTuition() method. Tuition for an UndergraduateStudent is $4,000 per semester, tuition for a GraduateStudent is $6,000 per semester, and tuition for...

  • Create a base class named Book. Data fields include title and author; functions include those that...

    Create a base class named Book. Data fields include title and author; functions include those that can set and display the fields. Derive two classes from the Book class: Fiction, which also contains a numeric grade reading level, and NonFiction, which contains a variable to hold the number of pages. The functions that set and display data field values for the subclasses should call the appropriate parent class functions to set and display the common fields, and include specific code...

  • Create a class named Book that contains data fields for the title and number of pages....

    Create a class named Book that contains data fields for the title and number of pages. Include get and set methods for these fields. Next, create a subclass named Textbook, which contains an additional field that holds a grade level for the Textbook and additional methods to get and set the grade level field. Write an application that demonstrates using objects of each class. Save the files as Book.java, Textbook.java, and DemoBook.java.

  • Create a class named Circle with fields named radius, diameter, and area. Include a constructor that...

    Create a class named Circle with fields named radius, diameter, and area. Include a constructor that sets the radius to 1 and calculates the other two values. Also include methods named setRadius() and getRadius(). The setRadius() method not only sets the radius, but it also calculates the other two values. (The diameter of a circle is twice the radius, and the area of a circle is pi multiplied by the square of the radius. Use the Math class PI constant...

  • A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS...

    A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS 210), credit hours (for example, 3), and a letter grade (for example, A). Include get and set methods for each field. Create a Student class containing an ID number and an array of five CollegeCourse objects. Create a get and set method for the Student ID number. Also create a get method that returns one of the Student’s CollegeCourses; the method takes an integer...

  • A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS...

    A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS 210), credit hours (for example, 3), and a letter grade (for example, A). Include get and set methods for each field. Create a Student class containing an ID number and an array of five CollegeCourse objects. Create a get and set method for the Student ID number. Also create a get method that returns one of the Student’s CollegeCourses; the method takes an integer...

  • a. Create a FitnessTracker class that includes data fields for a fitness activity, the number of...

    a. Create a FitnessTracker class that includes data fields for a fitness activity, the number of minutes spent participating, and the date. The class includes methods to get each field. In addition, create a default constructor that automatically sets the activity to "running", the minutes to 0, and the date to January 1 of the current year. Create an application that demonstrates each method works correctly. b. Create an additional overloaded constructor for the FitnessTracker class you created in Exercise...

  • A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS 21...

    A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS 210), credit hours (for example, 3), and a letter grade (for example, A). Include get and set methods for each field. Create a Student class containing an ID number and an array of five CollegeCourse objects. Create a get and set method for the Student ID number. Also create a get method that returns one of the Student’s CollegeCourses; the method takes an integer...

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