Now that weve helped out our friend and his sister they have
Now that we’ve helped out our friend and his sister they have invited us over for dinner to talk about what improvements can be made. We find out many things about the young boy, we even find out that his name is Alexander and his sister’s name is Elizabeth. Elizabeth tells us a about the day of her accident when she was climbing a tree and a branch broke causing her to fall to the ground giving her these near fatal wounds leaving her bed-ridden. Finally, we start talking about the improvements they would like made to make Elizabeth’s life a little happier.
Alexander finds himself searching through his pack for specific traits of a flower. Some days Elizabeth wants only red flowers or only flowers with thorns as she likes to peel them off. Surely we can help them out!
Create a flower object that has specific traits (name, color, presence of thorns and smell)
These flower objects must be able to stay in his pack (Use an array of length 25)
Be able to add, remove, display and search these flowers – by name (We can drop the sorting for now)
Using the same code as assignment 1 you can make your changes. I have included some base code for your convenience (This is 2 classes, Assignment2 and Flower).
Submit 2 files: Assignment2.java and flower.java
Here is the code from assignmet 1 below.
// Assignment01Driver.java
import java.util.Arrays;
import java.util.Scanner;
public class Assignment01Driver
{
public static void main(String[] args)
{
new Assignment01Driver ();
}
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)
{
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.\");
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[])
{
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter type of flower to add: \");
String string = scan.nextLine();
int idx = 0;
for (int i = 0; i < flowerPack.length; i++)
{
if (flowerPack[i] != null)
{
idx++;
if (idx == flowerPack.length)
{
System.out.println(\"Flower pack is full\ \");
}
}
else
{
flowerPack[i] = string;
break;
}
}
}
private void removeFlower(String flowerPack[])
{
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter the flower type to remove: \");
String string = scan.nextLine();
for (int idx = 0; idx < flowerPack.length; idx++)
{
if (flowerPack[idx].compareToIgnoreCase(string) == 0)
{
flowerPack[idx] = null;
break;
}
}
for (int idx = 0; idx < flowerPack.length; idx++)
{
if (flowerPack[idx] == null)
{
flowerPack[idx + 1] = flowerPack[idx];
break;
}
}
}
private void sortFlowers(String flowerPack[])
{
for (int i = 0; i < flowerPack.length; i++)
{
String min = flowerPack[i];
int minIndex = i;
for (int j = i; j < flowerPack.length; j++)
{
if (flowerPack[j] == null)
{
break;
}
if (min.compareToIgnoreCase(flowerPack[j]) > 0)
{
min = flowerPack[j];
minIndex = j;
}
}
if (minIndex != i)
{
flowerPack[minIndex] = flowerPack[i];
flowerPack[i] = min;
}
}
System.out.println(\"Flower pack sorted\ \");
}
private void searchFlowers(String flowerPack[])
{
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter the flower type to search: \");
String string = scan.nextLine();
boolean flower = false;
for ( int i = 0; i < flowerPack.length; i++)
{
if (flowerPack[i] == null)
{
break;
}
else if (flowerPack[i].compareToIgnoreCase(string) == 0)
{
flower = true;
break;
}
}
if (flower == true)
{
System.out.println(\"Flower type is present\ \");
}
else
{
System.out.println(\"Flower type not found\ \");
}
}
private void displayFlowers(String flowerPack[])
{
int c = 1;
for (int i = 0; i < flowerPack.length - 1; i++)
{
if (flowerPack[i] == null)
{
break;
}
else if (flowerPack[i].equalsIgnoreCase(flowerPack[i + 1]))
{
c++;
}
else
{
System.out.println(flowerPack[i] + \"s - \" + c);
c = 1;
}
}
System.out.println(\"\ \");
}
}
Solution
Complete code including sort
import java.util.Arrays;
import java.util.Scanner;
class Flower
{
String name, color, smell;
boolean thorns;
Flower(String n, String c, boolean t, String s )
{
name=n; color=c; thorns=t; smell=s;
}
public String toString()
{
String d= name+\" \"+color+\" \"+smell;
if(thorns) d+= \" Flower has Thorns\";
else d+=\" Flower does not have thorns\";
return d;
}
}
public class Assignment01Driver
{
public static void main(String[] args)
{
new Assignment01Driver ();
}
public Assignment01Driver ()
{
Scanner input = new Scanner(System.in);
Flower[] flowerPack = new Flower[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)
{
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.\");
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(Flower flowerPack[])
{
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter Name of flower to add: \");
String string = scan.nextLine();
System.out.println(\"Enter color of flower to add: \");
String color = scan.nextLine();
System.out.println(\"Enter smell of flower to add: \");
String smell = scan.nextLine();
System.out.println(\"Does the flower have thorns: Y or N \");
String thorns = scan.nextLine();
boolean t;
if(thorns.equalsIgnoreCase(\"Y\"))
t=true;
else t=false;
Flower f= new Flower(string, color,t,smell);
int idx = 0;
for (int i = 0; i < flowerPack.length; i++)
{
if (flowerPack[i] != null)
{
idx++;
if (idx == flowerPack.length)
{
System.out.println(\"Flower pack is full\ \");
}
}
else
{
flowerPack[i] = f;
break;
}
}
}
private void removeFlower(Flower flowerPack[])
{
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter the flower name to remove: \");
String string = scan.nextLine();
for (int idx = 0; idx < flowerPack.length; idx++)
{
if (flowerPack[idx].name.compareToIgnoreCase(string) == 0)
{
flowerPack[idx] = null;
break;
}
}
for (int idx = 0; idx < flowerPack.length-1; idx++)
{
if (flowerPack[idx] == null)
{
flowerPack[idx] = flowerPack[idx+1];
flowerPack[idx+1]=null;
//break;
}
}
}
private void sortFlowers(Flower flowerPack[])
{
for (int i = 0; i < flowerPack.length; i++)
{
Flower min = flowerPack[i];
int minIndex = i;
for (int j = i; j < flowerPack.length; j++)
{
if (flowerPack[j] == null)
{
break;
}
if (min.name.compareToIgnoreCase(flowerPack[j].name) > 0)
{
min = flowerPack[j];
minIndex = j;
}
}
if (minIndex != i)
{
flowerPack[minIndex] = flowerPack[i];
flowerPack[i] = min;
}
}
System.out.println(\"Flower pack sorted\ \");
}
private void searchFlowers(Flower flowerPack[])
{
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter the flower name to search: \");
String string = scan.nextLine();
boolean flower = false;
for ( int i = 0; i < flowerPack.length; i++)
{
if (flowerPack[i] == null)
{
break;
}
else if (flowerPack[i].name.compareToIgnoreCase(string) == 0)
{
flower = true;
break;
}
}
if (flower == true)
{
System.out.println(\"Flower type is present\ \");
}
else
{
System.out.println(\"Flower type not found\ \");
}
}
private void displayFlowers(Flower flowerPack[])
{
int c = 1;
for (int i = 0; i < flowerPack.length - 1; i++)
{
if (flowerPack[i] == null)
{
break;
}
else
{
System.out.println(i+1+\": \"+flowerPack[i]);
//c = 1;
}
}
System.out.println(\"\ \");
}
}







