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

Now that we’ve helped out our friend and his sister they have invited us over fo

ID: 3793514 • Letter: N

Question

Now that we’ve helped out our friend and his sister they have invited us over for dinner to talk about what improvements can be made. We find out many things about the young boy, we even find out that his name is Alexander and his sister’s name is Elizabeth. Elizabeth tells us a about the day of her accident when she was climbing a tree and a branch broke causing her to fall to the ground giving her these near fatal wounds leaving her bed-ridden. Finally, we start talking about the improvements they would like made to make Elizabeth’s life a little happier.

Alexander finds himself searching through his pack for specific traits of a flower. Some days Elizabeth wants only red flowers or only flowers with thorns as she likes to peel them off. Surely we can help them out!

Create a flower object that has specific traits (name, color, presence of thorns and smell)

These flower objects must be able to stay in his pack (Use an array of length 25)

Be able to add, remove, display and search these flowers – by name (We can drop the sorting for now)

Using the same code as assignment 1 you can make your changes. I have included some base code for your convenience (This is 2 classes, Assignment2 and Flower).

Submit 2 files: Assignment2.java and flower.java

import java.util.Scanner;

public class Assignment2 {

          public static void main(String[] args) {

                   new Assignment2();

          }

          // This will act as our program switchboard

          public Assignment2() {

                   Scanner input = new Scanner(System.in);

                   Flower[] flowerPack = new Flower[25];

                   System.out.println("Welcome to my flower 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 an item to the pack.");

                             System.out.println("2: Remove an item from the pack.");

                             System.out.println("3: Search for a flower.");

                             System.out.println("4: Display the flowers in the pack.");

                             System.out.println("0: Exit the flower pack interfact.");

                             // Get the user input

                             int userChoice = input.nextInt();

                             switch (userChoice) {

                             case 1:

                                      addFlower(flowerPack);

                                      break;

                             case 2:

                                      removeFlower(flowerPack);

                                      break;

                             case 3:

                                      searchFlowers(flowerPack);

                                      break;

                             case 4:

                                      displayFlowers(flowerPack);

                                      break;

                             case 0:

                                      System.out

                                                          .println("Thank you for using the flower pack interface. See you again soon!");

                                      System.exit(0);

                             }

                   }

          }

          private void addFlower(Flower flowerPack[]) {

                   // TODO: Add a flower that is specified by the user

          }

          private void removeFlower(Flower flowerPack[]) {

                   // TODO: Remove a flower that is specified by the user

          }

          private void searchFlowers(Flower flowerPack[]) {

                   // TODO: Search for a user specified flower

          }

          private void displayFlowers(Flower flowerPack[]) {

                   // TODO: Display only the unique flowers along with a count of any

                   // duplicates

                   /*

                   * For example it should say Roses - 7 Daffodils - 3 Violets - 5

                   */

          }

}

// This should be in its own file

public class Flower {

          // Declare attributes here

         

         

          public Flower(){

                  

          }

         

                  

          //Create accessors and mutators for your traits.

}

Explanation / Answer

//File: Assignment2.java

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Assignment2 {

   Scanner input;

   public static void main(String[] args) {

       new Assignment2();

   }

   // This will act as our program switchboard

   public Assignment2() {

       input = new Scanner(System.in);

       Flower[] flowerPack = new Flower[25];

       System.out.println("Welcome to my flower 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 an item to the pack.");

           System.out.println("2: Remove an item from the pack.");

           System.out.println("3: Search for a flower.");

           System.out.println("4: Display the flowers in the pack.");

           System.out.println("0: Exit the flower pack interfact.");

           // Get the user input

           int userChoice = input.nextInt();

           switch (userChoice) {

           case 1:

               addFlower(flowerPack);

               break;

           case 2:

               removeFlower(flowerPack);

               break;

           case 3:

               searchFlowers(flowerPack);

               break;

           case 4:

               displayFlowers(flowerPack);

               break;

           case 0:

               System.out

                       .println("Thank you for using the flower pack interface. See you again soon!");

               System.exit(0);

           }

       }

   }

   private void addFlower(Flower flowerPack[]) {

       input.nextLine(); //Clearing off next line character

       int index;
       boolean spaceAvailable = false;
       for (index = 0; index < flowerPack.length; index++) {
           if (flowerPack[index] == null) { //Check is space is available
               spaceAvailable = true;
               break;
           }
       }

       if (spaceAvailable) { //Accept flower details from user
           System.out.print("Enter name of flower: ");
           String name = input.nextLine();

           System.out.print("Enter color of flower: ");
           String color = input.nextLine();

           System.out.print("Enter if throns are present(Y/N): ");
           boolean throns = (input.nextLine().equalsIgnoreCase("Y")) ? true
                   : false;

           System.out.print("Enter if throns are present(Y/N): ");
           boolean smell = (input.nextLine().equalsIgnoreCase("Y")) ? true
                   : false;

           Flower flower = new Flower(name, color, throns, smell); //Create flower
           flowerPack[index] = flower; //Add flower to pack
           System.out.println("Flower added to pack.. ");

       } else {
           System.out.println("No space available!! ");
       }

   }

   private void removeFlower(Flower flowerPack[]) {
       input.nextLine(); //clearing off next line character
       System.out.print("Enter name of flower to be removed: ");
       String name = input.nextLine(); //Accept user input
       boolean flowerFound = false;

       for (int i = 0; i < flowerPack.length; i++) {
           if (flowerPack[i] != null && flowerPack[i].getName().equals(name)) {
               flowerPack[i] = null; //Remove all matching flowers
               flowerFound = true;
           }
       }

       if (flowerFound)
           System.out.println("Flower removed from pack.. ");
       else
           System.out.println("Flower not found ");
   }

   private void searchFlowers(Flower flowerPack[]) {
       input.nextLine();
       System.out.print("Enter name of flower to be searched: ");
       String name = input.nextLine(); //Accepts user input
       boolean flowerFound = false;

       for (int i = 0; i < flowerPack.length; i++) {
           if (flowerPack[i] != null && flowerPack[i].getName().equals(name)) {
               System.out.println("Name: " + flowerPack[i].getName()); //Display details of all matching flowers
               System.out.println("Color: " + flowerPack[i].getColor());
               System.out.println("Throns present: "
                       + flowerPack[i].isThronPresent());
               System.out.println("Smell present: "
                       + flowerPack[i].isSmellPresent() + " ");
               flowerFound = true;
           }
       }

       if (!flowerFound)
           System.out.println("Flower not found ");

   }

   private void displayFlowers(Flower flowerPack[]) {

       /*
       *
       * For example it should say Roses - 7 Daffodils - 3 Violets - 5
       */
       Map<String, Integer> countMap = new HashMap<String, Integer>();
       for (int i = 0; i < flowerPack.length; i++) {
           if (flowerPack[i] != null) {
               countMap.put(flowerPack[i].getName(),
                       (countMap.get(flowerPack[i].getName()) == null) ? 1
                               : countMap.get(flowerPack[i].getName()) + 1); //Save count of each type of flowers into a map
           }
       }
       for(Map.Entry<String, Integer> entry: countMap.entrySet()){
           System.out.println(entry.getKey() + " - " + entry.getValue()); //Displaying count values
       }
       if(countMap.size() == 0){
           System.out.println("No flower found ");
       } else {
           System.out.println();
       }

   }

}

//File Flower.java

public class Flower {

   private String name;
   private String color;
   private boolean thronPresent;
   private boolean smellPresent;

   public Flower() {

   }


   public Flower(String name, String color, boolean thronPresent,
           boolean smellPresent) { //Constructor
       super();
       this.name = name;
       this.color = color;
       this.thronPresent = thronPresent;
       this.smellPresent = smellPresent;
   }


   public String getName() {
       return name;
   }

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

   public String getColor() {
       return color;
   }

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

   public boolean isThronPresent() {
       return thronPresent;
   }

   public void setThronPresent(boolean thronPresent) {
       this.thronPresent = thronPresent;
   }

   public boolean isSmellPresent() {
       return smellPresent;
   }

   public void setSmellPresent(boolean smellPresent) {
       this.smellPresent = smellPresent;
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote