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'.
Originally Posted by
k_w_r2007
// 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).