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

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();

Solution

Please follow the code and comments for description :

CODE :

import java.util.Arrays;
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];
int length = 0;

System.out.println(\"Welcome to my flower pack interface.\"); // message
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(\"\");
System.out.println(\"1: Add an item to the pack.\"); // options
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 interface.\");

// Get the user input
int userChoice = input.nextInt(); // get the data

switch (userChoice) {
case 1: // case 1
  
System.out.print(\"Enter the name of the flower to be added : \"); // message
input.nextLine();
if (length < 25) { // check for the length
flowerPack[length] = input.nextLine();
length++; // increment the length
} else {
System.out.println(\"Sorry !!\ The maximum limit to hold the flowers has been exceeded.\"); // message
}
break;
case 2: // case 2
System.out.print(\"Enter the name of the flower to be removed : \"); // message
input.nextLine();
String name = input.nextLine();
for (int i = 0; i < flowerPack.length; i++) { // iterate to delete the data
if (flowerPack[i].equalsIgnoreCase(name)) {
flowerPack[i] = null;
System.out.println(\"Deleted the item successfully.!\");
for(int j = i + 1; j < flowerPack.length; j++) {
flowerPack[i] = flowerPack[j];
i++;
}
break;
}
}
length--;
break;
case 3:
String temp = null;
System.out.println(\"Sorting the data alphabetically..\"); // sor tthe data
for (int i = 0; i < flowerPack.length; i++) {
for (int j = i + 1; j < flowerPack.length; j++) {
if (flowerPack[i].compareTo(flowerPack[j]) > 0) {
temp = flowerPack[i]; // temporary varaible
flowerPack[i] = flowerPack[j];
flowerPack[j] = temp;
}
}
}
System.out.print(\"Names in the Alphabetical Sorted Order : \"); // message
for (int i = 0; i < flowerPack.length - 1; i++) {
System.out.print(flowerPack[i] + \",\"); // print the data
}
System.out.print(flowerPack[flowerPack.length - 1]);
break;
case 4:
System.out.println(\"Enter the name of the flower to be searched.\"); // serach the data
String fName = input.nextLine();
boolean found = false;
for (int i = 0; i < flowerPack.length; i++) {
if (flowerPack[i].equalsIgnoreCase(fName)) { // check for the data
found = true;
}
}
if (found) {
System.out.println(\"The Flower is found in the list.\"); // message
} else {
System.out.println(\"The Flower is not found in the list.\");
}
break;
case 5:
System.out.println(\"Displaying the flowers in the pack...\"); // display the data
System.out.println(Arrays.asList(flowerPack));
break;
case 0:
System.out.println(\"Exiting the interface...\"); // exit case
System.exit(0);
break;
default:
System.out.println(\"Sorry !!\ Wrong Selection.\"); // default message
}
}
}
}


OUTPUT :

run:
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 interface.
1
Enter the name of the flower to be added : 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 interface.
1
Enter the name of the flower to be added : lilly

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 interface.
1
Enter the name of the flower to be added : shoeflower

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 interface.
1
Enter the name of the flower to be added : daisy

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 interface.
1
Enter the name of the flower to be added : sunflower

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 interface.
1
Enter the name of the flower to be added : dahlia

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 interface.
1
Enter the name of the flower to be added : yarrow

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 interface.
1
Enter the name of the flower to be added : pineapple lilly

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 interface.
1
Enter the name of the flower to be added : beard tounge

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 interface.
1
Enter the name of the flower to be added : oxalis

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 interface.
1
Enter the name of the flower to be added : surprise lilly

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 interface.
1
Enter the name of the flower to be added : gaillardia

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 interface.
1
Enter the name of the flower to be added : peonies

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 interface.
1
Enter the name of the flower to be added : daffodils

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 interface.
1
Enter the name of the flower to be added : daylily

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 interface.
1
Enter the name of the flower to be added : zinnia

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 interface.
1
Enter the name of the flower to be added : plumeria

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 interface.
5
Displaying the flowers in the pack...
[rose, lilly, shoeflower, daisy, sunflower, dahlia, yarrow, pineapple lilly, beard tounge, oxalis, surprise lilly, gaillardia, peonies, daffodils, daylily, zinnia, plumeria, null, null, null, null, null, null, null, null]

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 interface.
1
Enter the name of the flower to be added : lavender

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 interface.
1
Enter the name of the flower to be added : periwinkle

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 interface.
1
Enter the name of the flower to be added : daisy

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 interface.
1
Enter the name of the flower to be added : shrub 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 interface.
1
Enter the name of the flower to be added : aster

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 interface.
1
Enter the name of the flower to be added : hibiscus

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 interface.
1
Enter the name of the flower to be added : scarlet sage

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 interface.
1
Enter the name of the flower to be added : lotus

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 interface.
5
Displaying the flowers in the pack...
[rose, lilly, shoeflower, daisy, sunflower, dahlia, yarrow, pineapple lilly, beard tounge, oxalis, surprise lilly, gaillardia, peonies, daffodils, daylily, zinnia, plumeria, lavender, periwinkle, daisy, shrub rose, aster, hibiscus, scarlet sage, lotus]

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 interface.
1
Enter the name of the flower to be added : Sorry !!
The maximum limit to hold the flowers has been exceeded.

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 interface.
2
Enter the name of the flower to be removed : lotus
Deleted the item successfully.!

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 interface.
5
Displaying the flowers in the pack...
[rose, lilly, shoeflower, daisy, sunflower, dahlia, yarrow, pineapple lilly, beard tounge, oxalis, surprise lilly, gaillardia, peonies, daffodils, daylily, zinnia, plumeria, lavender, periwinkle, daisy, shrub rose, aster, hibiscus, scarlet sage, null]

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 interface.
1
Enter the name of the flower to be added : lotus

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 interface.
5
Displaying the flowers in the pack...
[rose, lilly, shoeflower, daisy, sunflower, dahlia, yarrow, pineapple lilly, beard tounge, oxalis, surprise lilly, gaillardia, peonies, daffodils, daylily, zinnia, plumeria, lavender, periwinkle, daisy, shrub rose, aster, hibiscus, scarlet sage, lotus]

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 interface.
3
Sorting the data alphabetically..
Names in the Alphabetical Sorted Order : aster,beard tounge,daffodils,dahlia,daisy,daisy,daylily,gaillardia,hibiscus,lavender,lilly,lotus,oxalis,peonies,periwinkle,pineapple lilly,plumeria,rose,scarlet sage,shoeflower,shrub rose,sunflower,surprise lilly,yarrow,zinnia
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 interface.
4
Enter the name of the flower to be searched.
The Flower is not found in the list.

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 interface.
0
Exiting the interface...

Hope this is helpful.

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 flow
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 flow
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 flow
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 flow
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 flow
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 flow
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 flow
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 flow
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 flow

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site