Now that we’ve helped out our friend and his sister they have invited us over fo
ID: 3853432 • Letter: N
Question
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 and search these flowers – by name and traits (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 has 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 an overridden constructor here //Create accessors and mutators for your traits. }
Explanation / Answer
import java.util.Scanner;
import java..util.*
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); } } }
List<Flower> FlowerList= new ArrayList<Flower>(); //this is an arraylist for Flower
Flower objflower = new Flower(); // new object for Flower
private void addFlower(Flower objflower)
{
objflower.setName(name);
objflower.setcolor(fcolor);
objflower.setthorns(pot);
objflower.setsmell(fsmell);
FlowerList.add(objflower);
}
}
}
private void removeFlower(Flower objflower)
{
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
for(i=0;i<=FlowerList.size();i++)
{
If(FlowerList.get(i).getname()==name)
FlowerList.remove(objflower);
}
private void searchFlowers(Flower objflower)
{
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
for(i=0;i<=FlowerList.size();i++)
{
If(FlowerList.get(i).getname()==name)
System.out.println("Your flower exists in the list…");
}
}
private void displayFlowers(Flower objflower)
{
//Display flower name along with a count .. For example it should say Roses - 7 Daffodils - 3 Violets - 5 */ }
int i;
int count;
for(i=0;i<=FlowerList.size();i++)
{
for(j=0;j<= FlowerList.size();j++)
{
If(FlowerList.get(i).getName()==FlowerList.get(j).getName()) //if the name matches
count++;
System.out.println(FlowerList.get(i).getName()+"--"+count);
}
}
}
public class Flower
{
private String flowername;
private String fcolor;
private String presence_of_thorns;
private int smell;
public String getName()
{
return this.flowername;
}
public String getcolor()
{
return this.fcolor;
}
public String getthorns()
{
return this.presence_of_thorns;
}
public String getsmell()
{
return this.smell;
}
public boolean setName(String name)
{
this.flowername = name;
return true;
}
public boolean setcolor(String color)
{
this.color= color;
return true;
public boolean setthorns(String thorns)
{
this.presence_of_thorns = thorns;
return true;
}
public boolean setsmell(String fsmell)
{
this.smell= fsmell;
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.