Tutorial :Java Program Organization: How can I get rid of this massive case statement?



Question:

I am creating a program that will fill in a given grammar. Right now I am modeling the "kinds of words" like this:

public class WordDescriptor {        public static final String noun = "N";      public static final String plural = "p";      public static final String nounPhrase = "h";      public static final String usuParticipleVerb = "V";      public static final String transitiveVerb = "t";      public static final String intransitiveVerb = "i";      public static final String adjective = "A";      public static final String adverb = "v";      public static final String conjunction = "C";      public static final String preposition = "P";      public static final String interjection = "!";      public static final String pronoun = "r";      public static final String definiteArticle = "D";      public static final String indefiniteArticle = "I";      public static final String nominative = "o";      public static final String defaultTag = "?";        private String word; // where word is one of the constants defined above.          public String getWord(){          return word;      }        public String setWord(){          return word;      }        /** For debugging only.         *        * @param s       * @return       */      protected static String getKind(String s){          if(s.equals(noun)){              return "noun";          }else if(s.equals(plural)){              return "plural";          }else if(s.equals(nounPhrase)){              return "nounPhrase";          }else if(s.equals(usuParticipleVerb)){              return "usuParticipleVerb";          }else if(s.equals(transitiveVerb)){              return "transitiveVerb";          }else if(s.equals(intransitiveVerb)){              return "intransitiveVerb";          }else if(s.equals(adjective)){              return "adjective";          }else if(s.equals(adverb)){              return "adverb";          }else if(s.equals(conjunction)){              return "conjunction";          }else if(s.equals(preposition)){              return "preposition";          }else if(s.equals(interjection)){              return "interjection";          }else if(s.equals(pronoun)){              return "pronoun";          }else if(s.equals(definiteArticle)){              return "definiteArticle";          }else if(s.equals(indefiniteArticle)){              return "indefiniteArticle";          }else if(s.equals(nominative)){              return "nominative";          } else if(s.equals(defaultTag)){              return "defaultTag";          }else{              return "other: "+s;          }      }    }  

This is pretty much the most ugly code that I can imagine. I know I can make it a bit better by using a case statement however that is still friggin ugly. Here is my question:

How can I make it beautiful? I was thinking about making:

  • class WordDescriptor, with subclasses:
    • class noun, with subclasses:
      • singular
      • plural
    • class verb
    • class adverb

But I am not sure if this seems like a great idea either. How can I this out better?


Edit: If I did take the second approach I am not even sure what the classes would look like. Here is an example:

public abstract class WordDescriptor {       public String toString();    }    public class Noun extends WordDescriptor {        public String toString(){          return "Noun";      }  }    public class Plural extends Noun{      public String toString(){          return "Plural";      }  }  


Solution:1

You could do something with an enumerated type.

public enum SpeechPart  {      NOUN ("noun"),      PLURAL ("plural"),      NOUNPHRASE ("noun phrase"),      ADVERB ("adverb"),      ADJECTIVE ("adjective"),      CONJUNCTION ("conjunction"),      VERB ("verb");        private String english;        SpeechPart(String inEnglish)      {          this.english = inEnglish;      }        public String toString() { return english; }  }  

You can now assign these to a variable.

SpeechPart dog = SpeechPart.NOUN;  SpeechPart ran = SpeechPart.VERB;  SpeechPart quickly = SpeechPart.ADVERB;  

And then you can see what their parts of speech are:

System.out.println(dog.toString());  System.out.println(quickly);        // Implicit call to toString()  

This solution assumes only a single part of speech per word. To allow for modifiers, such as "first person," "third person," "plural," "present," "progressive," etc., you could simply enumerate all of them -- a tedious job but need be done only once. Alternatively, you could adapt the Decorator Pattern, which addresses specifically the need to add attributes to an object dynamically.

Another suggestion is enumerate the modifiers:

public enum SpeechModifier  {      SINGULAR,      PLURAL,      FIRST_PERSON,      SECOND_PERSON,      THIRD_PERSON,      PRESENT,      PAST,      PERFECT,      PROGRESSIVE;  }  

Then build a class that combines them together:

public class Word  {      String word;      SpeechPart part;      EnumSet<SpeechModifier> modifiers;  }  

Now you can model a whole word:

Word w1 = new Word();  w1.word = "bouncing";  w1.part = SpeechPart.VERB;  w1.modifiers = EnumSet<SpeechModifier>.of(SpeechModifier.PRESENT, SpeechModifier.PROGRESSIVE);  

This solution, however, doesn't prevent non-sensical combinations, such as FIRST_PERSON NOUN PAST.


Solution:2

I would just create a dictionary or map from abbreviation to full description. Then getKind would look up its input in the mapping and return the result, or other if the input doesn't map to anything.


Solution:3

Could you use a dictionary with the code (s in your case) as the key and the returned string as the value?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »