Question

In: Computer Science

Problem Write in drjava is fine. Using the classes from Assignment #2, do the following: Modify...

Problem

Write in drjava is fine.

Using the classes from Assignment #2, do the following:

  1. Modify the parent class (Plant) by adding the following abstract methods:
    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.

Assignment Submission:

Submit a print-out of the Plant and Vegetable classes, the driver file and a sample of the output. Also include a UML diagram of the classes involved. (Use tables in Word to create the various classes. Remember to use the correct arrows between the classes)

Marking Checklist

  1. Does EACH class have all the usual methods?
  2. Are all methods in EACH class tested, including child objects calling inherited parent methods?
  3. Does the child class call the parent’s constructor?
  4. Does the child class override the parent’s toString?
  5. Does the output produced have lots of comments explaining what is being output?
  6. Does each class, and the output, have blank lines and appropriate indenting to make them more readable?
  7. public class Plant
  8. private String name;
  9. private String lifespan;

class Plant{
    String name;
    String lifeSpan;

    //Default Constructor
    public Plant(){

    }

    //Parametrized Constructor
    public Plant(String name,String lifeSpan){
        this.name=name;
        this.lifeSpan=lifeSpan;
    }

    public String getName() {
        return name;
    }

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

    public String getLifeSpan() {
        return lifeSpan;
    }

    public void setLifeSpan(String lifeSpan) {
        this.lifeSpan = lifeSpan;
    }

    public String toString(){
        return "\n\tName:"+name+"\n\tLifeSpan:"+lifeSpan;
    }
}

class Tree extends Plant{
    float height;

    //Default Constructor
    public Tree(){

    }

    //Parametrized Constructor
    public Tree(float height,String name,String lifeSpan){
        super(name,lifeSpan); //Super Class Constructor
        this.height=height;
    }

    public float getHeight() {
        return height;
    }

    public void setHeight(float height) {
        this.height = height;
    }

    public String toString(){
        return "\n\t"+super.toString()+"\n\tHeight:"+height;
    }

}

class Flower extends Plant{
    String color;

    //Default Constructor
    public Flower(){

    }

    //Parametrized Constructor
    public Flower(String color,String name,String lifeSpan){
        super(name,lifeSpan); //Super Class Constructor
        this.color=color;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String toString(){
        return "\n\t"+super.toString()+"\n\tColor:"+color;
    }
}


public class drjava{
    public static void main(String args[]){

        System.out.println("\n\nPlant DETAILS\n");
        Plant plant=new Plant();
        System.out.println("Testing Plant Setter and Getter and toString Methods");
        plant.setName("Rose");
        plant.setLifeSpan("2Years");
        System.out.println("Plant Name:"+plant.getName());
        System.out.println("Plant LifeSpan:"+plant.getLifeSpan());
        System.out.println("Plant toString:"+plant.toString());

        System.out.println("\n\nTREE DETAILS\n");
        Tree tree=new Tree();
        System.out.println("Testing Tree Setter and Getter and toString Methods");
        tree.setName(plant.getName());
        tree.setLifeSpan(plant.getLifeSpan());
        tree.setHeight(3.565f);
        System.out.println("Tree Name:"+tree.getName());
        System.out.println("Tree Height:"+tree.getHeight());
        System.out.println("Tree LifeSpan"+tree.getLifeSpan());
        System.out.println("Tree toString:"+tree.toString());

        System.out.println("\n\nFlower DETAILS\n");
        Flower flower=new Flower();
        System.out.println("Testing Flower Setter and Getter and toString Methods");
        flower.setName("Rose Flower");
        flower.setLifeSpan("2days");
        flower.setColor("Red");
        System.out.println("Flower Name:"+flower.getName());
        System.out.println("Flower Lifespan:"+flower.getLifeSpan());
        System.out.println("Flower Color:"+flower.getColor());
        System.out.println("Flower toString:\n"+flower.toString());
    }
}      

please put each class and driver,thank you!

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

Problem Write in drjava is fine. Using the classes from Assignment #2, do the following: Modify...
Problem Write in drjava is fine. Using the classes from Assignment #2, do the following: Modify the parent class (Plant) by adding the following abstract methods: a method to return the botanical (Latin) name of the plant a method that describes how the plant is used by humans (as food, to build houses, etc) Add a Vegetable class with a flavor variable (sweet, salty, tart, etc) and 2 methods that return the following information: list 2 dishes (meals) that the...
Modify programming problem 4 from Assignment 2. You will create a number of threads—for example, 100—and...
Modify programming problem 4 from Assignment 2. You will create a number of threads—for example, 100—and each thread will request a pid, sleep for a random period of time, and then release the pid. (Sleeping for a random period of time approximates the typical pid usage in which a pid is assigned to a new process, the process executes and then terminates, and the pid is released on the process's termination.) On UNIX and Linux systems, sleeping is accomplished through...
Using Classes This problem relates to the pre-loaded class Person. Using the Person class, write a...
Using Classes This problem relates to the pre-loaded class Person. Using the Person class, write a function print_friend_info(person) which accepts a single argument, of type Person, and: prints out their name prints out their age if the person has any friends, prints 'Friends with {name}' Write a function create_fry() which returns a Person instance representing Fry. Fry is 25 and his full name is 'Philip J. Fry' Write a function make_friends(person_one, person_two) which sets each argument as the friend of...
Need this program Using Classes , Objects, Save to file, and Printbill Simple python assignment Write...
Need this program Using Classes , Objects, Save to file, and Printbill Simple python assignment Write a menu-driven program for Food Court. (You need to use functions!) Display the food menu to a user (Just show the 5 options' names and prices - No need to show the Combos or the details!) Ask the user what he/she wants and how many of it. (Check the user inputs) AND Use strip() function to strip your inputs. Keep asking the user until...
In this assignment will demonstrate your understanding of the following: 1. C++ classes; 2. Implementing a...
In this assignment will demonstrate your understanding of the following: 1. C++ classes; 2. Implementing a class in C++; 3. Operator overloading with chaining; 4. Preprocessor directives #ifndef, #define, and #endif; 5. this – the pointer to the current object. In this assignment you will implement the Date class and test its functionality. Consider the following class declaration for the class date: class Date { public: Date(); //default constructor; sets m=01, d=01, y =0001 Date(unsigned m, unsigned d, unsigned y);...
Using C#, modify the codes below to do the following: Develop a polymorphic banking application using...
Using C#, modify the codes below to do the following: Develop a polymorphic banking application using the Account hierarchy created in the codes below. Create an array of Account references to SavingsAccount and CheckingAccount objects. For each Account in the array, allow the user to specify an amount of money to withdraw from the Account using method Debit and an amount of money to deposit into the Account using method Credit. As you process each Account, determine its type. If...
You will write classes from the last in-class assignment, R&DLibrary. R&DLibrary is the main file in...
You will write classes from the last in-class assignment, R&DLibrary. R&DLibrary is the main file in which a user can choose to enroll themselves, check out an item, return an item, pay a fine and display their account (what is currently checked out, accumulated fines). Write a material class that calculates the fine based off of the type of item, the length it’s been checked out and whether or not it’s even been checked out. Lastly, write a employee Class...
DO IT To receive credit for this assignment, each problem must be computed using a MATLAB...
DO IT To receive credit for this assignment, each problem must be computed using a MATLAB script. Do not hard-code computations. Define variables for the parameters given in the problem, and use MATLAB to calculate necessary intermediate and final output variables. Inlcude the MATLAB Code. A 200-mi. transmission line has the following parameters at 60 Hz: Resistance, r = 0.24 Ω/mi. per phase Series Reactance, x = 0.71 Ω/mi. per phase Shunt Susceptance, b = 5.42 × 10−6 S/mi. per...
Write a method in JAVA to do the following: As the user to select from 2...
Write a method in JAVA to do the following: As the user to select from 2 different (default) pokemon or load their pokemon by giving you the name of their pokemon. Based on their selection (default or supplied name), read the move list and damage range from the input file(you do not need to create this) for the selected pokemon. Randomly select one of the default pokemon - Bulbasaur, Charmander, Squirtle (or you can add additional computer only options if...
DO HARDWORK To receive credit for this assignment, each problem must be computed using a MATLAB...
DO HARDWORK To receive credit for this assignment, each problem must be computed using a MATLAB script. Do not hard-code computations. Define variables for the parameters given in the problem, and use MATLAB to calculate necessary intermediate and final output variables. Inlcude the MATLAB Code. A 200-mi. transmission line has the following parameters at 60 Hz: Resistance, r = 0.24 Ω/mi. per phase Series Reactance, x = 0.71 Ω/mi. per phase Shunt Susceptance, b = 5.42 × 10−6 S/mi. per...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT