Question

In: Computer Science

As I was on a hike the other day I came across a small child in...

As I was on a hike the other day I came across a small child in the woods. He told me his life story, with special mention of his disabled sister who loves flowers, and asked me for a favor. He wanted a way to organize the flowers that he picks for her each day and perform a few basic tasks with them, along with a few restrictions. It is our goal to help him out! • He can only carry 25 flowers as adding any more causes many of them to become crushed. • All flowers should be able to be displayed • He should be able to add and remove flowers by using their name. • He needs to be able to search for a specific type of flower in his pack in case his sister has a special request. • He needs to be able to sort flowers by their names alphabetically in ascending order (A-Z) • He needs to show how many of each flower he has in his pack. Now, I have started a simple program which will serve as guidance for you, please help me finish it. (Please don’t modify the code that I give you, just add your code where required) Submit 1 file: Assignment1.java import java.util.Scanner; public class Assignment01Driver { public static void main(String[] args){ new Assignment01Driver (); } // This will act as our program switchboard public Assignment01Driver (){ Scanner input = new Scanner(System.in); String[] flowerPack = new String[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: Sort the contents of the pack."); System.out.println("4: Search for a flower."); System.out.println("5: 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: sortFlowers(flowerPack); break; case 4: searchFlowers(flowerPack); break; case 5: 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(String flowerPack[]) { // TODO: Add a flower that is specified by the user } private void removeFlower(String flowerPack[]) { // TODO: Remove a flower that is specified by the user } private void sortFlowers(String flowerPack[]) { // TODO: Sort the flowers in the pack (No need to display them here) - Use Selection or Insertion sorts // NOTE: Special care is needed when dealing with strings! research the compareTo() method with strings } private void searchFlowers(String flowerPack[]) { // TODO: Search for a user specified flower } private void displayFlowers(String 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 */ } }

Solutions

Expert Solution

Assignment01Driver.java

==================================================
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Assignment01Driver {

   static int flowerscount = 0;
   Scanner input = new Scanner(System.in);

   public Assignment01Driver() {

       String[] flowerPack = new String[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: Sort the contents of the pack.");
           System.out.println("4: Search for a flower.");
           System.out.println("5: 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:
               sortFlowers(flowerPack);
               break;
           case 4:
               searchFlowers(flowerPack);
               break;
           case 5:
               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(String flowerPack[]) {
       // TODO: Add a flower that is specified by the user

       if (flowerscount >= 25)
           System.out.println("You have already Added 25 flowers, you cant add more");

       else {

           System.out.println("Enter the flower name to add\n");
           input.nextLine();
           String flower = input.nextLine();
           flowerPack[flowerscount++] = flower;

       }

   }

   private void removeFlower(String flowerPack[]) { // TODO: Remove a flower
                                                       // that is specified by
                                                       // the user

       System.out.println("Enter the flower name to remove\n");
       input.nextLine();
       String flower = input.nextLine();
       List<String> list = new ArrayList<String>();
       Collections.addAll(list, flowerPack);
       if (list.remove(flower)) {
           System.out.println("Flower with name " + flower + " Removed");
           --flowerscount;
       }
       flowerPack = list.toArray(flowerPack);

   }

   private void sortFlowers(String flowerPack[]) {
       // TODO: Sort the flowers in the pack (No need to display them here) -
       // Use Selection or Insertion sorts // NOTE: Special care is needed when
       // dealing with strings! research the compareTo() method with strings
       String key = "";
       int i = 0;
       // System.out.println(Arrays.toString(flowerPack));
       for (int j = 1; j < flowerscount; j++) { // the condition has changed
           key = flowerPack[j];
           i = j - 1;
           while (i >= 0) {
               if (key.compareTo(flowerPack[i]) > 0) {// here too
                   break;
               }
               flowerPack[i + 1] = flowerPack[i];
               i--;
           }
           flowerPack[i + 1] = key;
           // System.out.println(Arrays.toString(flowerPack));
       }
       System.out.println("Flowers are Sorted: ");
       for (int k = 0; k < flowerscount; ++k)
           System.out.print(flowerPack[k] + " ");
       System.out.println();

   }

   private void searchFlowers(String flowerPack[]) {
       // TODO: Search for a user specified flower
       System.out.println("Enter the flower name to search\n");
       input.nextLine();
       String flower = input.nextLine();
       boolean flag = false;
       for (int i = 0; i < flowerPack.length; ++i) {
           if (flower.equals(flowerPack[i])) {
               System.out.println("Flower with name " + flower + " Found");
               flag = true;
               break;
           }
       }
       if (!flag)
           System.out.println("Not found!!");
   }

   private void displayFlowers(String 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
       */

       HashMap<String, Integer> map = new HashMap<String, Integer>();
       System.out.println("Flowers are as follows: ");
       for (int i = 0; i < flowerPack.length; ++i) {
           if (flowerPack[i] != null) {
               if (map.get(flowerPack[i]) == null) {
                   map.put(flowerPack[i], 1);
               } else {
                   Integer val = map.get(flowerPack[i]);
                   map.put(flowerPack[i], val + 1);

               }
           }

       }
       Iterator it = map.entrySet().iterator();
       while (it.hasNext()) {
           Map.Entry pair = (Map.Entry) it.next();
           // System.out.println(pair.getKey() + " = " + pair.getValue());
           if ((int) pair.getValue() == 1)
               System.out.print(pair.getKey() + " ");
           else
               System.out.print(pair.getKey() + "-" + pair.getValue() + " ");
           it.remove();
       }
       System.out.println();
   }

   public static void main(String[] args) {
       new Assignment01Driver();

   }
}
====================================================================================
Adding Output Image , Let me know if there is any concern.

Thanks, Let me know if there is any concern.


Related Solutions

As I was on a hike the other day I came across a small child in...
As I was on a hike the other day I came across a small child in the woods. He told me his life story, with special mention of his disabled sister who loves flowers, and asked me for a favor. He wanted a way to organize the flowers that he picks for her each day and perform a few basic tasks with them, along with a few restrictions. It is our goal to help him out! He can only carry...
On your way for lectures one day, you came across an accident scene where the victim...
On your way for lectures one day, you came across an accident scene where the victim was bleeding profusely. You happened to be the first person at the scene so you assisted the victim to a nearby health centre. When you entered the yard of the health facility you noticed that the compound was very neat with beautiful flowers and modern structures. At the emergency ward was a nurse who was busily having a phone conversation with a supposed friend....
CASE STUDY On your way for lectures one day, you came across an accident scene where...
CASE STUDY On your way for lectures one day, you came across an accident scene where the victim was bleeding profusely. You happened to be the first person at the scene so you assisted the victim to a nearby health centre. When you entered a yard of the health facility you noticed that the compound was very neat with beautiful flowers and modern structures. At the emergency ward was a nurse who was busily having a phone conversation with a...
Question 2 You are out exploring plants while on a hike, and you come across a...
Question 2 You are out exploring plants while on a hike, and you come across a group of ferns. You notice some with hairy fiddleheads (immature leaves), and some without. You form a preliminary hypothesis that they are members of different species. (32 pts) Part 1: What specific features/aspects would you examine to test your hypothesis that these ferns have undergone speciation? Utilize at least two species concepts/definitions in your response. (16 pts) Part 2: What events could potentially be...
Does anyone know when we use heck reaction or suzuki reaction? I came across some example...
Does anyone know when we use heck reaction or suzuki reaction? I came across some example but not sure when to use each reaction? I thought they are the same?
I only spend time hiking (H) and sleeping (S). For every hour I hike I need...
I only spend time hiking (H) and sleeping (S). For every hour I hike I need three hours of sleep or that time hiking gives me no pleasure. Extra sleep (additional to that) doesn’t make me any happier. 1) Graph 2+ indifference curves 2) Write an equation that could represent the utility function 3) For that utility function, what is the marginal utility for each good? 4) What is the MRS? Do these preferences represent diminishing MRS?
The belief in the day of judgement and heaven and hell: a) Came to the religion...
The belief in the day of judgement and heaven and hell: a) Came to the religion later b) Was original to the Jewish belief system c) Was stated in Pentateuch
You go on a long hike one day. Even though the temperature outside increases steadily throughout...
You go on a long hike one day. Even though the temperature outside increases steadily throughout the day, your internal temperature stays relatively stable. When your body temperature begins to get too high, your brain detects the change and initiates heat losing actions, such as sweating.  What is the control center in this homeostatic mechanism? The increased temperature The brain The sweat glands The blood Which of the following lists the levels of organization in order from smallest to largest? Atoms-->...
Many small restaurants in Portland, Oregon, and other cities across the United States do not take reservations.
  Number of Customers Waiting Time (Minutes) 5 47 2 27 4 36 4 44 6 52 3 21 7 82 3 43 8 69 4 28 Many small restaurants in Portland, Oregon, and other cities across the United States do not take reservations. Owners say that with smaller capacity, no-shows are costly, and they would rather have their staff focused on customer service rather than maintaining a reservation system.† However, it is important to be able to give reasonable...
In using my propane powered grill the other day (nice day, 65°F) I noticed the metal...
In using my propane powered grill the other day (nice day, 65°F) I noticed the metal cylinder holding the propane had frost forming on it. Why?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT