Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA PROGRAM I need my code below to be able to accomplish these 4 tasks: • Crea

ID: 3801446 • Letter: J

Question

JAVA PROGRAM

I need my code below to be able to accomplish these 4 tasks:

•   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 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. -- Need to add this!

There are two classes, Assignment3 and Flower

/*
* Luke Bement
* CSC 275 - C
* Assignement #3
*/
package assignement3;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;

public class Assignment3Driver {
//added a new Scanner object
Scanner sc = new Scanner(System.in);
public static void main(String[] args){
new Assignment3Driver ();
}

// This will act as our program switchboard

public Assignment3Driver (){
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[]) {
// Add a flower that is specified by the user
boolean found = false;
int index=0;
for(int i =0;i<flowerPack.length;i++){
if(flowerPack[i]==null){
found=true;
index = i;
break;
}
}
if(found){
System.out.println("Enter the flower name");
String newFlower = sc.nextLine();
flowerPack[index]=newFlower;
}
else{
System.out.println("no space");

}

}
private void removeFlower(String flowerPack[]) {
   // Remove a flower that is specified by the user
   System.out.println("Enter the flower name for remove");
   String removeFlower = sc.next();
   for (int i = 0; i < flowerPack.length; i++)
   if (flowerPack[i].equals(removeFlower)) {
   flowerPack[i] = null;
   System.out.println(removeFlower + " removed from index no " + i);
   break;
   }
   }


private void sortFlowers(String flowerPack[]) {
// Sort the flowers in the pack (No need to display them here) - Use Selection or Insertion sorts
Arrays.sort(flowerPack, new Comparator<String>() {
// @Override
public int compare(String o1, String o2) {
if (o1 == null && o2 == null) {
return 0;
}
if (o1 == null) {
return 1;
}
if (o2 == null) {
return -1;
}
return o1.compareTo(o2);
}});

}
private void searchFlowers(String flowerPack[]) {
// Search for a user specified flower
   System.out.println("Enter the flower name ");
   String flower = sc.next();
   int count = 1;
   for (int i = 0; i < flowerPack.length ;i++){
   System.out.println("Searching "+flowerPack[i]+" "+flower);
   if (flowerPack[i]!=null&&flowerPack[i].equals(flower)) {
   count=-1;
   break;
}
}
System.out.println();
if(count<=0){
System.out.println(flower +" found");
}
else{
System.out.println("no match found for : " + flower);
}

}
private void displayFlowers(String flowerPack[]) {
// Display only the unique flowers along with a count of any duplicates
/*
* For example it should say
* Roses - 7
* Daffodils - 3
* Violets - 5
*/

   HashMap<String,Integer> hm = new HashMap<String, Integer>();
   for (int i = 0; i < flowerPack.length; i++){
   if (flowerPack[i] == null) {} else{
   if(!hm.containsKey(flowerPack[i])){
   hm.put(flowerPack[i],0);
   }
   hm.put(flowerPack[i],hm.get(flowerPack[i])+1);
   }
   }
   Set<String> key = hm.keySet();
   for(String k : key){
   System.out.println(k+" "+hm.get(k));
}

}
}

package assignement3;

public class Flower {

private String name;
private String color;
private boolean thornsPresent;
private String smell;

public Flower(){

}

public Flower(String name,String color,boolean thornsPresent,String smell)
{
this.name=name;
this.color=color;
this.thornsPresent=thornsPresent;
this.smell=smell;
}

public String getName() {
return name;
}
public void setName(String name) {

this.name = name;
}
public String getColor() {

return color;
}

public void setColor(String color) {
this.color = color;
}
public boolean isThornsPresent() {

return thornsPresent;
}
public void setThornsPresent(boolean thornsPresent) {

this.thornsPresent = thornsPresent;
}
public String getSmell() {

return smell;
}
public void setSmell(String smell) {

this.smell = smell;
}
}

Explanation / Answer

/*
* Luke Bement
* CSC 275 - C
* Assignement #3
*/
package assignement3;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;

public class Assignment3Driver {
//added a new Scanner object
Scanner sc = new Scanner(System.in);
public static void main(String[] args){
new Assignment3Driver ();
}

// This will act as our program switchboard

public Assignment3Driver (){
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[]) {
// Add a flower that is specified by the user
boolean found = false;
int index=0;
for(int i =0;i<flowerPack.length;i++){
if(flowerPack[i]==null){
found=true;
index = i;
break;
}
}
if(found){
System.out.println("Enter the flower name");
String newFlower = sc.nextLine();
flowerPack[index]=newFlower;
}
else{
System.out.println("no space");

}

}
private void removeFlower(String flowerPack[]) {
   // Remove a flower that is specified by the user
   System.out.println("Enter the flower name for remove");
   String removeFlower = sc.next();
   for (int i = 0; i < flowerPack.length; i++)
   if (flowerPack[i].equals(removeFlower)) {
   flowerPack[i] = null;
   System.out.println(removeFlower + " removed from index no " + i);
   break;
   }
   }


private void sortFlowers(String flowerPack[]) {
// Sort the flowers in the pack (No need to display them here) - Use Selection or Insertion sorts
Arrays.sort(flowerPack, new Comparator<String>() {
// @Override
public int compare(String o1, String o2) {
if (o1 == null && o2 == null) {
return 0;
}
if (o1 == null) {
return 1;
}
if (o2 == null) {
return -1;
}
return o1.compareTo(o2);
}});

}
private void searchFlowers(String flowerPack[]) {
// Search for a user specified flower
   System.out.println("Enter the flower name ");
   String flower = sc.next();
   int count = 1;
   for (int i = 0; i < flowerPack.length ;i++){
   System.out.println("Searching "+flowerPack[i]+" "+flower);
   if (flowerPack[i]!=null&&flowerPack[i].equals(flower)) {
   count=-1;
   break;
}
}
System.out.println();
if(count<=0){
System.out.println(flower +" found");
}
else{
System.out.println("no match found for : " + flower);
}

}
private void displayFlowers(String flowerPack[]) {
// Display only the unique flowers along with a count of any duplicates
/*
* For example it should say
* Roses - 7
* Daffodils - 3
* Violets - 5
*/

   HashMap<String,Integer> hm = new HashMap<String, Integer>();
   for (int i = 0; i < flowerPack.length; i++){
   if (flowerPack[i] == null) {} else{
   if(!hm.containsKey(flowerPack[i])){
   hm.put(flowerPack[i],0);
   }
   hm.put(flowerPack[i],hm.get(flowerPack[i])+1);
   }
   }
   Set<String> key = hm.keySet();
   for(String k : key){
   System.out.println(k+" "+hm.get(k));
}

}
}

package assignement3;

public class Flower {

private String name;
private String color;
private boolean thornsPresent;
private String smell;

public Flower(){

}

public Flower(String name,String color,boolean thornsPresent,String smell)
{
this.name=name;
this.color=color;
this.thornsPresent=thornsPresent;
this.smell=smell;
}

public String getName() {
return name;
}
public void setName(String name) {

this.name = name;
}
public String getColor() {

return color;
}

public void setColor(String color) {
this.color = color;
}
public boolean isThornsPresent() {

return thornsPresent;
}
public void setThornsPresent(boolean thornsPresent) {

this.thornsPresent = thornsPresent;
}
public String getSmell() {

return smell;
}
public void setSmell(String smell) {

this.smell = smell;
}
}