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 6 of 6

Thread: How to delete and add elements to an array dynamically?

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to delete and add elements to an array dynamically?

    The idea is to create a Class named Bag that carry fruits of type String, and have different methods like putItem which takes the fruit and put it inside the bag, getItem which randomly pick the fruit, isEmpty which check whether the bag is empty, etc
    The professer said that we should have an int variable named index that keeps a track of the array, but still now I don't get it.
    The problem is that I am lost with this class, so could anyone help me code it

    Here is my code:

    public class Bag{
     
        private String fruits[];
        private int index;
     
        // Constructor that initillizes the size of array
        public Bag(){
        String[] newFruits = new String[8];
        fruits = newFruits;
        }
     
        // Method to to put an item in the bag
        public void putItem(String pItem){
            if(isFull() == true)
               return false;
     
            else
               fruits[index] = pItem;
               index ++;
               return true;
     
        }
     
        // Method to check whether the bag is full
        public boolean isFull(){
          if (index == fruits.length()-1)
            return true;
     
          else
            return false;
        }
     
        // Method to check whether the bag is empty
        public boolean isEmpty(){
            return(index == 0);
        }
     
        // Method to to get aan item in the bag randomly
        public String getItem(String getFruits){
     
            for(i=1; i<fruits.length; i++)
                fruits[i] = getFruits [i+1];
        }
     
        // Method to display the content in the bag
        public String toString(){
     
            return("Fruits present in bag: " +fruits);
        }
    }
    Last edited by Deep_4; November 7th, 2012 at 12:28 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help me to complete this class, please

    Hello k_w_r2007 and welcome to the Java Programming Forums

    Is this application ment to take user input from the console?
    For example, does a user select which fruit to remove from the bag?

    Please explain to me how you would like this program to work.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me to complete this class, please

    Thank you JavaPF for your replay, I really need your help. No we don't take an input from the user. The size of the array is already initialized which is 8.

    We make an object bag, then we put different fruits of type String inside the bag using putItem()
    When we remove the fruits, yes we choose which fruit we want to pick using getItem()

    Please help I am stuck and tomorrow is the submission, I was to busy with my mother illness.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help me to complete this class, please

    Sorry I am confused as to how this is ment to work. Do all these things happen automatically?

    Please give me an example output or something to help me better understand!

    Do you have an assignment document I can read?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help me to complete this class, please

    I've edited the code you originally posted so it would compile. There were all sorts of errors.

    Obviousally as i'm unsure as to what this is ment to do, I cannot complete it to work.

    Where are the fruits?!?!

    public class Bag {
     
        private String fruits[];
        private int index;
     
        // Constructor that initillizes the size of array
        public Bag() {
            String[] newFruits = new String[8];
            fruits = newFruits;
        }
     
        // Method to to put an item in the bag
        public boolean putItem(String pItem) {
            if (isFull() == true){
                return false;
            }
            else{
                fruits[index] = pItem;
            index++;
            return true;
            }
     
        }
     
        // Method to check whether the bag is full
        public boolean isFull() {
            if (index == fruits.length - 1){
                return true;
            }
            else{
                return false;
            }
        }
     
        // Method to check whether the bag is empty
        public boolean isEmpty() {
            return (index == 0);
        }
     
        // Method to to get an item in the bag randomly
        public String getItem(String getFruits) {
     
            for (int i = 1; i < fruits.length; i++){
                fruits[i] = getFruits;
            }
            return getFruits;
        }
     
        // Method to display the content in the bag
        public String toString() {
     
            return ("Fruits present in bag: " + fruits);
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  6. #6
    Junior Member
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Help me to complete this class, please

    Hi, I have a couple of suggestions. First, I recommend changing the name of the method putItem() to addFruit(), or something similarly descriptive. Also, change 'pItem' to 'fruit'.

    Quote Originally Posted by k_w_r2007 View Post
        // Method to to put an item in the bag
        public void putItem(String pItem){
            if(isFull() == true)
               return false;
     
            else
               fruits[index] = pItem;
               index ++;
               return true;
        }
    This method definition will not compile as is.

    The first keyword is public, an access modifier, which lets any Java class call the method. This is correct.

    The next keyword is void. void tells the compiler that the method will not return a value. However, you included statements to return the value 'true' or the value 'false'. The compiler will complain as a result. There are two obvious fixes, and probably more not so obvious fixes.

    1. Change the return type 'void' to the return type 'boolean'. A boolean type can contain the values 'true' and 'false'.

    2. Remove the return values from the method. Instead of using isFull() within the method, use it before trying to add fruit to the bag. This is best practice solution IMHO.

    Here is how I would write the method:

    public void addFruit(String fruit){        
               fruits[index] = fruit;
               index ++;           
    }

    Your main method might look something like this:
    String apple = "Red Delicious Apple";
    if (isFull()){
    System.out.println("Your bag is full, you can't add that item.")
    }
    else{
    Bag.addFruit(apple);
    }

    You can get better help by posting your assignment exactly, word for word, as it was given to you(hopefully written by your professor).
    Last edited by dean_martin; April 21st, 2009 at 11:36 AM.

  7. The Following User Says Thank You to dean_martin For This Useful Post:

    JavaPF (April 21st, 2009)