Answer :
Answer:
Here is the addLikeASet() method:
public boolean addLikeASet(T anEntry){ // method takes the T parameter anEntry
boolean res = true; //sets the result to true
if(Arrays.asList(bag).contains(anEntry); { //checks if the bag already contains anEntry
res=false; } //returns false
else { if the bag does not already contain anEntry
bag[numberOfEntries]=anEntry; //enters the anEntry in bag array
numberOfEntries++; } //increments the numberOfEntries by 1
return res; } //returns true
Explanation:
You can also implement this method like this:
public boolean isFilled(){ // the function to check if the bag is full
return numberOfEntries == bag.length; } // number of entries equals length of the bag which shows that the bag is full
public boolean addLikeASet(T anEntry){
boolean res=true;
if (!isFilled() || (Arrays.asList(bag).contains(anEntry)); //checks if the bag is full or contains anEntry already
res=false; //returns false
else { if bag does not already contain anEntry
bag[numberOfEntries]=anEntry; //add anEntry to the bag
numberOfEntries++; } add 1 to the count of numberOfEntries
return res; } //returns true
You can also use a loop to check if anEntry is already present in bag:
for(int i=0; i< bag.length; i++) {
if(anEntry.equals(bag[i])) {
return false; } }
return true;
In this exercise we have to use the knowledge of the JAVA language to write the code, so we have to:
The code is in the attached photo.
So to make it easier the code can be found at:
public boolean addLikeASet(T anEntry){ // method takes the T parameter anEntry
boolean res = true; //sets the result to true
if(Arrays.asList(bag).contains(anEntry); { //checks if the bag already contains anEntry
res=false; } //returns false
else { if the bag does not already contain anEntry
bag[numberOfEntries]=anEntry; //enters the anEntry in bag array
numberOfEntries++; } //increments the numberOfEntries by 1
return res; } //returns true
See more about JAVA at brainly.com/question/26104476
