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

Java Program -- need help As I was on a hike the other day I came across a small

ID: 3791070 • Letter: J

Question

Java Program -- need help

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 that 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.

-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 incase his sister has a special request.

-He needs to be able to sort flowers by their names alphabetically in descending order (A-Z)

-He needs to show how many of each flower he has in his pack.

Please help me finish this code! (Please don’t modify the code that I give you, just add your code where required)

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

       */

      

   }

}

Explanation / Answer

package test_groupId.SpringStandalone;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Assignment01Driver {
   //added a new Scanner object
   Scanner sc = new Scanner(System.in);
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
   boolean found = false;
       int index=0;
           for(int i =0;i<flowerPack.length;i++){
               if(flowerPack[i]==null){
                   found=true;
                   index = i;
                   break;
               }
               }
               if(found){
                   System.out.println("Enter the flower name");
                   String newFlower = sc.nextLine();
                   flowerPack[index]=newFlower;
                   }
               else{
                   System.out.println("no space");
              
               }
  
}
private void removeFlower(String flowerPack[]) {
// TODO: Remove a flower that is specified by the user
   System.out.println("Enter the flower name for remove");
       String removeFlower = sc.nextLine();
   for(int i=0;i<flowerPack.length;i++){
           if(flowerPack[i]==removeFlower){
               flowerPack[i]=null;
               System.out.println(removeFlower+" removed from index no "+i);
               break;
           }
       }
  
}
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
   Arrays.sort(flowerPack, new Comparator<String>() {
           // @Override
       public int compare(String o1, String o2) {
       if (o1 == null && o2 == null) {
       return 0;
       }
       if (o1 == null) {
       return 1;
       }
       if (o2 == null) {
       return -1;
       }
       return o1.compareTo(o2);
       }});
  
}
private void searchFlowers(String flowerPack[]) {
// TODO: Search for a user specified flower
   System.out.println("Enter the flower name ");
       String flower = sc.nextLine();
   int count = 0;
       for(int i=0;i<flowerPack.length;i++){
           if(flowerPack[i]==flower){
               count++;
           }
       }
       System.out.println(count);
           if(count>0){
               System.out.println(count +" "+flower +" found");
           }
           else{
               System.out.println("no match found for : " + 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
*/
   //i am running out of time so i am giving simply
   for(int i = 0;i<flowerPack.length;i++){
           if(flowerPack[i]==null){}
           else
           System.out.println(flowerPack[i]);
       }
  
}
}

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