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. I would like you to take a look and see if the analysis part of the program is done correctly according to the instructions.
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
Please let me know if it is done correctly, or what the code should look like if it needs work, thank you.
Here is the code I have:
//PlantDriver
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Scanner;
public class PlantDriver {
public static void main(String[] args) throws IOException {
new PlantDriver();
}
public PlantDriver() throws IOException {
Scanner input = new Scanner(System.in);
LinkedList<Plant> plantPack = new
LinkedList<Plant>();
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<Plant> 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<Plant> 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<Plant> 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<Plant> 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<Plant> 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<Plant> 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<Plant> 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<Plant> plantPack)
{
for (Plant thePlant : plantPack) {
System.out.println(thePlant.toString());
}
}
private void filterPlants(LinkedList<Plant> 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<Plant> 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();
}
}
}
//Plant
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;
}
}
//Flower
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
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;
}
}
//Weed
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;
}
}
//Herb
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;
}
}
Screenshots of execution
The code provided looks good to me. Please provide more details of analysis function if any thing specific is required.
Original question: I need a program that simulates a flower pack with the traits listed below:...
I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...
10.16 LAB: Plant information (ArrayList)Given a base Plant class and a derived Flower class, complete main() to create an ArrayList called myGarden. The ArrayList should be able to store objects that belong to the Plant class or the Flower class. Create a method called printArrayList(), that uses the printInfo() methods defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to the myGarden ArrayList, and output each element in myGarden using the printInfo() method.Ex. If the input is:plant Spirea 10 flower Hydrangea 30 false lilac flower Rose 6 false white plant Mint 4...
Please make the following code modular. Therefore contained in a package with several classes and not just one. import java.util.*; public class Q { public static LinkedList<String> dogs = new LinkedList<String> (); public static LinkedList<String> cats = new LinkedList<String> (); public static LinkedList<String> animals = new LinkedList<String> (); public static void enqueueCats(){ System.out.println("Enter name"); Scanner sc=new Scanner(System.in); String name = sc.next(); cats.addLast(name); animals.addLast(name); } ...
java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...
I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student { private String firstName; private String lastName; private String id; private boolean tuitionPaid; public Student(String firstName, String lastName, String id, boolean tuitionPaid) { this.firstName=firstName; this.lastName=lastName; this.id=id; this.tuitionPaid=tuitionPaid; } ...
FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...
After dinner with Alexander and Elizabeth we notice that Alexander is mixing some herbs as a medication for Elizabeth which eases her pain and helps her sleep. Once the medicine is administered we begin small talk with Alexander. During the conversation he tells us the story about how his mother and father were hopelessly in love and were lucky enough to die side by side protecting the things they loved most, their children. In the middle of the story we...
Why is my program returning 0 for the area of a triangle? public class GeometricObjects { private String color = " white "; private boolean filled; private java.util.Date dateCreated; public GeometricObjects() { dateCreated = new java.util.Date(); } public GeometricObjects(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; } public String getColor() { return color; } public void setColor(String color)...
I need to update the code listed below to meet the criteria listed on the bottom. I worked on it a little bit but I could still use some help/corrections! import java.util.Scanner; public class BankAccount { private int accountNo; private double balance; private String lastName; private String firstName; public BankAccount(String lname, String fname, int acctNo) { lastName = lname; firstName = fname; accountNo = acctNo; balance = 0.0; } public void deposit(int acctNo, double amount) { // This method is...
I have a below codes with all details and need to answer this below question ONLY! How to explain below codes with the two properties of a Greedy algorithm - Optimal Substructure and the Greedy Property line by line? ====================== public class TeamFormation { private static ArrayList buildTeam(ArrayList team, int skill) { ArrayList newTeam = new ArrayList(); newTeam.add(skill); for (int player : team) { if (skill == (player + 1)) { newTeam.add(player); } } return newTeam; } private static boolean...