Question: this is the problem/ someone tried to help me with this already but sent it back with even more errors/ using bluejay After a long
this is the problem/ someone tried to help me with this already but sent it back with even more errors/ using bluejay
After a long and tedious journey of helping Alexander and Elizabeth we feel weve done our part in helping and plan to get on the road in the morning. Our bags were packed and set at the edge of the bed waiting for us to awake, but when we wake up our bags are missing! It seems thieves came in through the open window last night and stole our belongings. Before we can get back on the road we need to gather some money in order to purchase new equipment. To help raise money we have talked to a local shop owner and he mentioned that he liked Alexanders flower pack, but would like it to hold more than just flowers. The new flower pack should hold plants which can be flowers, fungus or weed. Create a plant class that has three child classes (flower, fungus and weed). o Each subclass shares certain qualities (ID and Name) The ID should be something the program creates such as a sequential number; the user should not be asked to indicate the ID o Flower traits include (Color, Thorns, and Smell) o Fungus traits include (Color and Poisonous) o Weed traits include (Color, Poisonous, Edible and Medicinal) All items should be able to be displayed. Be able to add, remove, search and filter these plants by name. For traits that are Yes or No such as Is the Weed Poisonous? consider using Booleans. Dont use a string for all traits. Submit 5 files: Final.java, Flower.java, Fungus.java, Plant.java and Weed.java
THIS IS MY CODE, I HAVE ONE ERROR AND I DONT KNOW WHAT IS WRONG, PLEASE HELP MY PROBLEM IS IN MY FINAL CLASS
FINAL CLASS********************
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Scanner; public class Final {
private Scanner input = new Scanner(System.in); protected ArrayList
public void searchPlant () { System.out.print("To search a plant enter it's name: "); String findPlant = input.next().toUpperCase(); int findHash = findPlant.hashCode(); for (int find = 0 ; find < myBag.size() ; find++) { int myBagHash = myBag.get(find).getPlantName().hashCode(); if (myBagHash == findHash) { System.out.println("Found a " + findPlant); } else System.out.println("A plant by the name, " + findPlant + " doesn't exist in the bag."); } }
public void filterPlant () { System.out.printf("FlowerID%4sFlower Name ",""); for (int i = 0 ; i < myBag.size() ; i++) { System.out.println(myBag.get(i).getPlantID() + "\t" + myBag.get(i).getPlantName() ); } }
public void plantBuilder (Object plant , ArrayList
if (plant instanceof Flower &! (plant instanceof Fungus || plant instanceof Weed)) { Flower flower = (Flower) plant; flower.setPlantID(id); flower.setPlantName(); flower.setPlantColor(); flower.setPlantThorns(); flower.setPlantSmell(); myBag.add(flower); }
if (plant instanceof Fungus &! (plant instanceof Weed)) { Fungus fungus = (Fungus) plant; fungus.setPlantID(id); fungus.setPlantName(); fungus.setPlantColor(); fungus.setIsPoisonous(); myBag.add(fungus); }
if (plant instanceof Weed) { Weed weed = (Weed) plant; weed.setPlantID(id); weed.setPlantName(); weed.setPlantColor(); weed.setIsEdible(); weed.setIsMedicinal(); myBag.add(weed); } } private File filePath; private String filename= "plantData.txt"; public void createPlantFile() { filePath = new File(filename); try { writeToFile = new PrintWriter(filePath); } catch (FileNotFoundException e) { System.out.println("Cannot create file."); } } private PrintWriter writeToFile; public void savePlantFile(ArrayList
Object plant = myBag.get(f); if(plant instanceof Flower &!(plant instanceof Fungus || plant instanceof Weed)) { Flower flower = (Flower) plant; writeToFile.println(flower.getPlantColor() + " " +flower.getPlantThorns() + " " +flower.getPlantThorns()); } else if(plant instanceof Fungus &!(plant instanceof Weed) ) { Fungus fungus = (Fungus) plant; writeToFile.println(fungus.getPlantColor() + " " +fungus.getIsPoisonous() ); } else { Weed weed = (Weed) plant; writeToFile.println(weed.getPlantColor()+ " " + weed.getIsEdible()+ " " + weed.getIsMedicinal() ); } } closePlantFile(); } public void closePlantFile() { writeToFile.close(); } public void loadPlantFile() { filePath = new File(filename); try{ Scanner input = new Scanner(filePath); readPlantFile(input); } catch (FileNotFoundException e) { System.out.println("Unable to access the file "); } }
public void readPlantFile(Scanner input) { try { while (input.hasNextLine()) {
String id = input.next().toUpperCase(); if (id.startsWith("FLOWER") ) { Flower flower = new Flower(); flower.setPlantID(id); flower.setPlantName(input.next()); flower.setPlantColor(input.next()); flower.setPlantThorns(input.next()); flower.setPlantSmell(input.next()); myBag.add(flower); }
if (id.startsWith("FUNGUS")) { Fungus fungus = new Fungus(); fungus.setPlantID(id); fungus.setPlantName(input.next()); fungus.setPlantColor(input.next()); fungus.setIsPoinsonous(input.next()); myBag.add(fungus); }
if (id.startsWith("WEED")) { Weed weed = new Weed(); weed.setPlantID(id); weed.setPlantName(input.next()); weed.setPlantColor(input.next()); weed.setIsEdible(input.next()); weed.setIsMedicinal(input.next()); myBag.add(weed); } } } catch (Exception e) {
System.out.println("End of File "); } } }
FLOWER CLASS******
public class Flower extends Plant { private String plantColor; private String plantSmell; private String plantThorns; public Flower () { this.getPlantColor(); this.getPlantThorns(); this.getPlantSmell(); } public Flower (String c, String t, String s) { plantColor= c; plantThorns= t; plantSmell = s; } public void setPlantColor() { System.out.print("Assign a color: "); this.plantColor = input.next().toUpperCase(); } public void setPlantColor(String color) { this.plantColor= color; } public String getPlantColor() { return plantColor; } @SuppressWarnings("static-access") public void setPlantThorns() { System.out.print("Are there thorns? Yes or No: "); this.plantThorns= input.next().valueOf("YES").toUpperCase(); } public void setPlantThorns(String t) { this.plantThorns= t; } public String getPlantThorns() { return plantThorns; } public void setPlantSmell() { System.out.print("How does the flower smell? "); this.plantSmell= input.next().toUpperCase(); } public void setPlantSmell(String s) { this.plantSmell = s; } public String getPlantSmell() { return plantSmell; } }
WEED CLASS************************************************************************* public class Weed extends Flower { private String isEdible; private String isMedicinal; public Weed () { isEdible = ""; isMedicinal = ""; } public Weed(String e, String m) { isEdible= e; isMedicinal= m; } @SuppressWarnings("static-access") public void setIsEdible() { System.out.print("Is the plant edible, Yes or No? "); this.isEdible = input.next().valueOf("YES").toUpperCase(); } public void setIsEdible(String e) { this.isEdible = e; } public String getIsEdible() { return isEdible; } @SuppressWarnings("static-access") public void setIsMedicinal() { System.out.print("Is there medicinal value, Yes or No? "); this.isMedicinal= input.next().valueOf("YES").toUpperCase(); } public void setIsMedicinal(String m) { this.isMedicinal = m; } public String getIsMedicinal() { return isMedicinal; } }
FUNGUS CLASS***********************************************
public class Fungus extends Flower { private String isPoisonous; public Fungus () { isPoisonous = ""; } public Fungus (String p) { isPoisonous = p; } @SuppressWarnings("static-access") public void setIsPoisonous() { System.out.print("Is the plant poisonous, Yes or NO? "); this.isPoisonous = input.next().valueOf("YES").toUpperCase(); } public void setIsPoinsonous(String p) { this.isPoisonous = p; } public String getIsPoisonous() { return isPoisonous; } }
PLANT CLASS******************************
import java.util.Scanner; public class Plant {
private String plantID; private String plantName; protected Scanner input = new Scanner(System.in); public void setPlantID(int type) { switch (type) { case 1: this.plantID = "FLOWER"; break; case 2: this.plantID = "FUNGUS"; break; case 3: this.plantID = "WEED"; break; } } public void setPlantID(String type) { plantID = type; } public String getPlantID() { return plantID; } public void setPlantName() { System.out.print("Assign a name: "); this.plantName = input.next().toUpperCase(); } public void setPlantName(String name) { plantName = name; } public String getPlantName() { return plantName; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
