After dinner with Alexander and Elizabeth we notice that Alexander is mixing som
ID: 3783356 • Letter: A
Question
After dinner with Alexander and Elizabeth we notice that Alexander is mixing some herbs as a medication for Elizabeth which eases her pain and helps her sleep. Once the medicine is administered we begin small talk with Alexander. During the conversation he tells us the story about how his mother and father were hopelessly in love and were lucky enough to die side by side protecting the things they loved most, their children.
In the middle of the story we become distracted by Elizabeth trying to request something, but we can't make out what she wants because her medicine has kicked in. After a few moments we determine she is requesting a flower, but we can't understand the entire name, just the start. Instantly we remember that we know how to search through our flower pack while only using partial names. We instantly turn around and start working on yet another improvement to the flower pack.
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 ArrayList)
Be able to add, display and remove flowers
Implement a partial search (Searching for 'r' should return all flowers with an 'r' in their name, and the same goes for any partial search). This is commonly known as a filter.
As a new addition a rubric has been added to blackboard for the assignment.
Using the same code as assignment 2 you can make your changes. I have included some base code for your convenience (This is 2 classes, Assignment3 and Flower).
Submit 2 files: Assignment3.java and flower.java
import java.util.ArrayList;
import java.util.Scanner;
public class Assignment3 {
public static void main(String[] args) {
new Assignment3();
}
// This will act as our program switchboard
public Assignment3() {
Scanner input = new Scanner(System.in);
ArrayList<Flower> flowerPack = new ArrayList<Flower>();
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("5: Filter flower pack by incomplete name");
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 5:
filterFlowers(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(ArrayList<Flower> flowerPack) {
// TODO: Add a flower that is specified by the user
}
private void removeFlower(ArrayList<Flower> flowerPack) {
// TODO: Remove a flower that is specified by the user
}
private void searchFlowers(ArrayList<Flower> flowerPack) {
// TODO: Search for a user specified flower
}
private void displayFlowers(ArrayList<Flower> flowerPack) {
// TODO: Display flowers using any technique you like
}
private void filterFlowers (ArrayList<Flower> flowerPack) {
// TODO Filter flower results
}
}
// This should be in its own file
public class Flower {
// Declare attributes here
public Flower(){
}
// Create an overridden constructor here
//Create accessors and mutators for your triats.
}
Explanation / Answer
Hi Friend, 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;
}
}
################ Assignment3.java #############
import java.util.ArrayList;
import java.util.Scanner;
public class Assignment3 {
private static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
new Assignment3();
}
// This will act as our program switchboard
public Assignment3() {
ArrayList<Flower> flowerPack = new ArrayList<Flower>();
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("5: Filter flower pack by incomplete name");
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 5:
filterFlowers(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(ArrayList<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
flowerPack.add(f);
}
private void removeFlower(ArrayList<Flower> flowerPack) {
// taking user input
System.out.print("Enter name of flower to be romoved: ");
String name = input.nextLine();
for(int i=0; i<flowerPack.size(); i++){
if(flowerPack.get(i).getName().equalsIgnoreCase(name)){
flowerPack.remove(i);
break;
}
}
}
private void searchFlowers(ArrayList<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.size(); i++){
if(flowerPack.get(i).getName().equalsIgnoreCase(name)){
Flower f = flowerPack.get(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(ArrayList<Flower> flowerPack) {
for(int i=0; i<flowerPack.size(); i++){
Flower f = flowerPack.get(i);
System.out.println("Name: "+f.getName()+", Color: "+f.getColor()+
", isThrown: "+f.isThrown()+", Smell: "+f.getSmell());
System.out.println();
}
}
private void filterFlowers (ArrayList<Flower> flowerPack) {
// taking user input
System.out.print("Enter WORD for partial search: ");
String name = input.next();
// creating list to store searched item
ArrayList<Flower> partialSearch = new ArrayList<>();
for(int i=0; i<flowerPack.size(); i++){
if(flowerPack.get(i).getName().contains(name)){
partialSearch.add(flowerPack.get(i));
}
}
System.out.println("Flowers details containg "+name+" in name: ");
for(Flower f : partialSearch){
System.out.println("Name: "+f.getName()+", Color: "+f.getColor()+
", isThrown: "+f.isThrown()+", Smell: "+f.getSmell());
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.