Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Original question: I need a program that simulates a flower pack with the traits listed below: - You must use a LinkedList for storage (not

Original question:

I need a program that simulates a flower pack with the traits listed below:

- You must use a LinkedList for storage (not an Arrray list).

- You must be able to display, add, remove, sort, filter, and analyze information from our flower pack.

- The flower pack should hold flowers, weeds, fungus, and herbs. All should be a subclass of a plant class.

- Each subclass shares certain qualities (ID, Name, and Color) plant class

- Flower traits include (Thorns, and Smell)

- Fungus traits include (Poisonous)

- Weed traits include (Poisonous, Edible and Medicinal)

- Herb traits include (Flavor, Medicinal, Seasonal)

No GUI is required.

The code I have is listed below, it appears to do just about everything I need it to. The one part of the instructions I am unclear on is when it says we need to be able to analyze information from our flower pack.

Here are the instructions regarding analyzing information from our flower pack:

Analysis can be case sensitive, if you so desire. Remember - you MUST use recursion to solve this problem. Meaning not a single loop should be called when doing these calculations. You CAN use a loop when you want to move from analyzing one flower to the next, but your loop CANNOT be used when analyzing a specific flower.

Analysis Examples:

- How you display the results is up to you

- Analyze 3 different strings such as ar, ne, and um which strings is up to you and does not require user input

How do I modify my code to analyze information from the flower pack according to the above instructions (with use of recursion)? Thank you.

Here is the code I have:

PlantDriver.java ----------------------------

import java.util.*; import java.io.*; public class PlantDriver { public static void main(String[] args) throws IOException { new PlantDriver(); } public PlantDriver() throws IOException { Scanner input = new Scanner(System.in); LinkedList plantPack = new LinkedList(); System.out.println("Welcome to my Plant pack interface."); System.out.println("Please select a number from the options below"); System.out.println(""); while (true) { // Give the user a list of their options System.out.println("1: Add a plant item to the pack."); System.out.println("2: Add a flower item to the pack."); System.out.println("3: Add a fungus item to the pack."); System.out.println("4: Add a weed item to the pack."); System.out.println("5: Add a herb item to the pack."); System.out.println("6: Remove a plant item (regardless of its type)from the pack."); System.out.println("7: Search for a plant by name."); System.out.println("8: Display the plants in the pack."); System.out.println("9: Filter plant pack by incomplete name"); System.out.println("10: Print Plants from the pack to a file!"); System.out.println("11: Read Plants from a file that is already created and output them"); System.out.println("0: Exit the flower pack interface."); // Get the user input int userChoice = input.nextInt(); switch (userChoice) { case 1: addPlant(plantPack); break; case 2: addFlower(plantPack); break; case 3: addFungus(plantPack); break; case 4: addWeed(plantPack); break; case 5: addHerb(plantPack); break; case 6: removePlant(plantPack); break; case 7: searchPlants(plantPack); break; case 8: displayPlants(plantPack); break; case 9: filterPlants(plantPack); break; case 10: savePlantsToFile(plantPack); break; case 11: readPlantsFromFile(); break; case 0: System.out.println("Thank you for using the plant pack interface. See you again soon!"); System.exit(0); } } } private void addPlant(LinkedList plantPack) { //Add a plant that is specified by the user String plantName; String plantColor; String plantID; Scanner plantInput = new Scanner(System.in); System.out.println("Please enter the name of a plant type to add:"); plantName = plantInput.nextLine(); System.out.println("What is the color of the plant you would like to add?: "); plantColor = plantInput.nextLine(); System.out.println("What is the ID of the plant?: "); plantID = plantInput.nextLine(); Plant thePlant = new Plant(plantColor, plantID, plantName); plantPack.add(thePlant); } private void addFlower(LinkedList plantPack) { //Add a plant that is specified by the user String flowerName; String flowerColor; String flowerID; String scentType; String isItThorny; boolean isThorny; Scanner flowerInput = new Scanner(System.in); System.out.println("Please enter the name of a flower type to add:"); flowerName = flowerInput.nextLine(); System.out.println("What is the color of the flower you would like to add?: "); flowerColor = flowerInput.nextLine(); System.out.println("What is the ID of the flower?: "); flowerID = flowerInput.nextLine(); System.out.println("Is the flower a thorny kind of flower? (Please answer yes or no with y or n only"); isItThorny = flowerInput.nextLine(); if (isItThorny.equalsIgnoreCase("y")) { isThorny = true; } else { isThorny = false; } System.out.println("Please describe the scent of the flower: "); scentType = flowerInput.nextLine(); Flower theFlower = new Flower(flowerColor, flowerID, flowerName, scentType, isThorny); plantPack.add(theFlower); } private void addFungus(LinkedList plantPack) { //Add a plant that is specified by the user String fungusName; String fungusColor; String fungusID; String isItPoisnous; boolean isPoisonous; Scanner fungusInput = new Scanner(System.in); System.out.println("Please enter the name of a fungus type to add:"); fungusName = fungusInput.nextLine(); System.out.println("What is the color of the fungus you would like to add?: "); fungusColor = fungusInput.nextLine(); System.out.println("What is the ID of the fungus?: "); fungusID = fungusInput.nextLine(); System.out.println("Is the fungus a poisonous kind of fungus? (Please answer yes or no with y or n only"); isItPoisnous = fungusInput.nextLine(); if (isItPoisnous.equalsIgnoreCase("y")) { isPoisonous = true; } else { isPoisonous = false; } Fungus newFungus = new Fungus(fungusColor, fungusID, fungusName, isPoisonous); plantPack.add(newFungus); } private void addWeed(LinkedList plantPack) { //Add a plant that is specified by the user String weedName; String weedColor; String weedID; String isItEdible; boolean isEdible; String isItMedicinal; boolean isMedicinal; String isItPoisnous; boolean isPoisonous; Scanner weedInput = new Scanner(System.in); System.out.println("Please enter the name of a weed type to add:"); weedName = weedInput.nextLine(); System.out.println("What is the color of the weed you would like to add?: "); weedColor = weedInput.nextLine(); System.out.println("What is the ID of the weed?: "); weedID = weedInput.nextLine(); System.out.println("Is the weed an edible kind of weed? (Please answer yes or no with y or n only"); isItEdible = weedInput.nextLine(); if (isItEdible.equalsIgnoreCase("y")) { isEdible = true; } else { isEdible = false; } System.out.println("Is the weed a medicinal kind of weed? (Please answer yes or no with y or n only"); isItMedicinal = weedInput.nextLine(); if (isItMedicinal.equalsIgnoreCase("y")) { isMedicinal = true; } else { isMedicinal = false; } System.out.println("Is the weed a poisonous kind of weed? (Please answer yes or no with y or n only"); isItPoisnous = weedInput.nextLine(); if (isItPoisnous.equalsIgnoreCase("y")) { isPoisonous = true; } else { isPoisonous = false; } Plant thePlant = new Weed(weedColor, weedID, weedName, isEdible, isMedicinal, isPoisonous); plantPack.add(thePlant); } private void addHerb(LinkedList plantPack) { //Add a plant that is specified by the user String herbName; String herbColor; String herbID; String herbFlavor; String isItMedicinal; boolean isMedicinal; String isItSeasonal; boolean isSeasonal; Scanner herbInput = new Scanner(System.in); System.out.println("Please enter the name of a herb type to add:"); herbName = herbInput.nextLine(); System.out.println("What is the color of the herb you would like to add?: "); herbColor = herbInput.nextLine(); System.out.println("What is the ID of the herb?: "); herbID = herbInput.nextLine(); System.out.println("What is the flavor of the herb?: "); herbFlavor = herbInput.nextLine(); System.out.println("Is the herb a medicinal kind of herb? (Please answer yes or no with y or n only"); isItMedicinal = herbInput.nextLine(); if (isItMedicinal.equalsIgnoreCase("y")) { isMedicinal = true; } else { isMedicinal = false; } System.out.println("Is the herb a seasonal herb? (Please answer yes or no with y or n only"); isItSeasonal = herbInput.nextLine(); if (isItSeasonal.equalsIgnoreCase("y")) { isSeasonal = true; } else { isSeasonal = false; } Plant thePlant = new Herb(herbColor, herbID, herbName, herbFlavor, isMedicinal, isSeasonal); plantPack.add(thePlant); } private void removePlant(LinkedList plantPack) { // Remove a plant that is specified by the user String plantName; Scanner plantInput = new Scanner(System.in); System.out.println("Please enter the name of the plant (regardless of the type (e.g. Plant, Flower, Fungus, Weed) that you would like to remove: "); plantName = plantInput.nextLine(); for (Plant thePlant : plantPack) { if (thePlant.getName().equals(plantName)) { plantPack.remove(thePlant); } }

} private void searchPlants(LinkedList plantPack) { String plantName; Scanner plantInput = new Scanner(System.in); System.out.println("Please enter the name of the plant (regardless of the type (e.g. Plant, Flower, Fungus, Weed, Herb) that you would like to search: "); plantName = plantInput.nextLine(); int index = -1; for (int i = 0; i < plantPack.size(); i++) { //done in O(n) time This is a linear search if (plantName.equalsIgnoreCase(plantPack.get(i).getName())) { index = i; break; } } if (index != -1) { System.out.println("The search element : " + plantName + " was found"); } else { System.out.println("The search element was not found in the LinkedList."); } } private void displayPlants(LinkedList plantPack) { for (Plant thePlant : plantPack) { System.out.println(thePlant.toString()); } } private void filterPlants (LinkedList plantPack) { // TODO Filter plant String plantName; Scanner plantInput = new Scanner(System.in); System.out.println("Enter the name of a plant to search by first character?"); plantName = plantInput.nextLine(); for (Plant thePlant : plantPack) { if(thePlant.getName().charAt(0) == plantName.charAt(0)) { System.out.println("Flowers based on the first character: " + thePlant.toString()); } } } private void savePlantsToFile (LinkedList plantPack) throws IOException { File plantFile = new File("plantFile.txt"); FileOutputStream plantStream = new FileOutputStream(plantFile); PrintWriter plantOutStream = new PrintWriter(plantStream); for (Plant thePlant : plantPack) { plantOutStream.println(thePlant.toString()); } plantOutStream.close(); } private void readPlantsFromFile() throws FileNotFoundException { Scanner plantInput = new Scanner (new File ("plantInputData.txt")); try { while (plantInput.hasNext()) { Plant newPlant = new Plant(plantInput.next(), plantInput.next(), plantInput.next()); System.out.println(newPlant.toString()); } plantInput.close(); } catch (Exception e) { e.printStackTrace(); } } }

------------------------------------------------------------ Flower.java ------------------------------------ public class Flower extends Plant { private boolean hasThorns; private String smell;

public Flower(String flowerColor, String flowerID, String flowerName, String smell, boolean hasThorns) { super(flowerColor, flowerID, flowerName); this.hasThorns = hasThorns; this.smell = smell; }

public void setSmell(String flowerSmell) { this.smell = flowerSmell; }

public void setThorns(boolean isThorny) { this.hasThorns = isThorny; }

public String getSmell() { return smell; }

public boolean getIsThorny() { return hasThorns; }

public String toString() { return "This flower is colored: " + super.getColor() + " with a name of " + super.getName() + " and an ID of " + super.getID() + " the smell is " + this.smell + " and it is thorny = " + this.hasThorns; }

@Override public boolean equals(Object otherFlower) { if (otherFlower == null) { return false; }

if (!Flower.class.isAssignableFrom(otherFlower.getClass())) { return false; }

Flower other = (Flower) otherFlower; if (!(this.getName().equals(other.getName()))) { return false; }

if (!(this.getColor().equals(other.getColor()))) { return false; }

if ((this.getSmell() != (other.getSmell()))) { return false; }

if (!(this.getID().equals(other.getID()))) { return false; }

if(this.getIsThorny() != other.getIsThorny()) { return false; }

return true; } } ------------------------------------------------------------ Fungus.java ------------------------------------ public class Fungus extends Plant{

private boolean isPoisonous;

public Fungus(String fungusColor, String fungusID, String fungusName, boolean isPoisonous) { super(fungusColor, fungusID, fungusName); this.isPoisonous = isPoisonous; }

public void setIsPoisonous(boolean isPoisonous) { this.isPoisonous = isPoisonous; }

public boolean getIsPoisonous(){ return isPoisonous; }

public String toString() { return "This fungus as sampled is " + this.getColor() + " in color " + " with an ID of " + this.getID() + " and a name of " + this.getName() + " and it's poisonous = " + this.getIsPoisonous(); }

@Override public boolean equals(Object otherFungus) {

if (otherFungus == null) { return false; } if (!Fungus.class.isAssignableFrom(otherFungus.getClass())) { return false; }

Fungus other = (Fungus) otherFungus;

if (!(this.getColor().equals(other.getColor()))) { return false; }

if (!(this.getID().equals(other.getID()))) { return false;

}

if (!(this.getName().equals(other.getName()))) { return false; }

if (this.isPoisonous != other.isPoisonous) { return false; }

return true; }

} ------------------------------------------------------------ Herb.java ------------------------------------ public class Herb extends Plant{ private String flavor; private boolean isMedicinal, isSeasonal;

public Herb(String herbColor, String herbID, String herbName, String flavor, boolean isMedicinal, boolean isSeasonal) { super(herbColor, herbID, herbName); this.flavor = flavor; this.isMedicinal = isMedicinal; this.isSeasonal = isSeasonal; }

public void setHerbFlavor(String herbFlavor) { this.flavor = herbFlavor; }

public String getHerbFlavor() { return flavor; }

public void setMedicinalProperty(boolean isMedicinal) { this.isMedicinal = isMedicinal; }

public boolean getMedicinalProperty() { return isMedicinal; }

public void setSeasonalProperty(boolean isSeasonal) { this.isSeasonal = isSeasonal; }

public boolean getSeasonalProperty() { return isSeasonal; }

public String toString() { return "This herb is of " + this.getColor() + " color and is called " + this.getName() + " with an ID of " + this.getID() + " with a " + this.getHerbFlavor() + " flavor and " + " with a seasonal property of " + this.getSeasonalProperty() + " and a Medicinal Property of " + this.getMedicinalProperty(); }

@Override public boolean equals(Object otherHerb) { if(otherHerb == null) { return false; }

if(!Herb.class.isAssignableFrom(otherHerb.getClass())) { return false; }

Herb other = (Herb) otherHerb;

if (!this.flavor.equals(other.flavor)) { return false; }

if (!this.getColor().equals(other.getColor())) { return false; }

if (!this.getID().equals(other.getID())) { return false; }

if (!this.getName().equals(other.getName())) { return false; }

if (this.isMedicinal != other.isMedicinal) { return false; }

if (this.isSeasonal != other.isSeasonal) { return false; }

return true;

} } ------------------------------------------------------------ Plant.java ------------------------------------ import java.util.*;

public class Plant {

private String id; private String name; private String color;

public Plant() { this.id = ""; this.name = ""; this.color = ""; }

public Plant(String plantColor, String plantID, String plantName) { this.id = plantID; this.color = plantColor; this.name = plantName; }

public void setID(String plantID) { this.id = plantID; }

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

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

public String getName() { return name; }

public String getColor() { return color; }

public String getID() { return id; }

public String toString() { return "This plant's name is " + this.getName() + " with a color of: " + this.getColor() + " with a unique ID of: " + this.getID(); }

@Override public boolean equals(Object otherPlant) { if (otherPlant == null) { return false; }

if (!Plant.class.isAssignableFrom(otherPlant.getClass())) { return false; }

final Plant other = (Plant) otherPlant;

if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) { return false; } if (!(this.color.equals(other.color))) { return false; }

if (!(this.id.equals(other.id))) { return false; }

return true; }

} ------------------------------------------------------------ Weed.java ------------------------------------ import java.util.*; public class Weed extends Plant { private boolean isEdible, isMedicinal, isPoisonous;

public Weed(String weedColor, String weedID, String weedName, boolean isEdible, boolean isMedicinal, boolean isPoisonous) { super(weedColor, weedID, weedName); this.isEdible = isEdible; this.isMedicinal = isMedicinal; this.isPoisonous = isPoisonous; }

public void setIsEdible(boolean isEdible) { this.isEdible = isEdible; }

public boolean getIsEdible() { return isEdible; }

public void setIsMedicinal(boolean isMedicinal) { this.isMedicinal = isMedicinal; }

public boolean getIsMedicinal() { return isMedicinal; }

public void setIsPoisonous(boolean isPoisonous) { this.isPoisonous = isPoisonous; }

public boolean getIsPoisonous() { return isPoisonous; }

public String toString() { return "This weed is of " + this.getColor() + " color and is called " + this.getName() + " with an ID of " + this.getID() + " it is edible = " + this.getIsEdible() + " and it is Poisonous " + this.getIsPoisonous() + " and it is medicinal " + this.getIsMedicinal(); }

@Override public boolean equals(Object otherWeed) { if (otherWeed == null) { return false; }

if (!(Weed.class.isAssignableFrom(otherWeed.getClass()))) { return false; }

Weed other = (Weed) otherWeed;

if (!(this.getID().equals(other.getID()))) { return false; }

if (!(this.getName().equals(other.getName()))) { return false; }

if (!(this.getColor().equals(other.getColor()))) { return false; }

if (this.isEdible != other.isEdible) { return false; }

if (this.isMedicinal != other.isMedicinal) { return false; }

if (this.isPoisonous != other.isPoisonous) { return false; }

return true;

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions

Question

What is the purchasing power parity exchange rate?

Answered: 1 week ago