Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: I need some help with an assignment.

  1. #1
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    110
    My Mood
    Where
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default I need some help with an assignment.

    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 don’t 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?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: I need some help with an assignment.

    Quote Originally Posted by Rain_Maker View Post
    I'm starting with my addflower method.
    Well there's ya problem. Why have you written an addFlower class and not a method like you state?
    Improving the world one idiot at a time!

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I need some help with an assignment.

    Quote Originally Posted by Rain_Maker View Post
    I have assignment due Thursday night and I have been going back and forth with it.
    You waste time writing it, we waste time reading it. You get the same advice regardless of whether it is homework or not, or when it may be due. Do us all a favor and do not waste time with irrelevant bits.
    Quote Originally Posted by Rain_Maker View Post
    (Please don’t modify the code that I give you, just add your code where required)
    We do not give code solutions either, that is your assignment.
    Quote Originally Posted by Rain_Maker View Post
    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.
    I do not understand what this means.
    Quote Originally Posted by Rain_Maker View Post
    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?
    If you are iterating the array in a loop, use the loop index counter to access the index of the array. I do not understand exactly what you mean with this question.

    Overall suggestion:
    First, pick one problem to work with at a time. Given the assignment requirements probably work on addFlower first, since you can not sort, search, or remove until you have added something. I would think that addFlower would be a method of a class that stores and organizes flowers, and not a class just to add an element to an array. See the way Assignment1 was written, fill in the method bodies.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: I need some help with an assignment.

    My guess is that the "just add your code" comment was part of the assignment instructions.
    Improving the world one idiot at a time!

  5. The Following User Says Thank You to Junky For This Useful Post:

    jps (September 9th, 2013)

  6. #5
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    110
    My Mood
    Where
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: I need some help with an assignment.

    Sorry for the confusion everyone. I wrote some pseudo code down. This may help you guys understand my problem.

    Here is pseduo code on what I'm trying to do.


    Private void addFlower(String flowerPack[]) {
    // TODO: Add a flower that is specified by the user


    Psudeo Code

    Scanner scan = new Scanner(System.in);

    System.out.println( “ Hello user, please enter a flower name.”);
    System.out.println( “ Please be mindful you may enter a maximum of 25 flowers in your list. ” );
    System.out.println( “ Please enter now: ”);
    String addedflower = scan.nextLine();

    for(int i = 0; i < flowerpack.length; i++)
    {
    Flowerpack[i] = addflower;
    break;
    Don’t know what to do here after going through the first iteration. Once this for loop has ended I will have to recall the method which will start my "int i" back to 0. This in result will override my previous answer. Is there a better way for coding this out?

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I need some help with an assignment.

    String addedflower = scan.nextLine(); will get a line of text. If there are multiple flowers given on this line you will need to parse the line.
    Assuming there is a single flower given, you will just have to decide where in the array to place the flower. You can search the array for the first index that does not have a flower in it, or you can keep track of how many flowers are in the array.
    Since keeping the number of flowers in the array is one of the requirements, this is necessary for more than one reason. Again, it can be done both ways. Keep up with an integer or write a method to count the flowers

  8. #7
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    110
    My Mood
    Where
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: I need some help with an assignment.

    Thanks for the reply. For the addflower method I wanted to allow the user to enter one flower....and then store that flower in my array. I guess I will remove the nexline constuctor and use just scan.next.(I think this will help prevent any future problems. ) I'm just a little confuse how I will write the code to keep track of my array. i.e.

    int x = 0
    x++
    addflower[x];
    Once this method complete itself and I will have to use it again to store another array string. My problem or wall I’m hitting is that if I insert another string it will override my pervious answer. Using a for loop doesn’t seem to help. I thought about using if else statements.

  9. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I need some help with an assignment.

    If you want a value in a variable to persist between method calls you will have to define it outside the method so it is not reset every call.
    You will also have to maintain the value of this when removing flowers, sounds error prone to me.
    Someone once said never keep track of the same information in two places. In this case the two places would be what is actually in the array and the value in the variable. I lean you to a method that would return the number of flowers currently held.

  10. #9
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    110
    My Mood
    Where
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: I need some help with an assignment.

     
    /**
     * 
     * CSC 275
     * Assignment  1 
     * 
     */
     
    import java.util.*;
     
    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[]) {
     
            Scanner scan = new Scanner(System.in);
            System.out.println();
            System.out.println( " Hello user, please enter a flower name.");
            System.out.println( " Please be mindful you may enter a maximum of 25 flowers in your list. " );
            System.out.print( " Please enter now: ");
            String addedflower = scan.nextLine();
     
     
     
        for(int i = 0; i < flowerPack.length; i++)
        {
           if(flowerPack[i] == null)
           {
           flowerPack[i] = addedflower;
           break;
           }
     
     
       }
    }
    private void removeFlower(String flowerPack[])
    {
            Scanner scan = new Scanner(System.in);
            System.out.print( " Please enter a flower you would like to remove. ");
            String deleteflower = scan.nextLine();
            int i;
     
            for(i = 0; i < flowerPack.length; i++)
            {
               if( flowerPack[i] == deleteflower)
                {
                flowerPack[i] = "test";
     
               }
                        System.out.println(flowerPack[i]);         
     
     
     
            }
     
    }      
     
     
     
    private void sortFlowers(String flowerPack[]) {
        int i, j;
     
        for(i = 0; i < flowerPack.length; i++ )
        {
            String currentMinimum = flowerPack[i];
            int currentMinIndex = i;
     
            for( j = i +1; j < flowerPack.length-1; j++) 
            {
     
            int test = flowerPack[i].compareTo(flowerPack[j]);
     
            if( test > 0)
            {
               currentMinimum = flowerPack[j];
               currentMinIndex = j;
            }
     
            if(currentMinIndex != i)
            {
            flowerPack[currentMinIndex] = flowerPack[i];
            flowerPack[i] = currentMinimum;
            }
     
            }
     
        }
     
     
     
    } 
     
    private void searchFlowers(String flowerPack[]) {
     
            Scanner scan = new Scanner(System.in);
     
            System.out.println( " Please enter the flower you would like to search for.");
            String key = scan.next();
     
          boolean found = false;
     
        for(int y = 0; y < flowerPack.length; y++)
        {
     
            if( key == flowerPack[y])
            {
                found = true;
                System.out.println(key + "" + flowerPack[y]);
            }   
        }
     
         if(found == true)
          {
          System.out.println( key +  " is in the list of flowers.");
          System.out.println("");
          }
     
         else   
          {
          System.out.println(" No results found.");
          System.out.println("");
          }   
     
     
     
    }
     
    private void displayFlowers(String flowerPack[]) 
     
        {
     
     
            for(int x=0; x < flowerPack.length; x++)
     
     
    {
                System.out.println(flowerPack[x]);
     
     
     
     
            }
     
        }    
    }


    Okay I made great progress on this assignment. But I need a little guidance to finish the assignment.


    1. For some reason my searchflower method is no longer working. I really don't know why...it was working but now its not. I added a few system.out.println to see what the program is doing but I'm seeing its skipping my if statement. And is directly going to my else statement. Don't know why any suggestions?

    2. This might help me kill one bird with one stone. My print method and remove flower method is giving me trouble. For some reason having trouble coming up with a for loop to accomplish my task. I know for the removeflower method is going to have some type of "key". That key will search through the array and look for all arrays equaling that key. If that key equals it should remove it from the array. I was thinking about writing it like this.


    private void removeFlower(String flowerPack[])
    {
    Scanner scan = new Scanner(System.in);
    System.out.print( " Please enter a flower you would like to remove. ");
    String deleteflower = scan.nextLine();
    int i;

    for(i = 0; i < flowerPack.length; i++)
    {
    if( flowerPack[i] == deleteflower)
    {
    flowerPack[i] = " ";

    }



    }

    But I'm seeing that my flower entry is still in my array when I try to do a test print. Is there anyway to fix it? Any help much appreciated!

  11. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I need some help with an assignment.

    Quote Originally Posted by Rain_Maker View Post
    1. For some reason my searchflower method is no longer working
    2. This might help me kill one bird with one stone.
    Do not compare strings with ==, use .equals instead

    As a side note if(found == true), why not if(found), in the end the variable found evaluates to a boolean by itself

  12. The Following User Says Thank You to jps For This Useful Post:

    Rain_Maker (September 12th, 2013)

  13. #11
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    110
    My Mood
    Where
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: I need some help with an assignment.

    ohh wow you are right! Thanks! Let me try that!

  14. #12
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    110
    My Mood
    Where
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: I need some help with an assignment.

    Hello jps,

    Sorry to brother you...but I have one more question. I'm trying to count the occurrence of each string array element from my print method. But I'm a little confuse on what I should do. I know I want to use the equalToIgnoreCase, a counter and maybe another array. But I'm having trouble coming up with the pseudo code. Can you help me out?

    Thanks!

  15. #13
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I need some help with an assignment.

    You can decide how to accomplish your task by writing the steps down on paper. Start at the very beginning. Write down every step necessary. Do not worry about missing some of the specifics, start with a generalized idea of the overall process. When you get to the end go back and try to break those steps down into smaller steps each. Keep this up until each step seems to be such a simple task that you can not break it down any more.

    "Sorry to bother you..." We help because we enjoy helping, no bother.

  16. #14
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    110
    My Mood
    Where
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: I need some help with an assignment.

    okay thanks

  17. #15
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I need some help with an assignment.

    Hello, this is my first post on this forum. Hopefully it's of some help. My level of programming experience is somewhere between beginner and intermediate. I think this for loop may work for you in the removeFlower method...
    *code removed*
    Please read The problem with spoonfeeding
    Last edited by jps; September 13th, 2013 at 09:58 PM. Reason: spoonfeeding

  18. #16
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,476
    My Mood
    Mellow
    Thanks
    151
    Thanked 609 Times in 596 Posts

    Default Re: I need some help with an assignment.

    Nick, don't just provide solutions, and post your code in code tags. You can learn more about the community and how to use code tags in the Announcement sticky at the top of the "What's Wrong With My Code?" forum.

  19. The Following 2 Users Say Thank You to GregBrannon For This Useful Post:

    bignick (September 14th, 2013), Rain_Maker (September 13th, 2013)

  20. #17
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I need some help with an assignment.

    Quote Originally Posted by GregBrannon View Post
    Nick, don't just provide solutions, and post your code in code tags. You can learn more about the community and how to use code tags in the Announcement sticky at the top of the "What's Wrong With My Code?" forum.
    Oops...sorry! I'll checkout the sticky.

Similar Threads

  1. Help with Assignment
    By docguy89 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: March 15th, 2013, 08:30 AM
  2. Need help with assignment
    By djhunt90 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2012, 02:36 PM
  3. Need help with Assignment
    By gakon3445 in forum Algorithms & Recursion
    Replies: 5
    Last Post: September 14th, 2012, 12:55 PM
  4. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  5. Replies: 1
    Last Post: February 22nd, 2010, 07:20 AM