Advertisement
mmayoub

inheritance, exercise 01, slide 27

Jul 16th, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. Inheritance, Excercise 01, slide 27
  2.  
  3. /*
  4.  * Testrer01 class
  5.  *******************************************************/
  6. import packege01.Cat;
  7. import packege01.SiamiCat;
  8. import packege01.StreetCat;
  9.  
  10. public class Testrer01 {
  11.  
  12.     public static void main(String[] args) {
  13.         Cat c = new Cat("coco", 12, "brown");
  14.         StreetCat sc = new StreetCat("soso", 10, "gold", 2);
  15.         SiamiCat siami = new SiamiCat("siami", 18, "White", "any food");
  16.         System.out.println(c);
  17.         System.out.println(sc);
  18.         System.out.println(siami);
  19.     }
  20. }
  21.  
  22. /*
  23.  * Cat class
  24.  *******************************************************/
  25. package packege01;
  26.  
  27. public class Cat {
  28.     // private properties
  29.     protected String name; // cat name
  30.     protected double whisker;// length of cat whisker in mm
  31.     protected String color; // color of the cat
  32.  
  33.     public Cat(String name, double whisker, String color) {
  34.         super();
  35.         this.name = name;
  36.         this.whisker = whisker;
  37.         this.color = color;
  38.     }
  39.  
  40.     public String getName() {
  41.         return this.name;
  42.     }
  43.  
  44.     public void setName(String name) {
  45.         this.name = name;
  46.     }
  47.  
  48.     public double getWhisker() {
  49.         return this.whisker;
  50.     }
  51.  
  52.     public void setWhisker(double whisker) {
  53.         this.whisker = whisker;
  54.     }
  55.  
  56.     public String getColor() {
  57.         return this.color;
  58.     }
  59.  
  60.     public void setColor(String color) {
  61.         this.color = color;
  62.     }
  63.  
  64.     @Override
  65.     public String toString() {
  66.         return "Cat [" + fieldsList() + "]";
  67.     }
  68.  
  69.     protected String fieldsList() {
  70.         return "name=" + name + ", whisker=" + whisker + ", color=" + color;
  71.     }
  72. }
  73.  
  74. /*
  75.  * StreetCat class
  76.  *******************************************************/
  77. package packege01;
  78.  
  79. public class StreetCat extends Cat {
  80.     private int fightsNo; // number of fights
  81.  
  82.     public StreetCat(String name, double whisker, String color, int fightsNo) {
  83.         super(name, whisker, color);
  84.         if (!this.setFightsNo(fightsNo))
  85.             this.fightsNo = 0;
  86.     }
  87.  
  88.     public int getFightsNo() {
  89.         return this.fightsNo;
  90.     }
  91.  
  92.     public boolean setFightsNo(int fightsNo) {
  93.         if (fightsNo < 0) {
  94.             return false;
  95.         }
  96.         this.fightsNo = fightsNo;
  97.         return true;
  98.     }
  99.  
  100.     @Override
  101.     public String toString() {
  102.         return "StreetCat [" + super.fieldsList() + ", fightsNo=" + fightsNo
  103.                 + "]";
  104.     }
  105.  
  106. }
  107.  
  108. /*
  109.  * SiamiCat class
  110.  *******************************************************/
  111. package packege01;
  112.  
  113. public class SiamiCat extends Cat {
  114.     protected String bestFood; // name of best food
  115.  
  116.     public SiamiCat(String name, double whisker, String color, String bestFood) {
  117.         super(name, whisker, color);
  118.         this.bestFood = bestFood;
  119.     }
  120.  
  121.     public String getBestFood() {
  122.         return this.bestFood;
  123.     }
  124.  
  125.     public void setBestFood(String bestFood) {
  126.         this.bestFood = bestFood;
  127.     }
  128.  
  129.     @Override
  130.     public String toString() {
  131.         return "SiamiCat ["+super.fieldsList()+", bestFood=" + bestFood + "]";
  132.     }
  133.  
  134.    
  135.    
  136.    
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement