Advertisement
binibiningtinamoran

Dog

May 27th, 2019
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. public class Dog {
  2.  
  3.     private String name, color, breed;
  4.     private int age, weight;
  5.  
  6.     // CONSTRUCTOR
  7.     public Dog() {
  8.         this("none entered", "none entered", "none entered",1);
  9.     }
  10.  
  11.     public Dog(String name, String color, String breed, int age) {
  12.         this.name = name;
  13.         this.color = color;
  14.         this.breed = breed;
  15.  
  16.         if (age < 1) {
  17.             this.age = 1;
  18.         } else {
  19.             this.age = age;
  20.         }
  21.     }
  22.  
  23.     public Dog(int weight) {
  24.         this();
  25.         this.weight = weight;
  26.     }
  27.  
  28.     // Setters
  29.     public void setName(String name) {
  30.         this.name = name;
  31.     }
  32.  
  33.     public void setColor(String color) {
  34.         this.color = color;
  35.     }
  36.  
  37.     public void setBreed(String breed) {
  38.         this.breed = breed;
  39.     }
  40.  
  41.     public void setAge(int age) {
  42.         if (age < 1) {
  43.             this.age = 1;
  44.         } else {
  45.             this.age = age;
  46.         }
  47.     }
  48.  
  49.     public void setWeight(int weight) {
  50.         if (weight < 1) {
  51.             this.weight = 1;
  52.         } else {
  53.             this.weight = weight;
  54.         }
  55.     }
  56.  
  57.     // GETTERS
  58.     public String getName() {
  59.         return this.name;
  60.         // or return name;
  61.     }
  62.  
  63.     public String getColor() {
  64.         return this.color;
  65.     }
  66.  
  67.     public String getBreed() {
  68.         return this.breed;
  69.     }
  70.  
  71.     public int getAge() {
  72.         return this.age;
  73.     }
  74.  
  75.     public int getWeight() {
  76.         return this.weight;
  77.     }
  78.  
  79.     @Override
  80.     public String toString() {
  81.         return "Dog's name: " + this.name + "\n" +
  82.                 "Age: " + this.age + "\n" +
  83.                 "Breed: " + this.breed + "\n" +
  84.                 "Color: " + this.getColor() + "\n";
  85.     }
  86.  
  87.     @Override
  88.     public boolean equals(Object other) {
  89.         if (other instanceof Dog) {
  90.             Dog otherDog = (Dog) other;
  91.  
  92.             return this.name.equalsIgnoreCase(otherDog.name) &&
  93.                     this.color.equalsIgnoreCase(otherDog.getColor()) &&
  94.                     this.breed.equalsIgnoreCase(otherDog.getBreed()) &&
  95.                     this.age == otherDog.getAge();
  96.         } else {
  97.             return false;
  98.         }
  99.  
  100.     }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement