In java. Inheritance Create a properly encapsulated class named Sentence that ha
ID: 3914746 • Letter: I
Question
In java. Inheritance
Create a properly encapsulated class named Sentence that has the following: - A properly encapsulated String instance variable named sentence. - A constructor that takes 1 parameter, a String and set the instance variable. - A getter method for sentence instance variable. - Override the Object equals method, The method determines if two Sentence objects are equal by checking their instance variables are equal. Create a properly encapsulated class named Word that inherits from Sentence and has the following: - A properly encapsulated String instance variable named noVowelsWord. - A constructor that takes 2 String parameters s and w, s is used to set the super class’s instance variable and w is used to set the noVowelsWord instance variable after removing all the vowels. You may not use any loops or if statements to do this. - A getter method for noVowelsWord instance variable. - A method named isSubstring that takes no parameters and returns a boolean, the method returns true if the instance variable noVowelsWord is a substring of the super class’s instance variable sentence, otherwise it returns false. You may not use any loops or if statements to do this.
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
Sentence.java
---------
public class Sentence {
protected String sentence;
public Sentence(String s) {
sentence = s;
}
public String getSentence() {
return sentence;
}
public void setSentence(String s) {
this.sentence = s;
}
@Override
public boolean equals(Object obj) {
if(obj != null && obj instanceof Sentence){
if(((Sentence)obj).sentence.equals(sentence))
return true;
}
return false;
}
}
Word.java
----------
public class Word extends Sentence {
private String noVowelsWord;
public Word(String s, String w) {
super(s);
noVowelsWord = w.replaceAll("[aeiouAEIOU]", ""); //use regular expression to replace all vowels with empty strings
}
public String getNoVowelsWord() {
return noVowelsWord;
}
public boolean isSubstring(){
return sentence.contains(noVowelsWord);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.