Tuesday, June 20, 2017

Aggregate class - Tutorial






public class SentenceDemo{
   public static void main(String[] args){
      
       Sentence s1=new Sentence();
       System.out.println(s1.getMessage());
       System.out.println(s1.getMessage());
       System.out.println(s1.getMessage());
       System.out.println(s1.getMessage());
       System.out.println(s1.getMessage());
       System.out.println(s1.getMessage());
       System.out.println(s1.getCounter());
    }
}

public class Sentence{
  private String message;
  public static final int SIZE=5;
  private static int counter;
  private String messages[]={
      new Pronoun().getWord()+new Verb().getWord()+new Adjective().getWord()+new Adverb().getWord(),
      new Pronoun().getWord()+new Verb().getWord()+new Adjective().getWord()+new Adverb().getWord(),
      new Pronoun().getWord()+new Verb().getWord()+new Adjective().getWord()+new Adverb().getWord(),
      new Pronoun().getWord()+new Verb().getWord()+new Adjective().getWord()+new Adverb().getWord(),
      new Pronoun().getWord()+new Verb().getWord()+new Adjective().getWord()+new Adverb().getWord(),
    };
  public Sentence(){
      java.util.Random ran=new java.util.Random();
      message=messages[ran.nextInt(SIZE)]+".";
     counter++;
    }
  private Sentence(String message){
      super();
      this.message=message;
      counter++;
    }
  public String getMessage(){
      java.util.Random ran=new java.util.Random();
      message=messages[ran.nextInt(SIZE)]+".";
      message=message.replaceFirst("[a-zA-Z]", ""+Character.toUpperCase(message.charAt(0)));
      return message;
    }
    public Sentence deepCopy(Sentence s){
        return new Sentence(s.message);
    }
    public boolean equals(Sentence s){
        return (message.equals(s.message))?true:false;
    }
    public int getCounter(){
        return counter;
    }
}

public class Verb{
  private String word;
  private String words[]={" write "," run "," drive "," walk ","crawl"};
  public Verb(){
      java.util.Random ran=new java.util.Random();
      word=words[ran.nextInt(Sentence.SIZE)];
    }
  public String getWord(){
      return word;
    }
}

public class Pronoun{
  private String word;
  private String words[]={"I ","you ","me ","we ","us "};
  public Pronoun(){
      java.util.Random ran=new java.util.Random();
      word=words[ran.nextInt(Sentence.SIZE)];
    }
  public String getWord(){
      return word;
    }
}

public class Adjective{
  private String word;
  private String words[]={" very "," quiet ","  "," short "," creepy "};
  public Adjective(){
      java.util.Random ran=new java.util.Random();
      word=words[ran.nextInt(Sentence.SIZE)];
    }
  public String getWord(){
      return word;
    }
}

public class Adverb{
  private String word;
  private String words[]={" quickly "," very "," slowly "," beautifully "," happily "};
  public Adverb(){
      java.util.Random ran=new java.util.Random();
      word=words[ran.nextInt(Sentence.SIZE)];
    }
  public String getWord(){
      return word;
    }
}

No comments:

Post a Comment