Sentence.java (interface) /**  * This is a Interface for Sentence.  */ public interface Sentence {   /**    * Returns number of Words.    *    * @return - an int.    */   int getNumberOfWords();   /**    * Returns Longest Word.    *    * @return - a String.    */   String longestWord();   /**    * Returns a String that represents a sentence.    *    * @return - a String.    */   String toString();   /**    * clones a sentence.    *    * @return - a Sentence.    */   Sentence clone();   /**    * merges a sentence with another.    *    * @return - a Sentence.    */   Sentence merge(Sentence secondSentenc

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 10PE
icon
Related questions
Question

For all methods in each class (labeled), how do I write the JUnit 4 test(s) to test all methods? Full code is shown below:

 

PunctuationNode.java (class)

public class PunctuationNode implements Sentence {
  private String punctuation;
  private Sentence rest;

  /**
   * This is a constructor for class PunctuationNode.
   *
   * @param punctuation - a String.
   * @param rest        - a String.
   */
  public PunctuationNode(String punctuation, Sentence rest) {
    this.punctuation = punctuation;
    this.rest = rest;
  }

  /**
   * A method to get number of words.
   *
   * @return - a integer.
   */
  @Override
  public int getNumberOfWords() {
    return this.rest.getNumberOfWords();
  }

  /**
   * A method to get longest word.
   *
   * @return - a integer.
   */
  @Override
  public String longestWord() {
    return this.rest.longestWord();
  }

  /**
   * Returns a String that represents a sentence.
   *
   * @return - a Sentence.
   */
  @Override
  public String toString() {
    return this.punctuation + this.rest;
  }

  /**
   * clones a sentence.
   *
   * @return - a Sentence.
   */
  @Override
  public Sentence clone() {
    return new PunctuationNode(this.punctuation, this.rest.clone());
  }

  /**
   * merges a sentence with another.
   *
   * @return - a Sentence.
   */
  @Override
  public Sentence merge(Sentence other) {
    return new PunctuationNode(this.punctuation, this.rest.merge(other));
  }
}

 

WordNode.java (class)

/**
 * This is a class for Sentence.
 */
public class WordNode implements Sentence {
  private String word;
  private Sentence rest;

  /**
   * This is a constructor for the Sentence Class.
   *
   * @param word - string.
   * @param rest - a Sentence.
   */
  public WordNode(String word, Sentence rest) {
    this.word = word;
    this.rest = rest;
  }

  /**
   * Does: Calculates the total number of words in a Sentence.
   *
   * @return - The total number of words from "current" + the rest of the words.
   */
  @Override
  public int getNumberOfWords() {
    return 1 + this.rest.getNumberOfWords();
  }

  /**
   * Does: Determines the longest word in the sentence.
   *
   * @return - A string of the longest word.
   */
  @Override
  public String longestWord() {
    if (word.length() >= this.rest.longestWord().length()) {
      return this.word;
    }
    return this.rest.longestWord();
  }

  /**
   * Does: Adds a period to the end of the string if there none.
   *
   * @return - a String.
   */
  @Override
  public String toString() {
    if (this.rest.toString().equals("")) {
      // If we are at the end of the word nodes
      // and check for last node is Empty Node
      return this.word + ".";
    }
    if (rest instanceof PunctuationNode) {
      return this.word + this.rest.toString();
    }

    return this.word + " " + this.rest.toString();
  }

  /**
   * Does: clones a sentence.
   *
   * @return - a String.
   */
  @Override
  public Sentence clone() {
    return new WordNode(this.word, this.rest.clone());
  }

  /**
   * Does: merges a sentence.
   *
   * @return - a String.
   */
  @Override
  public Sentence merge(Sentence secondSentence) {
    return new WordNode(this.word, this.rest.merge(secondSentence));
  }
}

 

EmptyNode.java (class)

/**
 * This a class for EmptyNode.
 */
public class EmptyNode implements Sentence {

  /**
   * Returns 0.
   *
   * @return - 0 words at the start.
   */
  @Override
  public int getNumberOfWords() {
    return 0;
  }

  /**
   * Returns empty string.
   *
   * @return - 0 words at the start.
   */
  @Override
  public String longestWord() {
    return "";
  }

  /**
   * Returns empty string.
   *
   * @return - 0 words at the start.
   */
  @Override
  public String toString() {
    return "";
  }

  /**
   * Returns sentence to merge.
   *
   * @return - other sentence.
   */
  @Override
  public Sentence merge(Sentence other) {
    return other.clone();
  }

  /**
   * Returns empty Node.
   *
   * @return - 0 words at start.
   */
  @Override
  public Sentence clone() {
    return new EmptyNode();
  }

}

 

Sentence.java (interface)

/**
 * This is a Interface for Sentence.
 */
public interface Sentence {

  /**
   * Returns number of Words.
   *
   * @return - an int.
   */
  int getNumberOfWords();

  /**
   * Returns Longest Word.
   *
   * @return - a String.
   */
  String longestWord();

  /**
   * Returns a String that represents a sentence.
   *
   * @return - a String.
   */
  String toString();

  /**
   * clones a sentence.
   *
   * @return - a Sentence.
   */
  Sentence clone();

  /**
   * merges a sentence with another.
   *
   * @return - a Sentence.
   */
  Sentence merge(Sentence secondSentence);
}

Goals: To practice representing data using protocols (aka: a set of interfaces in Java) and lists,
define operations on it and also to work with interfaces and abstract classes to refactor code.
Part of the goal of this assignment is to give you practice with understanding and implementing
a linked list like the examples covered in the Module 4 Videos. Therefore, you may NOT use any
Java Collection classes provided by the Java libraries. Also, limit yourself to the recursive data
structure (as in the videos you watched this week) and do NOT use looping and iteration via
for/while constructs.
1.1
Introduction
A sentence in English is made of several words in a particular sequence. You must represent
such a sentence as a list of words. Much like the list example we saw in class, this one is made of
nodes (represented by a Sentence interface). There is one word per node, called wordNode. The
sentence may also contain zero or more punctuation marks, which are represented by a
PunctuationNode. The end of the sentence is denoted by a special empty node, called
EmptyNode.
Define the following operations for such a list of words:
A method getNumberOfWords that computes and returns the number of words in a
sentence. The punctuation does not count as a word. This method should answer/return an
int.
A method longestWord that determines and returns the longest word in a sentence. The
word returned is just the word, and should not begin or end with punctuation. If the
sentence contains no words, longestWord should return an empty string.
This method should answer/return a String
• A method toString that will convert the sentence into one string. There must be a space
between every two words. A punctuation mark should have no space between it and
whatever precedes it. There is no space between the last word and the end of this sentence.
If there is no punctuation mark at the end of the sentence, this string should end with a
period (this method should NOT add a period to the original sentence object, but the String
answered by this method should properly have the period)
This method should answer/return a String
A method clone () that returns a duplicate of a given sentence. A duplicate is a list that has
the same words and punctuation in the same sequence, but is independent of the original
list.
This method should answer/return a Sentence
A method merge (Sentence other) that will merge two sentences into a single sentence.
The merged list should preserve all the punctuation. The merged list should be returned by
this method, and the original lists should be unchanged.
This method should answer/return a Sentence
1.2
What to do
1. Design the interfaces and classes that you need for this purpose. We are purposely NOT
giving you the interface code as a "starter pack". Develop your interface for Sentence by
extracting the concepts from the specification above in Section 1.1
2. In a JUnit test class, create examples of sentences and write tests for each operation.
3. Design the fields and methods for your classes, and verify that your tests pass on them.
4. Check if you can abstract any common parts of your design/code. If so, take a "bottom up"
approach and push these common features into an appropriate superclass
Transcribed Image Text:Goals: To practice representing data using protocols (aka: a set of interfaces in Java) and lists, define operations on it and also to work with interfaces and abstract classes to refactor code. Part of the goal of this assignment is to give you practice with understanding and implementing a linked list like the examples covered in the Module 4 Videos. Therefore, you may NOT use any Java Collection classes provided by the Java libraries. Also, limit yourself to the recursive data structure (as in the videos you watched this week) and do NOT use looping and iteration via for/while constructs. 1.1 Introduction A sentence in English is made of several words in a particular sequence. You must represent such a sentence as a list of words. Much like the list example we saw in class, this one is made of nodes (represented by a Sentence interface). There is one word per node, called wordNode. The sentence may also contain zero or more punctuation marks, which are represented by a PunctuationNode. The end of the sentence is denoted by a special empty node, called EmptyNode. Define the following operations for such a list of words: A method getNumberOfWords that computes and returns the number of words in a sentence. The punctuation does not count as a word. This method should answer/return an int. A method longestWord that determines and returns the longest word in a sentence. The word returned is just the word, and should not begin or end with punctuation. If the sentence contains no words, longestWord should return an empty string. This method should answer/return a String • A method toString that will convert the sentence into one string. There must be a space between every two words. A punctuation mark should have no space between it and whatever precedes it. There is no space between the last word and the end of this sentence. If there is no punctuation mark at the end of the sentence, this string should end with a period (this method should NOT add a period to the original sentence object, but the String answered by this method should properly have the period) This method should answer/return a String A method clone () that returns a duplicate of a given sentence. A duplicate is a list that has the same words and punctuation in the same sequence, but is independent of the original list. This method should answer/return a Sentence A method merge (Sentence other) that will merge two sentences into a single sentence. The merged list should preserve all the punctuation. The merged list should be returned by this method, and the original lists should be unchanged. This method should answer/return a Sentence 1.2 What to do 1. Design the interfaces and classes that you need for this purpose. We are purposely NOT giving you the interface code as a "starter pack". Develop your interface for Sentence by extracting the concepts from the specification above in Section 1.1 2. In a JUnit test class, create examples of sentences and write tests for each operation. 3. Design the fields and methods for your classes, and verify that your tests pass on them. 4. Check if you can abstract any common parts of your design/code. If so, take a "bottom up" approach and push these common features into an appropriate superclass
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Developing computer interface
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning