How to I code without using the instanceof (see in bold) in my toString method? Please show work if possible!   /** * This is the WordNode class for the Sentence interface. */ public class WordNode implements Sentence { private final String word; private final Sentence rest; /** * This is the constructor for the Sentence class. * @param word is a string format * @param rest is a sentence */ public WordNode(String word, Sentence rest) { this.word = word; this.rest = rest; } /** * This calculates the total number of words in a sentence. * @return the total number of words from teh current */ @Override public int getNumberOfWords() { return 1 + this.rest.getNumberOfWords(); } /** * This method determines the longest word in the sentence. * @return a string of the longest word in teh sentence */ @Override public String longestWord() { if (word.length() >= this.rest.longestWord().length()) { return this.word; } return this.rest.longestWord(); } /** * This method adds a period to the end of a string if there is one. * If no string, there is no period. * @return a period to the string */ @Override public String toString() { if (this.rest.toString().equals("")) { return this.word + "."; } if (rest instanceof PunctuationNode) { return this.word + this.rest.toString(); } return this.word + " " + this.rest.toString(); } /** * This method clones a sentence; duplicates it. * @return a string of a clone sentence */ @Override public Sentence clone() { return new WordNode(this.word, this.rest.clone()); } /** * This method merges two sentences * @param secondSentence to merge with the first * @return a merge of two sentences to create a new one */ @Override public Sentence merge(Sentence secondSentence) { return new WordNode(this.word, this.rest.merge(secondSentence)); } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

How to I code without using the instanceof (see in bold) in my toString method? Please show work if possible!

 

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

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

/**
* This calculates the total number of words in a sentence.
* @return the total number of words from teh current
*/
@Override
public int getNumberOfWords() {
return 1 + this.rest.getNumberOfWords();
}

/**
* This method determines the longest word in the sentence.
* @return a string of the longest word in teh sentence
*/
@Override
public String longestWord() {
if (word.length() >= this.rest.longestWord().length()) {
return this.word;
}
return this.rest.longestWord();
}

/**
* This method adds a period to the end of a string if there is one.
* If no string, there is no period.
* @return a period to the string
*/
@Override
public String toString() {
if (this.rest.toString().equals("")) {
return this.word + ".";
}
if (rest instanceof PunctuationNode) {
return this.word + this.rest.toString();
}
return this.word + " " + this.rest.toString();
}

/**
* This method clones a sentence; duplicates it.
* @return a string of a clone sentence
*/
@Override
public Sentence clone() {
return new WordNode(this.word, this.rest.clone());
}

/**
* This method merges two sentences
* @param secondSentence to merge with the first
* @return a merge of two sentences to create a new one
*/
@Override
public Sentence merge(Sentence secondSentence) {
return new WordNode(this.word, this.rest.merge(secondSentence));
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Adjacency Matrix
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education