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

I am having a little trouble with this assignment, I have created the method for

ID: 3664377 • Letter: I

Question

I am having a little trouble with this assignment, I have created the method for inserting the flower and half of the view array method. I cant seem to get the remove flower method to work and i am not sure why. I have posted the original assignment and my code below it any help would be great.

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

                  */

                 

         }

}

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

import java.util.Scanner;

public class Assignment01Driver {

         Scanner input = 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[]) {

                  for(int i = 0;i < flowerPack.length; i++){

                  int counter = i;

                  if (counter == 24){

                           System.out.print("This is your final entry " );}

                  System.out.print("Enter the name of flower " + (i+1) + " " );

                  flowerPack[i] = input.nextLine();

                  System.out.print("Would you like to enter another flower y or n " );

                  String choice = input.nextLine();

                  if (choice.equalsIgnoreCase("n")){

                           break;}

                  }

         }

         private void removeFlower(String flowerPack[]) {

                  System.out.print("What flower would you like to erase? " );

                  String choice = input.nextLine();

                  for(int i = 0;i < flowerPack.length; i++){

                           if (flowerPack[i] == choice)

                                    flowerPack[i]= "null";    

                  }

                 

         }

         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

                  */

                  for(int i = 0;i < flowerPack.length; i++){

                           System.out.print(flowerPack[i] + " ");

                  }

                  System.out.print("Press any key to continue" );

                  String choice = input.nextLine();

         }

}

Explanation / Answer


// Assignment01Driver.java

import java.util.ArrayList;
import java.util.Scanner;

public class Assignment01Driver
{

Scanner input = 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];
int totalFlowers = 0;
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,totalFlowers);
totalFlowers++;
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[], int index)
{
System.out.print("Enter the name of the flower " + (index + 1) + " ");
flowerPack[index] = input.nextLine();
}

private void removeFlower(String flowerPack[])
{
System.out.print("What flower would you like to erase? ");
String choice = input.nextLine();
for (int i = 0; i < flowerPack.length; i++)
{
if (flowerPack[i] != null && flowerPack[i].equals(choice))
{
flowerPack[i] = "null";
}
}
}

private void sortFlowers(String flowerPack[])
{
String t;
for (int i = 0; i < flowerPack.length; i++)
{
for (int j = i + 1; j < flowerPack.length; j++)
{
if (flowerPack[i] != null && flowerPack[j] != null && flowerPack[i].compareTo(flowerPack[j]) > 0)
{
t = flowerPack[i];
flowerPack[i] = flowerPack[j];
flowerPack[j] = t;
}
}
}
System.out.println("Sort complete Press any key to continue");
String choice1 = input.nextLine();
}

private void searchFlowers(String flowerPack[])
{
System.out.print("What flower would you like to search? ");
boolean flag = false;
int counter = 0;
String choice = input.nextLine();
for (int i = 0; i < flowerPack.length; i++)
{
if (flowerPack[i] != null && flowerPack[i].equals(choice))
{
counter++;
flag = true;
}
}
if (flag)
{
System.out.print(choice + " - " + counter + " ");
}
if (!flag)
{
System.out.print("Flower " + choice + " not found ");
}
System.out.print("Press any key to continue");
String choice1 = input.nextLine();
}

//method to display flowerd
private void displayFlowers(String flowerPack[])
{
//storing names to avoid replication
ArrayList replicatedData = new ArrayList();
//checking for replication
for (int i = 0; i < flowerPack.length; i++)
{
boolean found = false;
for(int k=0;k<replicatedData.size();k++)
{
if(replicatedData.get(k).equals(flowerPack[i]))
{
found = true;
}
}
if(!found)
{
replicatedData.add(flowerPack[i]);
int count = 0;
//counting how many times it repeats
for(int j=0;j<flowerPack.length;j++)
{
if(flowerPack[i].equals(flowerPack[j]))
{
count++;
}
}
//finally printing resuls
System.out.print(flowerPack[i] + "-"+count+" ");
}
}
}
}


/*
output:

Welcome to my flower pack interface.
Please select a number from the options below

1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
1
Enter the name of the flower 1 Red
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
1
Enter the name of the flower 2 Rose
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
1
Enter the name of the flower 3 Green
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
1
Enter the name of the flower 4 Green
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
1
Enter the name of the flower 5 Blue
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
1
Enter the name of the flower 6 Blue
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
3
Sort complete
Press any key to continue
4
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
4
What flower would you like to search? Green
Green - 2
Press any key to continue
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
5
Blue-2
Green-2
Red-1
Rose-1


*/