Q:

Define a class name Plant , Define another class name PalmTree which is extended form Plant

0

Define a class name Plant with the following specifications:

  1. A private variable name of type String.
  2. A private variable height of type int.
  3. A zero-parameterized constructor to initialize all the variables to " " and 1 respectively.
  4. A full-parameterized constructor to initialize the variables.
  5. Setter and getter methods for both variables.
  6. Override method toString to return the object's data.

 

Define another class name PalmTree which is extended form Plant with the following specifications:

  1. A private variable fruitName of type String.
  2. A zero-parameterized constructor which calls super class zero-parameter constructor, and also initializes fruitName to " ".
  3. A full-parameterized constructor initialize all the variables.
  4. Setter and getter methods for fruitName variable.
  5. Override method toString to return the object's data.

 

Check the functionality in main class by performing the following:

  1. Create two variables of type PalmTree.
  2. Ask the user to enter the data of one PalmTree object.
  3. Use both constructors of PalmTree.
  4. Print the objects you created.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.Scanner;
public class Main
{
  public static void main(String[] args)
  {
      
     
      Scanner input = new Scanner(System.in);
      System.out.println("Pleas enter the plant name :");
      String PlantName=input.nextLine();
      System.out.println("Pleas enter the Fruet name :");
      String FruetName=input.nextLine();
      System.out.println("Pleas enter the Hight :");
      int Hight=input.nextInt();
      PalmTree tree1=new PalmTree();
      PalmTree tree2=new PalmTree(PlantName,FruetName,Hight);
    System.out.println(tree2);
    System.out.println(tree1);
  }
}
class plant
{
    private String Name;
    private int hight;
    public plant()
    {
        Name="";
        hight=1;
    }
    public plant(String name,int hight)
    {
        Name=name;
        this.hight=hight;
    }

  public String getName() {
    return Name;
  }

  public void setName(String newName) {
    this.Name = newName;
  }

  public int getHight() {
    return hight;
  }

  public void setHight(int newHight) {
    this.hight = newHight;
  }
 
 
 public String toString() {
return "Tree Name is : " + this.Name + " And its hight is: " + this.hight;
}
}

class PalmTree extends plant
{
    private String fruetName;
    public PalmTree()
    {
        super();
        fruetName="";
    }
    public PalmTree( String PlantName,String FruetName,int Hight)
    {
        super(PlantName,Hight);
        this.fruetName= FruetName;
    }
    
public String toString() {
return "Plant Name is : "+this.getName() + " Fruet Name is :"+this.fruetName+" ,And its hight is: " + this.getHight();
}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now