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.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\nPress 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 + "\n");
}
if (!flag)
{
System.out.print("Flower " + choice + " not found\n");
}
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+"\n");
}
}
}
}


/*
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


*/


Related Solutions

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?
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-->...
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
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?
In the lecture on eugenics, we came across the ideas of both positive and negative eugenics....
In the lecture on eugenics, we came across the ideas of both positive and negative eugenics. Do you think positive eugenics can achieve the stated goal of improving the quality of our society? Why, or why not? In your answer, be sure to reference the related concepts we covered in the course. Your response should demonstrate your understanding of the principles of inheritance and evolution.
STUDY 4 The university is building a number of active learning classrooms and came across a...
STUDY 4 The university is building a number of active learning classrooms and came across a chair called the Ruckus Stool that is designed specifically for collaborative work. The design allows users to comfortably use it frontward and backwards, but you are unsure whether it will work for a wide range of activities. To test the design before ordering a large number, you ask a group of students to try it for three types of lessons: i) lecture, ii) two-person...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT