Question

In: Computer Science

Write in Java Modify the parent class (Plant) by adding the following abstract methods:(The class give...

Write in Java

  1. Modify the parent class (Plant) by adding the following abstract methods:(The class give in the end of question)
    1. a method to return the botanical (Latin) name of the plant
    2. a method that describes how the plant is used by humans (as food, to build houses, etc)
  2. Add a Vegetable class with a flavor variable (sweet, salty, tart, etc) and 2 methods that return the following information:
    1. list 2 dishes (meals) that the vegetable can be used in
    2. where this vegetable is grown

The Vegetable class should have the usual constructors (default and parameterized), get (accessor) and set (mutator) methods for each attribute, and a toString method

Child classes should call parent methods whenever possible to minimize code duplication.

The driver program must test all the methods in the Vegetable class, and show that the new methods added to the Plant class can be called by each of the child classes. Include comments in your output to describe what you are testing, for example   System.out.println(“testing Plant toString, accessor and mutator”);. Print out some blank lines in the output to make it easier to read and understand what is being output.

public class Plant {
  
private String name;
private String lifespan;

public Plant(){
name = "no name";
lifespan = "do not know";
}
public Plant(String newName, String newlife)
{
name = newName;
lifespan = newlife;
}
public String getName()
{
return name;
}
public String getlife()
{
return lifespan;
}
public void setName(String newName)
{
name = newName;
}
public void setLifeSpan(String newlife)
{
lifespan = newlife;
}
public void set(String newName, String newlife)
{
name = newName;
lifespan = newlife;
}
public String toString()
{
return "Name:"+name+"\nLifeSpan:"+lifespan;
}
}

Solutions

Expert Solution

/*******************************Plant.java************************/

import java.util.ArrayList;


/**
* The Class Plant.
*/
public abstract class Plant {

   /** The name. */
   private String name;
  
   /** The lifespan. */
   private String lifespan;

   /**
   * Instantiates a new plant.
   */
   public Plant() {
       name = "no name";
       lifespan = "do not know";
   }

   /**
   * Instantiates a new plant.
   *
   * @param newName the new name
   * @param newlife the newlife
   */
   public Plant(String newName, String newlife) {
       name = newName;
       lifespan = newlife;
   }

   /**
   * Gets the name.
   *
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * Gets the life.
   *
   * @return the life
   */
   public String getlife() {
       return lifespan;
   }

   /**
   * Sets the name.
   *
   * @param newName the new name
   */
   public void setName(String newName) {
       name = newName;
   }

   /**
   * Sets the life span.
   *
   * @param newlife the new life span
   */
   public void setLifeSpan(String newlife) {
       lifespan = newlife;
   }

   /**
   * Sets the.
   *
   * @param newName the new name
   * @param newlife the newlife
   */
   public void set(String newName, String newlife) {
       name = newName;
       lifespan = newlife;
   }

   /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
   public String toString() {
       return "Name:" + name + "\nLifeSpan:" + lifespan;
   }

   /**
   * Gets the botanical name.
   *
   * @return the botanical name
   */
   public abstract String getBotanicalName();

   /**
   * Usage.
   *
   * @return the array list
   */
   public abstract ArrayList<String> usage();
}
/*********************************Vegetable.java*******************************/

import java.util.ArrayList;


/**
* The Class Vegetable.
*/
public class Vegetable extends Plant {

   /** The flavor. */
   private String flavor;
  
   /** The origin. */
   private String origin;
  
   /** The dishes. */
   private ArrayList<String> dishes;
  
   /** The botnical name. */
   private String botnicalName;
  
   /** The uses. */
   private ArrayList<String> uses;

   /**
   * Instantiates a new vegetable.
   *
   * @param newName the new name
   * @param newlife the newlife
   */
   public Vegetable(String newName, String newlife) {
       super(newName, newlife);
       this.flavor = "N/A";
   }

   /**
   * Instantiates a new vegetable.
   *
   * @param newName the new name
   * @param newlife the newlife
   * @param flavor the flavor
   */
   public Vegetable(String newName, String newlife, String flavor) {
       super(newName, newlife);
       this.flavor = flavor;
   }

   /**
   * Gets the flavor.
   *
   * @return the flavor
   */
   public String getFlavor() {
       return flavor;
   }

   /**
   * Sets the flavor.
   *
   * @param flavor the new flavor
   */
   public void setFlavor(String flavor) {
       this.flavor = flavor;
   }

   /**
   * Gets the origin.
   *
   * @return the origin
   */
   public String getOrigin() {

       return origin;
   }

   /**
   * Gets the dishes.
   *
   * @return the dishes
   */
   public ArrayList<String> getDishes() {
       return dishes;
   }

   /**
   * Sets the dishes.
   *
   * @param dishes the new dishes
   */
   public void setDishes(ArrayList<String> dishes) {
       this.dishes = dishes;
   }

   /**
   * Sets the origin.
   *
   * @param origin the new origin
   */
   public void setOrigin(String origin) {
       this.origin = origin;
   }

  
   /**
   * Gets the botnical name.
   *
   * @return the botnical name
   */
   public String getBotnicalName() {
       return botnicalName;
   }

   /**
   * Sets the botnical name.
   *
   * @param botnicalName the new botnical name
   */
   public void setBotnicalName(String botnicalName) {
       this.botnicalName = botnicalName;
   }

   /**
   * Gets the uses.
   *
   * @return the uses
   */
   public ArrayList<String> getUses() {
       return uses;
   }

   /**
   * Sets the uses.
   *
   * @param uses the new uses
   */
   public void setUses(ArrayList<String> uses) {
       this.uses = uses;
   }

   /* (non-Javadoc)
   * @see Plant#getBotanicalName()
   */
   @Override
   public String getBotanicalName() {

       return botnicalName;
   }

  
   /* (non-Javadoc)
   * @see Plant#usage()
   */
   @Override
   public ArrayList<String> usage() {

       return uses;
   }

   /* (non-Javadoc)
   * @see Plant#toString()
   */
   @Override
   public String toString() {
       return super.toString() + "\nBotnicalName: " + botnicalName + "\nOrigin: " + origin + "\nDishes: " + dishes
               + "\nUses: " + uses;
   }

}
/***************************TestPlant.java******************/

import java.util.ArrayList;


/**
* The Class TestPlant.
*/
public class TestPlant {

   /**
   * The main method.
   *
   * @param args the arguments
   */
   public static void main(String[] args) {

       Plant plant = new Vegetable("Mango", "300 Years");
       ((Vegetable) plant).setOrigin("southern Asia");
       ((Vegetable) plant).setFlavor("Sweet");

       ArrayList<String> dishes = new ArrayList<>();
       dishes.add("Raw Mango Rasam");
       dishes.add("Corn and Raw Mango Salad");
       dishes.add("Chilled Mango Cheesecake");
       dishes.add("Mango and Mint Kheer");
       dishes.add("Eggless Mango Mousse");
       ((Vegetable) plant).setDishes(dishes);

       ((Vegetable) plant).setBotnicalName("Mangifera indica");
       ArrayList<String> uses = new ArrayList<>();
       uses.add("Food");
       uses.add("House");
       uses.add("Wood");
       ((Vegetable) plant).setUses(uses);

       System.out.println(plant.toString());

   }
}
/*****************output************************/

Name:Mango
LifeSpan:300 Years
BotnicalName: Mangifera indica
Origin: southern Asia
Dishes: [Raw Mango Rasam, Corn and Raw Mango Salad, Chilled Mango Cheesecake, Mango and Mint Kheer, Eggless Mango Mousse]
Uses: [Food, House, Wood]

Please let me know if you have any doubt or modify the answer, Thanks:)


Related Solutions

JAVA- Modify the LinkedList1 class presented in this chapter by adding sort() and reverse() methods. The...
JAVA- Modify the LinkedList1 class presented in this chapter by adding sort() and reverse() methods. The reverse method reverses the order of the elements in the list, and the sort method rearranges the elements in the list so they are sorted in alphabetical order. The class should use recursion to implement the sort and reverse operations. Extend the graphical interface in the LinkedList1Demo class to support sort and reverse commands, and use it to test the new methods. LinkedList1: class...
Using Java create a program that does the following: Modify the LinkedList1 class by adding sort()...
Using Java create a program that does the following: Modify the LinkedList1 class by adding sort() and reverse() methods. The reverse method reverses the order of the elements in the list, and the sort method rearranges the elements in the list so they are sorted in alphabetical order. Do not use recursion to implement either of these operations. Extend the graphical interface in the LinkedList1Demo class to support sort and reverse commands, and use it to test the new methods....
Modify StudentLinkedList class by adding the following methods: printStudentList: print by calling and printing “toString” of...
Modify StudentLinkedList class by adding the following methods: printStudentList: print by calling and printing “toString” of every object in the linkedList. Every student object to be printed in a separate line.  deleteStudentByID(long id): delete student object from the list whose ID is matching with the passed parameter.  sortListByID(): sort the linkedlist according to students IDs.  findMarksAverage(): find the average of all marks for all students in the list.  findMinMark(int markIndex): find the student with the minimum...
Problem 3: Modify StudentLinkedList class by adding the following methods:  printStudentList: print by calling and...
Problem 3: Modify StudentLinkedList class by adding the following methods:  printStudentList: print by calling and printing “toString” of every object in the linkedList. Every student object to be printed in a separate line.  deleteStudentByID(long id): delete student object from the list whose ID is matching with the passed parameter.  sortListByID(): sort the linkedlist according to students IDs.  findMarksAverage(): find the average of all marks for all students in the list.  findMinMark(int markIndex): find the student...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has a name property (string) Has a speech property (string) Has a constructor that takes two arguments, the name and the speech It has getSpeech() and setSpeech(speech) It has getName and setName(name) It has a method speak() that prints the name of the animal and the speech. o Example output: Tom says meow. Mouse Inherits the Animal class Has a constructor takes a name and...
Java - Write an abstract class called Shape with a string data field called colour. Write...
Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()
write program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
CS Using the following UML outline, create the following abstract parent class along with the 4...
CS Using the following UML outline, create the following abstract parent class along with the 4 subclasses and Driver class. Implement the Classes, listed attributes and methods along with any additional things that you may need. Add a driver class that utilizes the methods/attributes in the classes to output the following based on the vehicle type (therefore, you must create an instance for all 4 subclasses in your driver): 1. Vehicle Type (i.e., Car, Airplane, etc.) 2. Transportation method (wheels,...
Java instructions: 1. Modify abstract superclass (Employee10A) so it comment out the abstract payPrint method and...
Java instructions: 1. Modify abstract superclass (Employee10A) so it comment out the abstract payPrint method and uses a toString method to print out it’s instance variables. Make sure toString method cannot be overridden.​​​​​​ Source code below: public abstract class Employee10A {    private String firstName, lastName; static int counter = 0;    public Employee10A(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }    @Override public String toString() { return ("The employee's full name is " + firstName...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT