Question

In: Computer Science

Create a class Sentence with an instance variable public Word[] words. Furthermore: The class should have...

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.

Solutions

Expert Solution

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.


Related Solutions

(a) Create a Card class that represents a playing card. It should have an int instance...
(a) Create a Card class that represents a playing card. It should have an int instance variable named rank and a char variable named suit. Include the following methods: A constructor with two arguments for initializing the two instance variables. A copy constructor. A method equals — with one argument — which compares the calling object with another Card and returns true if and only if the corresponding ranks and suits are equal. Make sure your method will not generate...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length and #width. Make sure the variable names match those words. #Both will be floats. # #Rectangle should have a constructor with two required #parameters, one for each of those attributes (length and #width, in that order). # #Rectangle should also have a method called #find_perimeter. find_perimeter should calculate the #perimeter of the rectangle based on the current values for #length and width. # #perimeter...
Create a Java class named Package that contains the following: Package should have three private instance...
Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height. Package should have one private instance variable of the type Scanner named input, initialized to System.in. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.   Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of...
#Create a class called FrapOrder. FrapOrder should #have two attributes (instance variables): size and #extra_shots. Make...
#Create a class called FrapOrder. FrapOrder should #have two attributes (instance variables): size and #extra_shots. Make sure the variable names match those #words. size will be a character, either "S", "M", or "L". #extra_shots will be an integer. # #FrapOrder should have a constructor with two required #parameters, one for each of those attributes (size and #extra_shots, in that order). # #FrapOrder should also have a method called get_total. #get_total should calculate the total cost of the order. #If size...
For this Lab you have to implement a class Builder. Your Builder class should have instance...
For this Lab you have to implement a class Builder. Your Builder class should have instance variable name. , Supply a constructor method for your Builder class and the following methods: getName(), makeRow(int n, String s), printPyramid(int n, String s). Examining the problem, we need to create a Builder class, declare Builder class as follows public class Builder { } Inside the Builder class, declare a String variable called name. Step 3: Defining the constructors: Remember that the purpose of...
Question 1 price is an instance variable of a class. Inside that class there is a...
Question 1 price is an instance variable of a class. Inside that class there is a method public void change(int price). From inside the method " change" how do we refer to the instance variable price and not the parameter price? Answer: Question 2 A method of a class can access which of the following: 1 global variables in the same scope 2. local variables defined within the method 3. the instance variables of its class 4. its parameters Question...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # #...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # # - meat # - to_go # - rice # - beans # - extra_meat (default: False) # - guacamole (default: False) # - cheese (default: False) # - pico (default: False) # - corn (default: False) # #The constructor should let any of these attributes be #changed when the object is instantiated. The attributes #with a default value should be optional. Both positional #and...
Write a class to accept a sentence from the user and prints a new word in...
Write a class to accept a sentence from the user and prints a new word in a terminal formed out of the third letter of each word. For example, the input line “Mangoes are delivered after midnight” would produce “neltd”. Program Style : Basic Java Programming
Create a class called Sphere. The class will contain the following    Instance data double radius...
Create a class called Sphere. The class will contain the following    Instance data double radius Methods Constructor with one parameter which will be used to set the radius instance data Getter and Setter for radius             Area - calculate the area of the sphere (4 * PI * radius * radius)             Volume - calculate and return the volume of the sphere (4/3 * PIE * radius * radius * radius) toString - returns a string with the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT