JAVA PROGRAM Alexander finds himself searching through his pack for specific tra
ID: 3857927 • Letter: J
Question
JAVA PROGRAM
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 and search these flowers – by name and traits (We can drop the sorting for now)
Using the code below. This is 2 classes.
package assignement1;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
public class Assignment01Driver {
//added a new Scanner object
Scanner sc = new Scanner(System.in);
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();
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 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() {
// @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 hm = new HashMap();
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 key = hm.keySet();
for(String k : key){
System.out.println(k+" "+hm.get(k));
}
}
}
// 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 traits.
}
Explanation / Answer
package assignement1;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
import assignement1.FLower;
public class Assignment01Driver {
//added a new Scanner object
Scanner sc = new Scanner(System.in);
public static void main(String[] args){
new Assignment01Driver ();
}
// This will act as our program switchboard
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){
// 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(Flower flowerPack[]) {
// Add a flower that is specified by the user
boolean found = false;
int index=0;
for(int i =0;i if(flowerPack[i]==null){
found=true;
index = i;
break;
}
}
if(found){
System.out.println("Enter the flower name");
Flower newFlower = new Flower(sc.nextLine(),"red",false,"good");
flowerPack[index]=newFlower;
}
else{
System.out.println("no space");
}
}
private void removeFlower(Flower 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].getName().equals(removeFlower)) {
flowerPack[i] = null;
System.out.println(removeFlower + " removed from index no " + i);
break;
}
}
private void sortFlowers(Flower flowerPack[]) {
// Sort the flowers in the pack (No need to display them here) - Use Selection or Insertion sorts
Arrays.sort(flowerPack, new Comparator() {
// @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(Flower 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].getName()!=null&&flowerPack[i].getName().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(Flower flowerPack[]) {
// Display only the unique flowers along with a count of any duplicates
/*
* For example it should say
* Roses - 7
* Daffodils - 3
* Violets - 5
*/
Map<Flower,Integer> hm = new HashMap<Flower,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 key = hm.keySet();
for(Flower k : key){
System.out.println(k.getName()+" "+hm.get(k));
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
package assignement1;
public class Flower {
private String name;
private String color;
private boolean thronsPresent;
private String smell;
public Flower(){
}
public Flower(String name,String color,boolean thronsPresent,String smell)
{
this.name=name;
this.color=color;
this.thronsPresent=thronsPresent;
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 isThronsPresent() {
return thronsPresent;
}
public void setThronsPresent(boolean thronsPresent) {
this.thronsPresent = thronsPresent;
}
public String getSmell() {
return smell;
}
public void setSmell(String smell) {
this.smell = smell;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.