Advertisement
Guest User

Remington

a guest
Nov 21st, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. package theHardWay;
  2.  
  3. public class Trait {
  4.  
  5. public static final String[] traits = {
  6. "striped", "long-tailed","furry", "brown","short-eared",
  7. "spotted","short-tailed","hairless","white","long-eared"};
  8.  
  9. private String description;
  10. private int index;
  11.  
  12. public Trait() {
  13. index = (int)(Math.random()*traits.length);
  14. description = traits[index];
  15. }
  16.  
  17. public static void main(String[] args) {
  18. Trait longTailed = new Trait(1);
  19. Trait shortTailed = new Trait(6);
  20. System.out.println(longTailed+" and "+ shortTailed+
  21. " are the same trait: "+longTailed.equals(shortTailed));
  22. }
  23.  
  24. public Trait(int index) {
  25. this.index = index;
  26. description = traits[index];
  27. }
  28.  
  29. public String toString() {
  30. return description;
  31. }
  32.  
  33. private int getIndex() {
  34. return index;
  35. }
  36.  
  37. public boolean equals(Object o) {
  38. return (o instanceof Trait) && (this.getIndex() - ((Trait)o).getIndex()) % (traits.length/2)==0;
  39. }
  40.  
  41. public static Trait getDominantTrait(Trait t1, Trait t2) {
  42. if(t1.equals(t2) && t1.getIndex() < t2.getIndex()) return t1;
  43. else if(t1.equals(t2) && t2.getIndex() < t1.getIndex()) return t2;
  44. else if(Math.random() < .5) return t1;
  45. return t2;
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement