153k views
4 votes
Edit the code below in Java so that the interface is composed of the following nodes:

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
***
import java.util.ArrayList;
public interface Sentence {
ArrayList words = null;
/* Computes and returns the number of words in a sentence.
* The punctuation does not count as a word.
*/
public int getNumberOfWords();
/* Determines and returns the longest word in a sentence.
* The longest word should not begin or end with punctuation.
*/
public String longestWord();
/* Convert the sentence into one string.
* There must be a space between every two words.
* 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 (it shouldn’t add the period to the
* original sentence).
*/
public String toString();
/* 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.
*/
public Sentence clone();
/* 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.
*/
public Sentence merge(Sentence other);
}

User Jakubka
by
8.3k points

1 Answer

1 vote

import java.util.ArrayList;

public interface Sentence {

ArrayList<WordNode> words = null;

ArrayList<PunctuationNode> punctuation = null;

EmptyNode end = null;

/* Computes and returns the number of words in a sentence.

* The punctuation does not count as a word.

*/

public int getNumberOfWords();

/* Determines and returns the longest word in a sentence.

* The longest word should not begin or end with punctuation.

*/

public String longestWord();

/* Convert the sentence into one string.

* There must be a space between every two words.

* 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 (it shouldn’t add the period to the

* original sentence).

*/

public String toString();

/* 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.

*/

public Sentence clone();

/* 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.

*/

public Sentence merge(Sentence other);

}

We added two new nodes, PunctuationNode and EmptyNode, to represent punctuation and the end of the sentence respectively. The words field is now explicitly declared as an ArrayList of WordNode. We updated the comments to reflect the changes to the interface.

User Abstract
by
7.2k points