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:
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);
}
}
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.
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.
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?
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?!?!
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 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);
}
}
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
Code :
// 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:
Code :
public void addFruit(String fruit){
fruits[index] = fruit;
index ++;
}
Your main method might look something like this:
Code :
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).