Advertisement
atanasovetr

Natali

Jan 20th, 2021
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. class Plant {
  2.     protected String type;
  3.     protected boolean sunLovely;
  4.     protected String bloomingSeason;
  5.  
  6.     public Plant(){
  7.  
  8.     }
  9.  
  10.     public Plant(String type, boolean sunLovely, String bloomingSeason){
  11.         this.type = type;
  12.         this.sunLovely = sunLovely;
  13.         this.bloomingSeason = bloomingSeason;
  14.  
  15.     }
  16.  
  17.     public void bloom(String season){
  18.         if (this.bloomingSeason.equals(season)) {
  19.             System.out.println("The plant is blooming!");
  20.         }
  21.         else{
  22.             System.out.println("The plant can't bloom right now ;(");
  23.         }
  24.     }
  25.  
  26.     @Override
  27.     public String toString() {
  28.         String sunStatus;
  29.         if(sunLovely){
  30.             sunStatus = "loves sun";
  31.         }
  32.         else{
  33.             sunStatus = "prefers shadows";
  34.         }
  35.         return "type " + type + ", " + sunStatus + " and it's blooming season is " + bloomingSeason;
  36.     }
  37.  
  38.     public String getType() {
  39.         return type;
  40.     }
  41.  
  42.     public void setType(String type) {
  43.         this.type = type;
  44.     }
  45.  
  46.     public boolean isSunLovely() {
  47.         return sunLovely;
  48.     }
  49.  
  50.     public void setSunLovely(boolean sunLovely) {
  51.         this.sunLovely = sunLovely;
  52.     }
  53.  
  54.     public String getBloomingSeason() {
  55.         return bloomingSeason;
  56.     }
  57.  
  58.     public void setBloomingSeason(String bloomingSeason) {
  59.         this.bloomingSeason = bloomingSeason;
  60.     }
  61.  
  62. }
  63.  
  64. class AppleTree extends Plant{
  65.     private int applesOnTheTree;
  66.  
  67.     public AppleTree(){
  68.  
  69.     }
  70.  
  71.     public AppleTree(String type, boolean sunLovely, String bloomingSeason, int applesOnTheTree){
  72.         super(type, sunLovely, bloomingSeason);
  73.         this.applesOnTheTree = applesOnTheTree;
  74.     }
  75.  
  76.     public void takingApples(int applesTaken){
  77.         this.applesOnTheTree = this.applesOnTheTree - applesTaken;
  78.     }
  79.  
  80.     @Override
  81.     public String toString() {
  82.         return "Your apple tree is " + super.toString() + '\n' + "It also has " + applesOnTheTree + " apples in the leaves!" + '\n';
  83.     }
  84.  
  85.     public int getApplesOnTheTree() {
  86.         return applesOnTheTree;
  87.     }
  88.  
  89.     public void setApplesOnTheTree(int applesOnTheTree) {
  90.         this.applesOnTheTree = applesOnTheTree;
  91.     }
  92. }
  93.  
  94. class Rose extends Plant{
  95.     public Rose(){
  96.  
  97.     }
  98.  
  99.     public Rose(String type, boolean sunLovely, String bloomingSeason){
  100.         super(type, sunLovely, bloomingSeason);
  101.     }
  102.  
  103.     @Override
  104.     public String toString() {
  105.         return "Your rose is " + super.toString() + '\n';
  106.     }
  107.  
  108. }
  109.  
  110. class Test{
  111.     public static void main(String[] args) {
  112.         Rose rose1 = new Rose("White Rose", true, "Summer");
  113.         rose1.bloom("Summer");
  114.         System.out.println(rose1);
  115.  
  116.         AppleTree appleTree1 = new AppleTree("Green Apples", true, "Spring and Summer", 300);
  117.         appleTree1.bloom("Winter");
  118.         appleTree1.takingApples(20);
  119.         System.out.println(appleTree1);
  120.     }
  121. }
  122.  
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement