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;

class Plant{
    private String name;
    private int height;
    public Plant()
    {
        name="";
        height=1;
    }
    public Plant(String name, int height)
    {
        this.name=name;
        this.height=height;
    }
    public void setName(String name)
    {
        this.name=name;
    }
    public String getName()
    {
        return name;
    }
    public void setHeight(int height)
    {
        this.height=height;
    }
    public int getHeight()
    {
        return height;
    }
    public String toString()
    {
        return "plant name : "+name+", height: "+height;
    }
}

class PalmTree extends Plant{
    private String fruitName;
    public PalmTree()
    {
        super();
        fruitName="";
    }
    public PalmTree(String name,int height, String fruitName)
    {
        super(name,height);
        this.fruitName=fruitName;
    }
    public void setFruitName(String fruitName)
    {
        this.fruitName=fruitName;
    }
    public String getFruitName()
    {
        return fruitName;
    }
    public String toString()
    {
        return "palm tree information:"+ super.toString()+", fruit Name: "+fruitName;
    }
}

public class Main{
     public static void main(String[] args)
  {
      Scanner input=new Scanner(System.in);
      
      PalmTree p1,p2;
      p1=new PalmTree();
      System.out.println("enter the palm tree name:");
      p1.setName(input.next());
      System.out.println("enter the palm tree HEIGHT:");
      p1.setHeight(input.nextInt());
      System.out.println("enter the palm tree fruit name:");
      p1.setFruitName(input.next());
     
      p2=new PalmTree("washtonia",10,"dates");
      System.out.println(p1.toString());
      System.out.println(p2.toString());
  }
}

need an explanation for this answer? contact us directly to get an explanation for this answer
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 (2)

Similar questions


need a help?


find thousands of online teachers now
<< What is the output of the following programs, inhe...