answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Juli2301 [7.4K]
2 years ago
8

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.
Computers and Technology
1 answer:
lina2011 [118]2 years ago
3 0

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;

You might be interested in
They predicted another cold day in Seattle. They predicted another windy day in Seattle. Combine the sentences into one sentence
VLD [36.1K]

Answer:

They predicted another cold day in Seattle and another windy day in Seattle.

4 0
2 years ago
Which of the following statements is incorrect? An operating system provides an environment for the execution of programs. An op
rosijanka [135]

Answer:

command line and graphical user interface

Explanation:

there were (and still are) operating system with no graphical user interface at all, as for example some Unix releases

8 0
1 year ago
Jeff wants to be an archeologist. He found the information below and wants to properly cite it for his own use. The information
saw5 [17]
<span>“Detail Report for 15-1199.11-Archeologist.” O*NET OnLine. n.p., 2012. Web. 5 May 2013.</span>
7 0
1 year ago
Read 2 more answers
Adele’s mother owns a Daycare and she wants to learn all about this business, to help her mom and own it one day. Which CTSO sho
Gennadij [26K]

Answer:

She should join the Future Business Leaders of America–Phi Beta Lambda

Explanation:

CTSOs are Career and technical student organizations. These organizations are vocational and extracurricular groups based primarily in high schools, colleges and career technological centres, for students in Career and Technical Education. They are important parts of the high school and college programs.

The Future Business Leaders of America–Phi Beta Lambda prepares students to become community-minded business leaders. It provides opportunities to learn career skills and gain leadership experience.

Therefore Adele should pick this CTSO

7 0
2 years ago
Which type of address is used at the transport layer to identify the receiving application?
monitta

Answer:

Port number  is used at the transport layer to identify the receiving application

Explanation:

Transport layer is responsible for overall “end-to-end” communication. It provides communication service to the application process which might be running on “different hosts”.

TCP (Transmission Control Protocol) or UDP (User Data Protocol) is the protocol used in the Transport layer. The port number is essential so that the transport layer can carry packets to the right destination pointing to the right application.

A port number usually contains 16 bit integer. Normally this number will send along with the header. Port number plays a major role in Transport layer.

5 0
1 year ago
Other questions:
  • What is the major function of the network access layer?
    5·1 answer
  • PLEASE HELP!! WILL GIVE BRAINLIEST!!
    15·2 answers
  • Someone else can drive your car if _____.
    12·2 answers
  • #Remember that Fibonacci's sequence is a sequence of numbers
    14·1 answer
  • A key field is used to _____. enter a password uniquely identify records merge data list the most important information
    12·2 answers
  • 4. When emergency changes have to be made to systems, the system software may have to be modified before changes to the requirem
    13·1 answer
  • Probability of theft in an area is 0.03 with expected loss of 20% or 30% of things with probabilities 0.55 and 0.45. Insurance p
    5·1 answer
  • The Boffo Balloon Company makes helium balloons. Large balloons cost $13.00 a dozen, medium-sized balloons cost $11.00 a dozen,
    13·1 answer
  • 4. Why does Hancock believe that our communication online is more honest than we might<br> expect?
    15·2 answers
  • The hostel in which you plan to spend the night tonight offers very interesting rates, as long as you do not arrive too late. Ho
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!