Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

After a long and tedious journey of helping Alexander and Elizabeth we feel we’v

ID: 3857191 • Letter: A

Question

After a long and tedious journey of helping Alexander and Elizabeth we feel we’ve 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 Alexander’s 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. Don’t use a string for all traits. • Submit 5 files: Final.java, Flower.java, Fungus.java, Plant.java and Weed.java No sample code is provided the final.

import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Scanner; public class Bag { // CLASS Objects private Scanner input = new Scanner(System.in); protected ArrayList myBag = new ArrayList(); public static void main (String[] args) { new Bag(); } public Bag() { System.out.println("Plant inventory program, select an option"); while(true) { System.out.println("1: Add plant"); System.out.println("2: Remove plant"); System.out.println("3: Search plant by name"); System.out.println("4: Filter/Print plant by name"); System.out.println("5: Save bag to file: "); System.out.println("6: Load bag from file: "); System.out.println("7: Exit"); int menu = input.nextInt(); switch(menu) { case 1: addPlant(); break; case 2: removePlant(); break; case 3: searchPlant(); break; case 4: filterPlant(); break; case 5: savePlantFile(myBag); break; case 6: loadPlantFile(); break; case 7: System.exit(0); } } } public void addPlant () { System.out.println("Select type of plant"); System.out.println("1: Flower"); System.out.println("2: Fungus"); System.out.println("3: Weed"); int id = input.nextInt(); switch(id) { case 1: // send a new instance of Flower() to the generic Object argument. this.plantBuilder(new Flower(), myBag, id); break; case 2: this.plantBuilder(new Fungus(), myBag, id); break; case 3: this.plantBuilder(new Weed(), myBag, id); break; } } public void removePlant () { System.out.print("To delete a plant enter it's name: "); String deletePlant = input.next().toUpperCase(); // User selection int toDeleteHash = deletePlant.hashCode(); // generate hashcode from user word for (int index = 0 ; index < myBag.size() ; index++) { // start loop through arrayList int myBagHash = myBag.get(index).getPlantName().hashCode(); // generate hashcode from arraylist if (myBagHash == toDeleteHash) { // if equal myBag.remove(index); // delete break; } else // equal not found System.out.println("Sorry, no plant by the name " + deletePlant + " was found, Try again. "); } } /* * Search by name */ 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."); } } /* * Filter by name */ public void filterPlant () { System.out.printf("FlowerID%4sFlower Name ",""); for (int i = 0 ; i < myBag.size() ; i++) { System.out.println(myBag.get(i).getPlantID() + " " + myBag.get(i).getPlantName() ); } } /* * If I'm understanding this correctly. Plant is the superclass. Because of its relationship * to subclasses, I defined the input "plant" as a generic type "Object" as opposed to Flower, etc. * when this method is called, the actual class Object is passed as the argument (Either new Flower() * , new Fungus() or new Weed() because Object plant can be any Object. Then the local variable 'plant' * is checked to see what Object was passed to it by the IF statements. */ public void plantBuilder (Object plant , ArrayList myBag, int id) { // instance of Flower object only if (plant instanceof Flower &! (plant instanceof Fungus || plant instanceof Weed)) { Flower flower = (Flower) plant; // Declaring Flower flower as an instance of plant(Flower()) flower.setPlantID(id); flower.setPlantName(); flower.setPlantColor(); // Now, Flower() methods are available for use here flower.setPlantThorns(); // here flower.setPlantSmell(); // and here myBag.add(flower); // with flower object built, send to myBag (ArrayList) } // instance of Fungus object only if (plant instanceof Fungus &! (plant instanceof Weed)) { Fungus fungus = (Fungus) plant; fungus.setPlantID(id); fungus.setPlantName(); fungus.setPlantColor(); fungus.setIsPoisonous(); myBag.add(fungus); } // instance of Weed object only if (plant instanceof Weed) { Weed weed = (Weed) plant; weed.setPlantID(id); weed.setPlantName(); weed.setPlantColor(); weed.setIsEdible(); weed.setIsMedicinal(); myBag.add(weed); } } // Class variables near the utilizing methods 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 myBag ) { createPlantFile(); for (int f = 0 ; f < myBag.size() ; f++) { writeToFile.print(myBag.get(f).getPlantID() + " " + myBag.get(f).getPlantName() + " "); 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(); // Need this, otherwise file is unsaved } 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()) /*throws NoSuchElementException Error */ { /* * This is because the savePlantFile method performs a carriage return after writing * each row and the final entry is inevitably an empty line */ // scan first word (ID) 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); } // Color, Poison, Medicinal 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) { /* * So, catch the exception here with a nice little message */ System.out.println("End of File "); } } } Flower.java 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; } } Fungus.java 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.java import java.util.Scanner; public class Plant { // CLASS variables private String plantID; private String plantName; protected Scanner input = new Scanner(System.in); /* * Plant ID is straight forward, it is one of three options: Flower, Fungus, or Weed */ 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(); } // Overload for reading file input public void setPlantName(String name) { plantName = name; } public String getPlantName() { return plantName; } } Weed.java 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; } }

Why is the bag.java code not working?

Explanation / Answer

There was a minor mistake.

The issue was readPlantFile() function was throwing this error: -

This happens when there is no valid check done on scanner object . i.e. there must be hasNext() or hasNextLine() check before invoking next() method. Now the issue was in your code, you were using input.next() method to get the next value, but in if condition, you have used hasNextLine() check. So there is inconsistency.

So we need to have hasNext() check for using next() function and hasNextLine() for nextLine() function.

I have modified your code as below. Only Bag.java is changed.

package time;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.Scanner;

public class Bag {

// CLASS Objects

private Scanner input = new Scanner(System.in);

protected ArrayList myBag = new ArrayList();

public static void main(String[] args) {

new Bag();

}

public Bag() {

System.out.println("Plant inventory program, select an option");

while (true) {

System.out.println("1: Add plant");

System.out.println("2: Remove plant");

System.out.println("3: Search plant by name");

System.out.println("4: Filter/Print plant by name");

System.out.println("5: Save bag to file: ");

System.out.println("6: Load bag from file: ");

System.out.println("7: Exit");

int menu = input.nextInt();

switch (menu) {

case 1:

addPlant();

break;

case 2:

removePlant();

break;

case 3:

searchPlant();

break;

case 4:

filterPlant();

break;

case 5:

savePlantFile(myBag);

break;

case 6:

loadPlantFile();

break;

case 7:

System.exit(0);

}

}

}

public void addPlant() {

System.out.println("Select type of plant");

System.out.println("1: Flower");

System.out.println("2: Fungus");

System.out.println("3: Weed");

int id = input.nextInt();

switch (id) {

case 1:

// send a new instance of Flower() to the generic Object argument.

this.plantBuilder(new Flower(), myBag, id);

break;

case 2:

this.plantBuilder(new Fungus(), myBag, id);

break;

case 3:

this.plantBuilder(new Weed(), myBag, id);

break;

}

}

public void removePlant() {

System.out.print("To delete a plant enter it's name: ");

String deletePlant = input.next().toUpperCase();

// User selection

int toDeleteHash = deletePlant.hashCode();

// generate hashcode from user word

for (int index = 0; index < myBag.size(); index++) {

// start loop through arrayList

int myBagHash = ((Plant) myBag.get(index)).getPlantName().hashCode();

// generate hashcode from arraylist

if (myBagHash == toDeleteHash) {

// if equal

myBag.remove(index); // delete

break;

} else

// equal not found

System.out.println("Sorry, no plant by the name " + deletePlant + " was found, Try again. ");

}

}

/* * Search by name */

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 = ((Plant) 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.");

}

}

/* * Filter by name */

public void filterPlant() {

System.out.printf("FlowerID%4sFlower Name ", "");

for (int i = 0; i < myBag.size(); i++) {

System.out.println(((Plant) myBag.get(i)).getPlantID() + " " + ((Plant) myBag.get(i)).getPlantName());

}

}

/*

* * If I'm understanding this correctly. Plant is the superclass. Because

* of its relationship * to subclasses, I defined the input "plant" as a

* generic type "Object" as opposed to Flower, etc. * when this method is

* called, the actual class Object is passed as the argument (Either new

* Flower() * , new Fungus() or new Weed() because Object plant can be any

* Object. Then the local variable 'plant' * is checked to see what Object

* was passed to it by the IF statements.

*/

public void plantBuilder(Object plant, ArrayList myBag, int id) {

// instance of Flower object only

if (plant instanceof Flower & !(plant instanceof Fungus || plant instanceof Weed)) {

Flower flower = (Flower) plant;

// Declaring Flower flower as an instance of plant(Flower())

flower.setPlantID(id);

flower.setPlantName();

flower.setPlantColor();

// Now, Flower() methods are available for use here

flower.setPlantThorns(); // here

flower.setPlantSmell(); // and here

myBag.add(flower); // with flower object built, send to myBag

// (ArrayList)

}

// instance of Fungus object only

if (plant instanceof Fungus & !(plant instanceof Weed)) {

Fungus fungus = (Fungus) plant;

fungus.setPlantID(id);

fungus.setPlantName();

fungus.setPlantColor();

fungus.setIsPoisonous();

myBag.add(fungus);

}

// instance of Weed object only

if (plant instanceof Weed) {

Weed weed = (Weed) plant;

weed.setPlantID(id);

weed.setPlantName();

weed.setPlantColor();

weed.setIsEdible();

weed.setIsMedicinal();

myBag.add(weed);

}

}

// Class variables near the utilizing methods

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 myBag) {

createPlantFile();

for (int f = 0; f < myBag.size(); f++) {

writeToFile.print(((Plant) myBag.get(f)).getPlantID() + " " + ((Plant) myBag.get(f)).getPlantName() + " ");

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();

// Need this, otherwise file is unsaved

}

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.hasNext()) /* throws NoSuchElementException Error */

{

/*

* * This is because the savePlantFile method performs a

* carriage return after writing * each row and the final entry

* is inevitably an empty line

*/ // scan first word (ID)

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);

}

// Color, Poison, Medicinal

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) {

/* * So, catch the exception here with a nice little message */

//e.printStackTrace();

System.out.println("End of File ");

}

}

}

Sample Run: -

Plant inventory program, select an option

1: Add plant

2: Remove plant

3: Search plant by name

4: Filter/Print plant by name

5: Save bag to file:

6: Load bag from file:

7: Exit

3

To search a plant enter it's name: ASD

1: Add plant

2: Remove plant

3: Search plant by name

4: Filter/Print plant by name

5: Save bag to file:

6: Load bag from file:

7: Exit

4

FlowerID Flower Name

1: Add plant

2: Remove plant

3: Search plant by name

4: Filter/Print plant by name

5: Save bag to file:

6: Load bag from file:

7: Exit

6

1: Add plant

2: Remove plant

3: Search plant by name

4: Filter/Print plant by name

5: Save bag to file:

6: Load bag from file:

7: Exit

4

FlowerID Flower Name

FLOWER ASD

FUNGUS AS

WEED WEE

1: Add plant

2: Remove plant

3: Search plant by name

4: Filter/Print plant by name

5: Save bag to file:

6: Load bag from file:

7: Exit

3

To search a plant enter it's name: ASD

Found a ASD

A plant by the name, ASD doesn't exist in the bag.

A plant by the name, ASD doesn't exist in the bag.

1: Add plant

2: Remove plant

3: Search plant by name

4: Filter/Print plant by name

5: Save bag to file:

6: Load bag from file:

7: Exit

2

To delete a plant enter it's name: ASD

1: Add plant

2: Remove plant

3: Search plant by name

4: Filter/Print plant by name

5: Save bag to file:

6: Load bag from file:

7: Exit

4

FlowerID Flower Name

FUNGUS AS

WEED WEE

1: Add plant

2: Remove plant

3: Search plant by name

4: Filter/Print plant by name

5: Save bag to file:

6: Load bag from file:

7: Exit

5

1: Add plant

2: Remove plant

3: Search plant by name

4: Filter/Print plant by name

5: Save bag to file:

6: Load bag from file:

7: Exit

Note that you can always check what exception is thrown in catch by placing e.printStackTrace() method and solve accordingly.

Let me know in comments if you still face any issue.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote