// 5 points static boolean Q2(String possiblePhrase){ // return true if the inpu
ID: 3818907 • Letter: #
Question
// 5 points
static boolean Q2(String possiblePhrase){
// return true if the input could be a phrase in the format "<adjective> <noun> <verb> <adverb>",
// false otherwise. Since many words have multiple parts of speech depending on their usage, return
// true if it is possible that the input can be in this format by any choice of each word's part of speech.
// You may assume the input contains 4 words separated by spaces.
return false;
}
Can someone give me the correct answer to this question? Missed the lecture on this topic and this is the only question I can't get.
Explanation / Answer
static boolean Q2(String possiblePhrase){
// We first need a list of words
String adj[] = {"Good","New", "First", "Last", "Long", "great", "little", "own", "other", "old", "right", "big", "high", "different", "small", "large", "next", "early", "young", "important", "few", "public", "bad", "same", "able"};
String noun[] = { "time", "year", "people", "way", "day","man", "thing", "woman", "life", "child", "world"};
String verb[] = {"bear", "bore", "borne", "beat", "beaten", "become", "became", "see", "wear", "is", "am", "are", "know", "found", "left"};
String adv[] = {"not", "also", "very", "often", "however", "too", "really", "early", "never","again", "rather", "almost", "else", "thus", "exactly", "certainly", "normally", "fully"};
// return true if the input could be a phrase in the format "<adjective> <noun> <verb> <adverb>",
// false otherwise. Since many words have multiple parts of speech depending on their usage, return
// true if it is possible that the input can be in this format by any choice of each word's part of speech.
// You may assume the input contains 4 words separated by spaces.
String words[] = possiblePhrase.split(" "); //we extract words
if (!contains(adj,words[0]))
return false;
if (!contains(noun,words[1]))
return false;
if (!contains(verb,words[2]))
return false;
if (!contains(adv,words[3]))
return false;
return true;
}
private static boolean contains(String[] array, String word) {
for (String s:array )
if (s.equalsIgnoreCase(word))
return true;
return false;
}
This is how you should proceed. You can populate the string arrays of adjective, noun, verb, and adverb with your own list words. After doing so you are good to go. I hope this helps you. Incase you are still facing problems with the code, please comment below. I shall be glad to help you with the code.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.