Advertisement
Sunjaree

Lab manual 10 (Plants)

Dec 8th, 2019
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. class Plant{
  2.  
  3.     private String name,color;
  4.  
  5.   public   Plant(String name,String color){
  6.         this.name = name;
  7.         this.color  = color;
  8.     }
  9.  
  10.     public String getName() {
  11.         return name;
  12.     }
  13.  
  14.     public String getColor() {
  15.         return color;
  16.     }
  17.     public String toString(){
  18.       return  getName() + " " + getColor();
  19.     }
  20. }
  21.  
  22. class Flower extends Plant{
  23.  
  24.     boolean hasSmell,hasThorn;
  25.  
  26.  
  27.     public Flower(String name,String color,boolean hasSmell,boolean hasThorn){
  28.         super(name,color);
  29.         this.hasSmell = hasSmell;
  30.         this.hasThorn = hasThorn;
  31.     }
  32.    public boolean gethasSmell(){
  33.         return hasSmell;
  34.    }
  35.  
  36.     public boolean gethasThorn(){
  37.         return hasThorn;
  38.     }
  39.  
  40.  
  41.     public String toString(){
  42.         return getName() + " "  + getColor() + " " + gethasSmell() + " "  + gethasThorn();
  43.     }
  44.  
  45.  
  46. }
  47. class Herb extends Plant{
  48.  
  49.     String season;
  50.     boolean isMedicinal;
  51.  
  52.     public Herb(String name,String color,String season,boolean isMedicinal){
  53.         super(name,color);
  54.         this.season = season;
  55.         this.isMedicinal = isMedicinal;
  56.     }
  57.  
  58.     public String getSeason() {
  59.         return season;
  60.     }
  61.  public  boolean  getisMedicinal(){
  62.         return isMedicinal;
  63.  }
  64.  
  65.     public String toString() {
  66.         return getName() + " " + getColor()  + " " + getSeason() + " " + getisMedicinal();
  67.     }
  68. }
  69.  
  70. public class main {
  71.  
  72.  
  73.     public  static void  main(String[] args){
  74.  
  75.         Plant[] object = new Plant[5];
  76.  
  77.         object[0] = new Plant("blah blah","Yellow");
  78.         object[1] = new Flower("Rose","Red",true,true);
  79.         object[2] = new Herb("Mint","Green","Autumn",true);
  80.  
  81.         for(int i = 0;i<3;i++){
  82.             System.out.println(object[i].toString());
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement