I have assignment due Thursday night and I have been going back and forth with it. Eventually i told my self I'm going to take this assignment step by step. Below is my assignment to give you a brief history.
Assignment 1
As I was on a hike the other day I came across a small child in the woods. He told me his life story, with special mention of his disabled sister that loves flowers, and asked me for a favor.
He wanted a way to organize the flowers that he picks for her each day and perform a few basic tasks with them, along with a few restrictions. It is our goal to help him out!
He can only carry 25 flowers as adding any more causes many of them to become crushed.
He needs to be able to search for a specific type of flower in his pack incase his sister has a special request.
He needs to be able to sort flowers by their names alphabetically in descending order (A-Z)
He needs to know how many of each flower he has in his pack.
Now, I have started a simple program that will serve as guidance for you, please help me finish it. (Please dont modify the code that I give you, just add your code where required)
import java.util.Scanner; public class Assignment1 { public static void main(String[] args){ new Assignment1(); } // This will act as our program switchboard public Assignment1(){ 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[]) { // TODO: Add a flower that is specified by the user } private void removeFlower(String flowerPack[]) { // TODO: Remove a flower that is specified by the user } private void sortFlowers(String flowerPack[]) { // TODO: Sort the flowers in the pack (No need to display them here) - Use Selection or Insertion sorts // NOTE: Special care is needed when dealing with strings! research the compareTo() method with strings } private void searchFlowers(String flowerPack[]) { // TODO: Search for a user specified flower } private void displayFlowers(String 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 */ } }
I'm starting with my addflower method. While I was writing my code an issue came up that I didn't expect. Here is what I have so far.(I know I might be taking the wrong approach but i need to start some where. )
import java.util.*; public class addflower { public static void main(String[] args) { String[] addflowerjr = new String[25]; Scanner scan = new Scanner(System.in); System.out.println( " Please enter your flowers. You will only be allowed to enter no more than 25 flowers."); System.out.println( " Please enter now: "); testaddflower(addflowerjr); } public void testaddflower(String[] addflowerjr ) { for(int i = 0; i < addflowerjr.length; i++) { addflowerjr[i] = scan.nextLine(); } }
I don't understand on what to do but when I pass my array into my method I know i can get the first iteration when store my string. My question is how can stop at an index to continue my array. If I recall my method(when I complete my program) I will be starting back over from the first index. I want to continue on from that array. Does anyone have any suggestions?