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
import java.util.Scanner;
public class Assignment2 {
public static void main(String[] args) {
new Assignment2();
}
// This will act as our program switchboard
public Assignment2() {
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) {
// 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: Search for a flower.\");
System.out.println(\"4: 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:
searchFlowers(flowerPack);
break;
case 4:
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[]) {
// TODO: Add a flower that is specified by the user
}
private void removeFlower(Flower flowerPack[]) {
// TODO: Remove a flower that is specified by the user
}
private void searchFlowers(Flower flowerPack[]) {
// TODO: Search for a user specified flower
}
private void displayFlowers(Flower 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
*/
}
}
// This should be in its own file
public class Flower {
// Declare attributes here
public Flower(){
}
//Create accessors and mutators for your traits.
}
Solution
Hi, Please find my implementation.
Please let me know in case of any issue.
########## Flower.java ############################
// This should be in its own file
public class Flower {
// Declare attributes here
private String name;
private String color;
private boolean isThrown;
private String smell;
public Flower(){
name = \"\";
color = \"\";
isThrown = false;
smell = \"\";
}
// Create an overridden constructor here
public Flower(String name, String color, boolean isThrown, String smell) {
this.name = name;
this.color = color;
this.isThrown = isThrown;
this.smell = smell;
}
//Create accessors and mutators for your triats.
public String getName() {
return name;
}
public String getColor() {
return color;
}
public boolean isThrown() {
return isThrown;
}
public String getSmell() {
return smell;
}
public void setName(String name) {
this.name = name;
}
public void setColor(String color) {
this.color = color;
}
public void setThrown(boolean isThrown) {
this.isThrown = isThrown;
}
public void setSmell(String smell) {
this.smell = smell;
}
}
######################### Assignment2.java #################
import java.util.Scanner;
public class Assignment2 {
private static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
new Assignment3();
}
// This will act as our program switchboard
public Assignment2() {
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) {
// 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: Search for a flower.\");
System.out.println(\"4: Display the flowers in the pack.\");
System.out.println(\"0: Exit the flower pack interface.\");
// Get the user input
int userChoice = input.nextInt();
switch (userChoice) {
case 1:
addFlower(flowerPack);
break;
case 2:
removeFlower(flowerPack);
break;
case 3:
searchFlowers(flowerPack);
break;
case 4:
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) {
// taking user input
boolean isThrown = false;
System.out.print(\"Enter name of flower: \");
String name = input.nextLine();
System.out.print(\"Enter color: \");
String color = input.next();
System.out.print(\"Enter smell: \");
String smell = input.next();
System.out.print(\"Is this flower has thrown(y/n) : \");
char t = Character.toLowerCase(input.next().charAt(0));
if(t == \'y\')
isThrown = true;
// creating new Flower object
Flower f = new Flower(name, color, isThrown, smell);
// adding in linked list
for(int i=0; i<flowerPack.length; i++)
if(flowerPack[i] == null){
flowerPack[i] = f;
break;
}
}
private void removeFlower(Flower[] flowerPack) {
// taking user input
System.out.print(\"Enter name of flower to be romoved: \");
String name = input.nextLine();
int i;
for(i=0; i<flowerPack.length; i++){
if(flowerPack[i] == null) // not available in array
break;
if(flowerPack[i].getName().equalsIgnoreCase(name)){
break;
}
}
if(flowerPack[i] != null){ // if it is available in array then delete
for(int j=i; j<flowerPack.length-1; j++)
flowerPack[i] = flowerPack[i+1];
}
}
private void searchFlowers(Flower[] flowerPack) {
// taking user input
System.out.print(\"Enter name of flower to be search: \");
String name = input.nextLine();
for(int i=0; i<flowerPack.length; i++){
if(flowerPack[i] == null) // not available then stop searching
break;
if(flowerPack[i].getName().equalsIgnoreCase(name)){
Flower f = flowerPack[i];
System.out.println(\"Flower \"+name+\" is available at index \"+i);
System.out.println(\"Details of flower: \");
System.out.println(\"Name: \"+f.getName()+\", Color: \"+f.getColor()+
\", isThrown: \"+f.isThrown()+\", Smell: \"+f.getSmell());
break;
}
}
}
private void displayFlowers(Flower[] flowerPack) {
for(int i=0; i<flowerPack.length; i++){
Flower f = flowerPack[i];
if(f == null)
break;
System.out.println(\"Name: \"+f.getName()+\", Color: \"+f.getColor()+
\", isThrown: \"+f.isThrown()+\", Smell: \"+f.getSmell());
System.out.println();
}
}
}







