Results 1 to 7 of 7
  1. #1
    pink_872 is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default Help me to complete this class, please

    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 problem is that I am lost with this class, so could anyone help me code it

    Here is my code:

    public class Bag{

    private int fruits[];
    private String arrayOfs[];
    private int index;




    public Bag(int ArrayS[]){
    fruits = ArrayS;
    }


    public static void putItem(String pItem){

    arrayOfs = pItem;
    }


    public boolean isFull(){
    if (index == fruits.length()-1)
    return true;

    else
    return false;
    }


    public boolean isEmpty(){

    return(index == 0);
    }


    public String getItem(String fruits){

    for(i=1; i<5; i++)
    fruits[i] = fruits [i+1];
    }


    public String displayBag(){

    return("Fruits present in bag: " +);
    }
    }

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    you want fruit type string but you have int fruit...

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default ArrayList

    It would be better to use an ArrayList, because it can grow as needed (dynamic). An array is static: once the size is defined, it can't grow beyond that size.

    ArrayList (Java Platform SE 6)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    No Prob...

    First lets start with the bag... The bag is a container that will hold your fruit... So the bag will be your String Array... A bag can only hold so many pieces of fruit until it gets full... So will your array...

    When you take a piece of fruit out of the bag/Array it will have an empty spot ready to be replaced with a different piece of fruit...

    So... In your constructor you pass the initial Array of Fruit...

    Java Code:
    public Bag(int ArrayS[]){
    fruits = ArrayS;
    }

    Which is good... Now your bag is full of fruit...


    Now every time you take a piece of fruit out you will leave an empty spot... Google math.random to get a random number which you will use to pick a piece of fruit from your array and return it...

    fruits[0] is one piece of fruit...
    fruits[1] is another piece of fruit...
    etc...

    Now once you pick the item in the array fill it with an empty string and return the piece of fruit...

    __________________________________________________ _____________



    Now to put an item in the bag you will do the oposite... You will have to loop through the fruits array until you hit an empty string... Once you find an empty spot fill it with the new piece of fruit...

    __________________________________________________ ______________

    Finally if the fruits array is full of empty strings, then your bag is... Well... Empty...

    __________________________________________________ ______________

    Let me know where you need more help...
    Who Cares... As Long As It Works...

  5. #5
    pink_872 is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    Thanks for your replies, but still I am facing some problems. The professer said that we should have and int variable named index that keeps a track of the array, but still now I don't get it. I modified my class to this

    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 static 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] = fruits [i+1];
    }

    // Method to display the content in the bag
    public String displayBag(){

    return("Fruits present in bag: " +fruits);
    }
    }

    Please help me, I am tottally lost.

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    you should initialize index to 0 in the default constructor
    and why putItem method is static?

    you may Override String toString() method for printing fruits in the array

    randomize, read Random (Java Platform SE 6)
    Last edited by mtyoung; 04-21-2009 at 12:20 PM.

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Index keeps track of the number of fruit in the bag. This avoids having to treat the elements (fruit) in the array as a pseudo-stack and you can use the method described by Mark in post #4.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Replies: 2
    Last Post: 01-12-2009, 08:36 AM
  2. Complete code won't run in eclipse
    By confusedStudent in forum New To Java
    Replies: 2
    Last Post: 12-19-2008, 08:43 AM
  3. Download the Complete API?
    By JDCAce in forum NetBeans
    Replies: 2
    Last Post: 10-02-2008, 01:02 AM
  4. Please complete the setRate mthod
    By Adiel224 in forum New To Java
    Replies: 6
    Last Post: 09-19-2008, 10:41 AM
  5. Error : Runnable did not complete within 10000ms
    By piyushgpt1 in forum Advanced Java
    Replies: 1
    Last Post: 02-05-2008, 07:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •