In: Computer Science
Create a class Sentence with an instance variable public Word[] words.
Furthermore: The class should have a constructor Sentence(int size), where size determines the length of the sentence field of a given Sentence.
The class should have an instance method public boolean isValid() that determines the validity of a sentence according to the rules detailed below.
Also create a public nested class Word, with instance variables String value and Type type.
Within this class, you must create: A public enum type Type that contains NOUN, ADJECTIVE, and VERB. The constructor Word(String value, Type type).
RULES FOR SENTENCE VALIDITY:
1.A sentence must have a length of at least 1.
2. A NOUN must be followed by a VERB (unless it is at the end of a sentence).
3.An ADJECTIVE must be followed only by another ADJECTIVE or a NOUN.
4.A sentence must have one and only one VERB.
5. A sentence must end in a NOUN or in a VERB.
Word.java
public class Word {
  
   enum Type{
       NOUN,ADJECTIVE,VERB;
   }
   String value;
   Type type;
  
  
   public Word(String value, Type type) {
       super();
       this.value = value;
       this.type = type;
   }
   public String getValue() {
       return value;
   }
   public void setValue(String value) {
       this.value = value;
   }
   public Type getType() {
       return type;
   }
   public void setType(Type type) {
       this.type = type;
   }
  
  
}
Sentence.java
public class Sentence {
   Word[] words;
  
   public Sentence(int size) {
       words = new Word[size];
   }
  
   public Sentence(Word words[]) {
       this.words = words;
   }
  
   public boolean isValid() {
      
       int size = words.length;
      
       // Checking for the First Condition
of minumum size of one
       if(size>0) {
          
       //Checking the Fisth condition....
A sentence must end in a NOUN or in a VERB.
       if(words[size-1].type==Type.NOUN ||
words[size-1].type==Type.VERB) {
          
          
           int count =
0;
          
           for(int i=0;
i<size;i++)
          
    if(words[i].type==Type.VERB)
          
        count++;
          
           // Checking the
fourth condition of Sentence has exactly one Verb
           if(count==1)
{
          
   
          
    int count1=0;
          
   
          
    for(int i=0; i<size;i++) {
          
        if(words[i].type==Type.NOUN)
{
          
           
          
           
if(i<size-1)
          
           
    if(words[i].type!=Type.VERB)
          
           
        count1++;
          
        }
          
    }
          
   
          
    // Checking the Second condition of A NOUN must
be followed by a VERB (unless it is at the end of a
sentence).
          
   
          
    if(count1==0) {
          
       
          
        int count2=0;
          
       
          
        for(int i=0; i<size;i++)
{
          
           
if(words[i].type==Type.ADJECTIVE) {
          
           
   
          
           
    if(i<size-1)
          
           
       
if(words[i].type!=Type.ADJECTIVE)
          
           
           
if(words[i].type!=Type.NOUN)
          
           
            return
false;
          
            }
          
        }
          
       
          
        if(count2==0) {
          
            return
true;
          
        }
          
       
          
       
          
    }
          
   
          
    else
          
        return false;
           }
          
           else
          
    return false;
       }
      
       else
           return
false;
      
       }
      
       else
           return
false;
          
      
       return false;
      
   }
}
If you left with any doubts, feel free to ask.