Recall that a Set is an abstract data type somewhat similar to a Bag, they can store a finite collection of objects without any particular order. However, unlike a Bag, a Set cannot contain duplicates. The add() method for a Set is therefore very similar to the add() method for a Bag, with the additional requirement that it must first confirm that the item being added is unique. Write the member method addLikeASet that implements the following logic: When invoked the method takes the T parameter 'anEntry' and determines if 'anEntry' already exists in the bag. If the bag does not already contain 'anEntry' then the method attempts to add it to the bag, returning true when successful. If the bag already contained 'anEntry' the method does not attempt to add 'anEntry' and returns false. Your implementation code for this problem may NOT access/invoke any of the Bag API methods but, since it is a member method, your code may access the fields numberOfEntries and contents. Your solution code may also include helper methods.

Answer :

mahamnasir

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

${teks-lihat-gambar} lhmarianateixeira

Other Questions